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