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