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