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