1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Accessory;
4:
5: use PHPStan\DependencyInjection\ReportUnsafeArrayStringKeyCastingToggle;
6: use PHPStan\Php\PhpVersion;
7: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
9: use PHPStan\TrinaryLogic;
10: use PHPStan\Turbo\ShadowedByTurboExtension;
11: use PHPStan\Type\AcceptsResult;
12: use PHPStan\Type\BenevolentUnionType;
13: use PHPStan\Type\BooleanType;
14: use PHPStan\Type\CompoundType;
15: use PHPStan\Type\Constant\ConstantArrayType;
16: use PHPStan\Type\Constant\ConstantBooleanType;
17: use PHPStan\Type\Constant\ConstantIntegerType;
18: use PHPStan\Type\Constant\ConstantStringType;
19: use PHPStan\Type\ErrorType;
20: use PHPStan\Type\FloatType;
21: use PHPStan\Type\GeneralizePrecision;
22: use PHPStan\Type\InstanceofDeprecated;
23: use PHPStan\Type\IntegerType;
24: use PHPStan\Type\IntersectionType;
25: use PHPStan\Type\IsSuperTypeOfResult;
26: use PHPStan\Type\StringType;
27: use PHPStan\Type\Traits\NonArrayTypeTrait;
28: use PHPStan\Type\Traits\NonCallableTypeTrait;
29: use PHPStan\Type\Traits\NonGenericTypeTrait;
30: use PHPStan\Type\Traits\NonIterableTypeTrait;
31: use PHPStan\Type\Traits\NonObjectTypeTrait;
32: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
33: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
34: use PHPStan\Type\Type;
35: use PHPStan\Type\TypeCombinator;
36: use PHPStan\Type\UnionType;
37: use PHPStan\Type\VerbosityLevel;
38:
39: #[InstanceofDeprecated(insteadUse: 'Type::isNumericString()')]
40: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/AccessoryNumericStringType.cpp')]
41: class AccessoryNumericStringType implements CompoundType, AccessoryType
42: {
43:
44: use NonArrayTypeTrait;
45: use NonCallableTypeTrait;
46: use NonObjectTypeTrait;
47: use NonIterableTypeTrait;
48: use UndecidedBooleanTypeTrait;
49: use UndecidedComparisonCompoundTypeTrait;
50: use NonGenericTypeTrait;
51:
52: /** @api */
53: public function __construct()
54: {
55: }
56:
57: public function getReferencedClasses(): array
58: {
59: return [];
60: }
61:
62: public function getObjectClassNames(): array
63: {
64: return [];
65: }
66:
67: public function getObjectClassReflections(): array
68: {
69: return [];
70: }
71:
72: public function getConstantStrings(): array
73: {
74: return [];
75: }
76:
77: public function accepts(Type $type, bool $strictTypes): AcceptsResult
78: {
79: $isNumericString = $type->isNumericString();
80:
81: if ($isNumericString->yes()) {
82: return AcceptsResult::createYes();
83: }
84:
85: if ($type instanceof CompoundType) {
86: return $type->isAcceptedBy($this, $strictTypes);
87: }
88:
89: return new AcceptsResult($isNumericString, []);
90: }
91:
92: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
93: {
94: if ($type instanceof CompoundType) {
95: return $type->isSubTypeOf($this);
96: }
97:
98: if ($this->equals($type)) {
99: return IsSuperTypeOfResult::createYes();
100: }
101:
102: return new IsSuperTypeOfResult($type->isNumericString(), []);
103: }
104:
105: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
106: {
107: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
108: return $otherType->isSuperTypeOf($this);
109: }
110:
111: return new IsSuperTypeOfResult(
112: $otherType->isNumericString()->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()),
113: [],
114: );
115: }
116:
117: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
118: {
119: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
120: }
121:
122: public function equals(Type $type): bool
123: {
124: return $type instanceof self;
125: }
126:
127: public function describe(VerbosityLevel $level): string
128: {
129: return 'numeric-string';
130: }
131:
132: public function isOffsetAccessible(): TrinaryLogic
133: {
134: return TrinaryLogic::createYes();
135: }
136:
137: public function isOffsetAccessLegal(): TrinaryLogic
138: {
139: return TrinaryLogic::createYes();
140: }
141:
142: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
143: {
144: return $offsetType->isInteger()->and(TrinaryLogic::createMaybe());
145: }
146:
147: public function getOffsetValueType(Type $offsetType): Type
148: {
149: if ($this->hasOffsetValueType($offsetType)->no()) {
150: return new ErrorType();
151: }
152:
153: return new StringType();
154: }
155:
156: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
157: {
158: $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);
159:
160: if ($stringOffset instanceof ErrorType) {
161: return $stringOffset;
162: }
163:
164: return $this;
165: }
166:
167: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
168: {
169: return $this;
170: }
171:
172: public function unsetOffset(Type $offsetType): Type
173: {
174: return new ErrorType();
175: }
176:
177: public function toNumber(): Type
178: {
179: return new UnionType([
180: $this->toInteger(),
181: $this->toFloat(),
182: ]);
183: }
184:
185: public function toBitwiseNotType(): Type
186: {
187: return new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]);
188: }
189:
190: public function toAbsoluteNumber(): Type
191: {
192: return $this->toNumber()->toAbsoluteNumber();
193: }
194:
195: public function toInteger(): Type
196: {
197: return new IntegerType();
198: }
199:
200: public function toFloat(): Type
201: {
202: return new FloatType();
203: }
204:
205: public function toString(): Type
206: {
207: return $this;
208: }
209:
210: public function toArray(): Type
211: {
212: return new ConstantArrayType(
213: [new ConstantIntegerType(0)],
214: [$this],
215: [1],
216: isList: TrinaryLogic::createYes(),
217: );
218: }
219:
220: public function toArrayKey(): Type
221: {
222: $level = ReportUnsafeArrayStringKeyCastingToggle::getLevel();
223: if ($level !== ReportUnsafeArrayStringKeyCastingToggle::PREVENT) {
224: return new UnionType([
225: new IntegerType(),
226: new IntersectionType([
227: new StringType(),
228: new AccessoryNumericStringType(),
229: ]),
230: ]);
231: }
232:
233: return new UnionType([
234: new IntegerType(),
235: new IntersectionType([new StringType(), new AccessoryDecimalIntegerStringType(inverse: true)]),
236: ]);
237: }
238:
239: public function toCoercedArgumentType(bool $strictTypes): Type
240: {
241: if (!$strictTypes) {
242: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean());
243: }
244:
245: return $this;
246: }
247:
248: public function isNull(): TrinaryLogic
249: {
250: return TrinaryLogic::createNo();
251: }
252:
253: public function isConstantValue(): TrinaryLogic
254: {
255: return TrinaryLogic::createMaybe();
256: }
257:
258: public function isConstantScalarValue(): TrinaryLogic
259: {
260: return TrinaryLogic::createMaybe();
261: }
262:
263: public function getConstantScalarTypes(): array
264: {
265: return [];
266: }
267:
268: public function getConstantScalarValues(): array
269: {
270: return [];
271: }
272:
273: public function isTrue(): TrinaryLogic
274: {
275: return TrinaryLogic::createNo();
276: }
277:
278: public function isFalse(): TrinaryLogic
279: {
280: return TrinaryLogic::createNo();
281: }
282:
283: public function isBoolean(): TrinaryLogic
284: {
285: return TrinaryLogic::createNo();
286: }
287:
288: public function isFloat(): TrinaryLogic
289: {
290: return TrinaryLogic::createNo();
291: }
292:
293: public function isInteger(): TrinaryLogic
294: {
295: return TrinaryLogic::createNo();
296: }
297:
298: public function isString(): TrinaryLogic
299: {
300: return TrinaryLogic::createYes();
301: }
302:
303: public function isNumericString(): TrinaryLogic
304: {
305: return TrinaryLogic::createYes();
306: }
307:
308: public function isDecimalIntegerString(): TrinaryLogic
309: {
310: return TrinaryLogic::createMaybe();
311: }
312:
313: public function isNonEmptyString(): TrinaryLogic
314: {
315: return TrinaryLogic::createYes();
316: }
317:
318: public function isNonFalsyString(): TrinaryLogic
319: {
320: return TrinaryLogic::createMaybe();
321: }
322:
323: public function isLiteralString(): TrinaryLogic
324: {
325: return TrinaryLogic::createMaybe();
326: }
327:
328: public function isLowercaseString(): TrinaryLogic
329: {
330: return TrinaryLogic::createMaybe();
331: }
332:
333: public function isUppercaseString(): TrinaryLogic
334: {
335: return TrinaryLogic::createMaybe();
336: }
337:
338: public function isClassString(): TrinaryLogic
339: {
340: return TrinaryLogic::createNo();
341: }
342:
343: public function getClassStringObjectType(): Type
344: {
345: return new ErrorType();
346: }
347:
348: public function getObjectTypeOrClassStringObjectType(): Type
349: {
350: return new ErrorType();
351: }
352:
353: public function isVoid(): TrinaryLogic
354: {
355: return TrinaryLogic::createNo();
356: }
357:
358: public function isScalar(): TrinaryLogic
359: {
360: return TrinaryLogic::createYes();
361: }
362:
363: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
364: {
365: if ($type->isNull()->yes()) {
366: return new ConstantBooleanType(false);
367: }
368:
369: if ($type->isString()->yes() && $type->isNumericString()->no()) {
370: return new ConstantBooleanType(false);
371: }
372:
373: return new BooleanType();
374: }
375:
376: public function traverse(callable $cb): Type
377: {
378: return $this;
379: }
380:
381: public function traverseSimultaneously(Type $right, callable $cb): Type
382: {
383: return $this;
384: }
385:
386: public function generalize(GeneralizePrecision $precision): Type
387: {
388: return new StringType();
389: }
390:
391: public function tryRemove(Type $typeToRemove): ?Type
392: {
393: if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '0') {
394: return new IntersectionType([new StringType(), $this, new AccessoryNonFalsyStringType()]);
395: }
396:
397: return null;
398: }
399:
400: public function exponentiate(Type $exponent): Type
401: {
402: return new BenevolentUnionType([
403: new FloatType(),
404: new IntegerType(),
405: ]);
406: }
407:
408: public function getFiniteTypes(): array
409: {
410: return [];
411: }
412:
413: public function getDefaultBaseType(): Type
414: {
415: return new StringType();
416: }
417:
418: public function toPhpDocNode(): TypeNode
419: {
420: return new IdentifierTypeNode('numeric-string');
421: }
422:
423: public function hasTemplateOrLateResolvableType(): bool
424: {
425: return false;
426: }
427:
428: }
429: