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