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\ConstantBooleanType;
15: use PHPStan\Type\Constant\ConstantIntegerType;
16: use PHPStan\Type\Constant\ConstantStringType;
17: use PHPStan\Type\ErrorType;
18: use PHPStan\Type\FloatType;
19: use PHPStan\Type\GeneralizePrecision;
20: use PHPStan\Type\IntegerType;
21: use PHPStan\Type\IntersectionType;
22: use PHPStan\Type\IsSuperTypeOfResult;
23: use PHPStan\Type\StringType;
24: use PHPStan\Type\Traits\NonArrayTypeTrait;
25: use PHPStan\Type\Traits\NonCallableTypeTrait;
26: use PHPStan\Type\Traits\NonGenericTypeTrait;
27: use PHPStan\Type\Traits\NonIterableTypeTrait;
28: use PHPStan\Type\Traits\NonObjectTypeTrait;
29: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
30: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
31: use PHPStan\Type\Type;
32: use PHPStan\Type\TypeCombinator;
33: use PHPStan\Type\UnionType;
34: use PHPStan\Type\VerbosityLevel;
35:
36: class AccessoryNumericStringType implements CompoundType, AccessoryType
37: {
38:
39: use NonArrayTypeTrait;
40: use NonCallableTypeTrait;
41: use NonObjectTypeTrait;
42: use NonIterableTypeTrait;
43: use UndecidedBooleanTypeTrait;
44: use UndecidedComparisonCompoundTypeTrait;
45: use NonGenericTypeTrait;
46:
47: /** @api */
48: public function __construct()
49: {
50: }
51:
52: public function getReferencedClasses(): array
53: {
54: return [];
55: }
56:
57: public function getObjectClassNames(): array
58: {
59: return [];
60: }
61:
62: public function getObjectClassReflections(): array
63: {
64: return [];
65: }
66:
67: public function getConstantStrings(): array
68: {
69: return [];
70: }
71:
72: public function accepts(Type $type, bool $strictTypes): AcceptsResult
73: {
74: $isNumericString = $type->isNumericString();
75:
76: if ($isNumericString->yes()) {
77: return AcceptsResult::createYes();
78: }
79:
80: if ($type instanceof CompoundType) {
81: return $type->isAcceptedBy($this, $strictTypes);
82: }
83:
84: return new AcceptsResult($isNumericString, []);
85: }
86:
87: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
88: {
89: if ($type instanceof CompoundType) {
90: return $type->isSubTypeOf($this);
91: }
92:
93: if ($this->equals($type)) {
94: return IsSuperTypeOfResult::createYes();
95: }
96:
97: return new IsSuperTypeOfResult($type->isNumericString(), []);
98: }
99:
100: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
101: {
102: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
103: return $otherType->isSuperTypeOf($this);
104: }
105:
106: return new IsSuperTypeOfResult(
107: $otherType->isNumericString()->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()),
108: [],
109: );
110: }
111:
112: public function isAcceptedBy(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 $this->isSubTypeOf($acceptingType)->toAcceptsResult();
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: isList: TrinaryLogic::createYes(),
215: );
216: }
217:
218: public function toArrayKey(): Type
219: {
220: return new UnionType([
221: new IntegerType(),
222: new IntersectionType([
223: new StringType(),
224: new AccessoryNumericStringType(),
225: ]),
226: ]);
227: }
228:
229: public function toCoercedArgumentType(bool $strictTypes): Type
230: {
231: if (!$strictTypes) {
232: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean());
233: }
234:
235: return $this;
236: }
237:
238: public function isNull(): TrinaryLogic
239: {
240: return TrinaryLogic::createNo();
241: }
242:
243: public function isConstantValue(): TrinaryLogic
244: {
245: return TrinaryLogic::createMaybe();
246: }
247:
248: public function isConstantScalarValue(): TrinaryLogic
249: {
250: return TrinaryLogic::createMaybe();
251: }
252:
253: public function getConstantScalarTypes(): array
254: {
255: return [];
256: }
257:
258: public function getConstantScalarValues(): array
259: {
260: return [];
261: }
262:
263: public function isTrue(): TrinaryLogic
264: {
265: return TrinaryLogic::createNo();
266: }
267:
268: public function isFalse(): TrinaryLogic
269: {
270: return TrinaryLogic::createNo();
271: }
272:
273: public function isBoolean(): TrinaryLogic
274: {
275: return TrinaryLogic::createNo();
276: }
277:
278: public function isFloat(): TrinaryLogic
279: {
280: return TrinaryLogic::createNo();
281: }
282:
283: public function isInteger(): TrinaryLogic
284: {
285: return TrinaryLogic::createNo();
286: }
287:
288: public function isString(): TrinaryLogic
289: {
290: return TrinaryLogic::createYes();
291: }
292:
293: public function isNumericString(): TrinaryLogic
294: {
295: return TrinaryLogic::createYes();
296: }
297:
298: public function isNonEmptyString(): TrinaryLogic
299: {
300: return TrinaryLogic::createYes();
301: }
302:
303: public function isNonFalsyString(): TrinaryLogic
304: {
305: return TrinaryLogic::createMaybe();
306: }
307:
308: public function isLiteralString(): TrinaryLogic
309: {
310: return TrinaryLogic::createMaybe();
311: }
312:
313: public function isLowercaseString(): TrinaryLogic
314: {
315: return TrinaryLogic::createMaybe();
316: }
317:
318: public function isUppercaseString(): TrinaryLogic
319: {
320: return TrinaryLogic::createMaybe();
321: }
322:
323: public function isClassString(): TrinaryLogic
324: {
325: return TrinaryLogic::createNo();
326: }
327:
328: public function getClassStringObjectType(): Type
329: {
330: return new ErrorType();
331: }
332:
333: public function getObjectTypeOrClassStringObjectType(): Type
334: {
335: return new ErrorType();
336: }
337:
338: public function isVoid(): TrinaryLogic
339: {
340: return TrinaryLogic::createNo();
341: }
342:
343: public function isScalar(): TrinaryLogic
344: {
345: return TrinaryLogic::createYes();
346: }
347:
348: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
349: {
350: if ($type->isNull()->yes()) {
351: return new ConstantBooleanType(false);
352: }
353:
354: if ($type->isString()->yes() && $type->isNumericString()->no()) {
355: return new ConstantBooleanType(false);
356: }
357:
358: return new BooleanType();
359: }
360:
361: public function traverse(callable $cb): Type
362: {
363: return $this;
364: }
365:
366: public function traverseSimultaneously(Type $right, callable $cb): Type
367: {
368: return $this;
369: }
370:
371: public function generalize(GeneralizePrecision $precision): Type
372: {
373: return new StringType();
374: }
375:
376: public function tryRemove(Type $typeToRemove): ?Type
377: {
378: if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '0') {
379: return new IntersectionType([$this, new AccessoryNonFalsyStringType()]);
380: }
381:
382: return null;
383: }
384:
385: public function exponentiate(Type $exponent): Type
386: {
387: return new BenevolentUnionType([
388: new FloatType(),
389: new IntegerType(),
390: ]);
391: }
392:
393: public function getFiniteTypes(): array
394: {
395: return [];
396: }
397:
398: public function toPhpDocNode(): TypeNode
399: {
400: return new IdentifierTypeNode('numeric-string');
401: }
402:
403: public function hasTemplateOrLateResolvableType(): bool
404: {
405: return false;
406: }
407:
408: }
409: