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