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