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