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: /** @api */
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: return $this;
162: }
163:
164: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
165: {
166: return $this;
167: }
168:
169: public function unsetOffset(Type $offsetType): Type
170: {
171: return new ErrorType();
172: }
173:
174: public function toNumber(): Type
175: {
176: return new UnionType([
177: $this->toInteger(),
178: $this->toFloat(),
179: ]);
180: }
181:
182: public function toInteger(): Type
183: {
184: return new IntegerType();
185: }
186:
187: public function toFloat(): Type
188: {
189: return new FloatType();
190: }
191:
192: public function toString(): Type
193: {
194: return $this;
195: }
196:
197: public function toArray(): Type
198: {
199: return new ConstantArrayType(
200: [new ConstantIntegerType(0)],
201: [$this],
202: [1],
203: [],
204: TrinaryLogic::createYes(),
205: );
206: }
207:
208: public function toArrayKey(): Type
209: {
210: return new IntegerType();
211: }
212:
213: public function isNull(): TrinaryLogic
214: {
215: return TrinaryLogic::createNo();
216: }
217:
218: public function isConstantValue(): TrinaryLogic
219: {
220: return TrinaryLogic::createMaybe();
221: }
222:
223: public function isConstantScalarValue(): TrinaryLogic
224: {
225: return TrinaryLogic::createMaybe();
226: }
227:
228: public function getConstantScalarTypes(): array
229: {
230: return [];
231: }
232:
233: public function getConstantScalarValues(): array
234: {
235: return [];
236: }
237:
238: public function isTrue(): TrinaryLogic
239: {
240: return TrinaryLogic::createNo();
241: }
242:
243: public function isFalse(): TrinaryLogic
244: {
245: return TrinaryLogic::createNo();
246: }
247:
248: public function isBoolean(): TrinaryLogic
249: {
250: return TrinaryLogic::createNo();
251: }
252:
253: public function isFloat(): TrinaryLogic
254: {
255: return TrinaryLogic::createNo();
256: }
257:
258: public function isInteger(): TrinaryLogic
259: {
260: return TrinaryLogic::createNo();
261: }
262:
263: public function isString(): TrinaryLogic
264: {
265: return TrinaryLogic::createYes();
266: }
267:
268: public function isNumericString(): TrinaryLogic
269: {
270: return TrinaryLogic::createYes();
271: }
272:
273: public function isNonEmptyString(): TrinaryLogic
274: {
275: return TrinaryLogic::createYes();
276: }
277:
278: public function isNonFalsyString(): TrinaryLogic
279: {
280: return TrinaryLogic::createMaybe();
281: }
282:
283: public function isLiteralString(): TrinaryLogic
284: {
285: return TrinaryLogic::createMaybe();
286: }
287:
288: public function isClassStringType(): TrinaryLogic
289: {
290: return TrinaryLogic::createNo();
291: }
292:
293: public function getClassStringObjectType(): Type
294: {
295: return new ErrorType();
296: }
297:
298: public function getObjectTypeOrClassStringObjectType(): Type
299: {
300: return new ErrorType();
301: }
302:
303: public function isVoid(): TrinaryLogic
304: {
305: return TrinaryLogic::createNo();
306: }
307:
308: public function isScalar(): TrinaryLogic
309: {
310: return TrinaryLogic::createYes();
311: }
312:
313: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
314: {
315: return new BooleanType();
316: }
317:
318: public function traverse(callable $cb): Type
319: {
320: return $this;
321: }
322:
323: public function traverseSimultaneously(Type $right, callable $cb): Type
324: {
325: return $this;
326: }
327:
328: public function generalize(GeneralizePrecision $precision): Type
329: {
330: return new StringType();
331: }
332:
333: public static function __set_state(array $properties): Type
334: {
335: return new self();
336: }
337:
338: public function tryRemove(Type $typeToRemove): ?Type
339: {
340: if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '0') {
341: return TypeCombinator::intersect($this, new AccessoryNonFalsyStringType());
342: }
343:
344: return null;
345: }
346:
347: public function exponentiate(Type $exponent): Type
348: {
349: return new BenevolentUnionType([
350: new FloatType(),
351: new IntegerType(),
352: ]);
353: }
354:
355: public function getFiniteTypes(): array
356: {
357: return [];
358: }
359:
360: public function toPhpDocNode(): TypeNode
361: {
362: return new IdentifierTypeNode('numeric-string');
363: }
364:
365: }
366: