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