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