1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Accessory;
4:
5: use PhpParser\Node\Identifier;
6: use PHPStan\Php\PhpVersion;
7: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
9: use PHPStan\TrinaryLogic;
10: use PHPStan\Turbo\ShadowedByTurboExtension;
11: use PHPStan\Type\AcceptsResult;
12: use PHPStan\Type\BenevolentUnionType;
13: use PHPStan\Type\BooleanType;
14: use PHPStan\Type\CompoundType;
15: use PHPStan\Type\Constant\ConstantArrayType;
16: use PHPStan\Type\Constant\ConstantIntegerType;
17: use PHPStan\Type\ErrorType;
18: use PHPStan\Type\FloatType;
19: use PHPStan\Type\GeneralizePrecision;
20: use PHPStan\Type\InstanceofDeprecated;
21: use PHPStan\Type\IntegerType;
22: use PHPStan\Type\IntersectionType;
23: use PHPStan\Type\IsSuperTypeOfResult;
24: use PHPStan\Type\MixedType;
25: use PHPStan\Type\NativeTypePosition;
26: use PHPStan\Type\ObjectWithoutClassType;
27: use PHPStan\Type\StringRefinement;
28: use PHPStan\Type\StringType;
29: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
30: use PHPStan\Type\Traits\NonArrayTypeTrait;
31: use PHPStan\Type\Traits\NonGenericTypeTrait;
32: use PHPStan\Type\Traits\NonIterableTypeTrait;
33: use PHPStan\Type\Traits\NonObjectTypeTrait;
34: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
35: use PHPStan\Type\Traits\OperatorTypeTrait;
36: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
37: use PHPStan\Type\Type;
38: use PHPStan\Type\TypeCombinator;
39: use PHPStan\Type\TypeKind;
40: use PHPStan\Type\UnionType;
41: use PHPStan\Type\ValueSet;
42: use PHPStan\Type\VerbosityLevel;
43:
44: #[InstanceofDeprecated(insteadUse: 'Type::isLiteralString()')]
45: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/AccessoryLiteralStringType.cpp')]
46: class AccessoryLiteralStringType implements CompoundType, AccessoryType
47: {
48:
49: use MaybeCallableTypeTrait;
50: use NonArrayTypeTrait;
51: use NonObjectTypeTrait;
52: use NonIterableTypeTrait;
53: use UndecidedComparisonCompoundTypeTrait;
54: use NonGenericTypeTrait;
55: use NonRemoveableTypeTrait;
56: use OperatorTypeTrait;
57:
58: /** @api */
59: public function __construct()
60: {
61: }
62:
63: public function getReferencedClasses(): array
64: {
65: return [];
66: }
67:
68: public function getObjectClassNames(): array
69: {
70: return [];
71: }
72:
73: public function getObjectClassReflections(): array
74: {
75: return [];
76: }
77:
78: public function getConstantStrings(): array
79: {
80: return [];
81: }
82:
83: public function accepts(Type $type, bool $strictTypes): AcceptsResult
84: {
85: if ($type instanceof MixedType) {
86: return AcceptsResult::createNo();
87: }
88:
89: $isLiteralString = $type->isLiteralString();
90: if ($isLiteralString->yes()) {
91: return AcceptsResult::createYes();
92: }
93:
94: if ($type instanceof CompoundType) {
95: return $type->isAcceptedBy($this, $strictTypes);
96: }
97:
98: return new AcceptsResult($isLiteralString, []);
99: }
100:
101: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
102: {
103: if ($type instanceof CompoundType) {
104: return $type->isSubTypeOf($this);
105: }
106:
107: if ($this->equals($type)) {
108: return IsSuperTypeOfResult::createYes();
109: }
110:
111: return IsSuperTypeOfResult::createFromTrinaryLogic($type->isLiteralString());
112: }
113:
114: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
115: {
116: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
117: return $otherType->isSuperTypeOf($this);
118: }
119:
120: return IsSuperTypeOfResult::createFromTrinaryLogic(
121: $otherType->isLiteralString()->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()),
122: );
123: }
124:
125: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
126: {
127: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
128: }
129:
130: public function equals(Type $type): bool
131: {
132: return $type instanceof self;
133: }
134:
135: public function describe(VerbosityLevel $level): string
136: {
137: return 'literal-string';
138: }
139:
140: public function isOffsetAccessible(): TrinaryLogic
141: {
142: return TrinaryLogic::createYes();
143: }
144:
145: public function isOffsetAccessLegal(): TrinaryLogic
146: {
147: return TrinaryLogic::createYes();
148: }
149:
150: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
151: {
152: return $offsetType->isInteger()->and(TrinaryLogic::createMaybe());
153: }
154:
155: public function getOffsetValueType(Type $offsetType): Type
156: {
157: if ($this->hasOffsetValueType($offsetType)->no()) {
158: return new ErrorType();
159: }
160:
161: return new StringType();
162: }
163:
164: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
165: {
166: $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType);
167:
168: if ($stringOffset instanceof ErrorType) {
169: return $stringOffset;
170: }
171:
172: if ($valueType->isLiteralString()->yes()) {
173: return $this;
174: }
175:
176: return new StringType();
177: }
178:
179: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
180: {
181: return $this;
182: }
183:
184: public function unsetOffset(Type $offsetType): Type
185: {
186: return new ErrorType();
187: }
188:
189: public function toNumber(): Type
190: {
191: return new ErrorType();
192: }
193:
194: public function toBitwiseNotType(): Type
195: {
196: return new StringType();
197: }
198:
199: public function toAbsoluteNumber(): Type
200: {
201: return new ErrorType();
202: }
203:
204: public function toInteger(): Type
205: {
206: return new IntegerType();
207: }
208:
209: public function toFloat(): Type
210: {
211: return new FloatType();
212: }
213:
214: public function toString(): Type
215: {
216: return $this;
217: }
218:
219: public function toBoolean(): BooleanType
220: {
221: return new BooleanType();
222: }
223:
224: public function toArray(): Type
225: {
226: return new ConstantArrayType(
227: [new ConstantIntegerType(0)],
228: [$this],
229: [1],
230: isList: TrinaryLogic::createYes(),
231: );
232: }
233:
234: public function toArrayKey(): Type
235: {
236: return $this;
237: }
238:
239: public function toCoercedArgumentType(bool $strictTypes): Type
240: {
241: if (!$strictTypes) {
242: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean());
243: }
244:
245: return $this;
246: }
247:
248: public function getKindMask(): int
249: {
250: return TypeKind::STRING;
251: }
252:
253: public function isNull(): TrinaryLogic
254: {
255: return TrinaryLogic::createNo();
256: }
257:
258: public function isConstantValue(): TrinaryLogic
259: {
260: return TrinaryLogic::createMaybe();
261: }
262:
263: public function isConstantScalarValue(): TrinaryLogic
264: {
265: return TrinaryLogic::createMaybe();
266: }
267:
268: public function getConstantScalarTypes(): array
269: {
270: return [];
271: }
272:
273: public function getConstantScalarValues(): array
274: {
275: return [];
276: }
277:
278: public function isTrue(): TrinaryLogic
279: {
280: return TrinaryLogic::createNo();
281: }
282:
283: public function isFalse(): TrinaryLogic
284: {
285: return TrinaryLogic::createNo();
286: }
287:
288: public function isBoolean(): TrinaryLogic
289: {
290: return TrinaryLogic::createNo();
291: }
292:
293: public function isFloat(): TrinaryLogic
294: {
295: return TrinaryLogic::createNo();
296: }
297:
298: public function isInteger(): TrinaryLogic
299: {
300: return TrinaryLogic::createNo();
301: }
302:
303: public function isString(): TrinaryLogic
304: {
305: return TrinaryLogic::createYes();
306: }
307:
308: public function isNumericString(): TrinaryLogic
309: {
310: return TrinaryLogic::createMaybe();
311: }
312:
313: public function isDecimalIntegerString(): TrinaryLogic
314: {
315: return TrinaryLogic::createMaybe();
316: }
317:
318: public function isNonEmptyString(): TrinaryLogic
319: {
320: return TrinaryLogic::createMaybe();
321: }
322:
323: public function isNonFalsyString(): TrinaryLogic
324: {
325: return TrinaryLogic::createMaybe();
326: }
327:
328: public function isLiteralString(): TrinaryLogic
329: {
330: return TrinaryLogic::createYes();
331: }
332:
333: public function getStringRefinement(): ?int
334: {
335: return StringRefinement::LITERAL;
336: }
337:
338: public function isLowercaseString(): TrinaryLogic
339: {
340: return TrinaryLogic::createMaybe();
341: }
342:
343: public function isClassString(): TrinaryLogic
344: {
345: return TrinaryLogic::createMaybe();
346: }
347:
348: public function isUppercaseString(): TrinaryLogic
349: {
350: return TrinaryLogic::createMaybe();
351: }
352:
353: public function getClassStringObjectType(): Type
354: {
355: return new ObjectWithoutClassType();
356: }
357:
358: public function getObjectTypeOrClassStringObjectType(): Type
359: {
360: return new ObjectWithoutClassType();
361: }
362:
363: public function hasMethod(string $methodName): TrinaryLogic
364: {
365: return TrinaryLogic::createMaybe();
366: }
367:
368: public function isVoid(): TrinaryLogic
369: {
370: return TrinaryLogic::createNo();
371: }
372:
373: public function isScalar(): TrinaryLogic
374: {
375: return TrinaryLogic::createYes();
376: }
377:
378: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
379: {
380: return new BooleanType();
381: }
382:
383: public function traverse(callable $cb): Type
384: {
385: return $this;
386: }
387:
388: public function traverseSimultaneously(Type $right, callable $cb): Type
389: {
390: return $this;
391: }
392:
393: public function generalize(GeneralizePrecision $precision): Type
394: {
395: return new StringType();
396: }
397:
398: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
399: {
400: return new BenevolentUnionType([
401: new FloatType(),
402: new IntegerType(),
403: ]);
404: }
405:
406: public function getFiniteTypes(): array
407: {
408: return [];
409: }
410:
411: public function getDefaultBaseType(): Type
412: {
413: return new StringType();
414: }
415:
416: public function toPhpDocNode(): TypeNode
417: {
418: return new IdentifierTypeNode('literal-string');
419: }
420:
421: public function toNativePhpTypeNode(NativeTypePosition $position)
422: {
423: return $position->isScalarAllowed() ? new Identifier('string') : null;
424: }
425:
426: public function hasTemplateOrLateResolvableType(): bool
427: {
428: return false;
429: }
430:
431: public function hasUnresolvedTemplateArgumentType(): bool
432: {
433: return false;
434: }
435:
436: public function toValueSet(): ValueSet
437: {
438: return ValueSet::createStrings($this);
439: }
440:
441: public function tryUnion(Type $otherType): ?Type
442: {
443: return null;
444: }
445:
446: public function tryIntersect(Type $otherType): ?Type
447: {
448: return null;
449: }
450:
451: }
452: