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