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