1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Accessory;
4:
5: use PhpParser\Node\Identifier;
6: use PHPStan\Php\PhpVersion;
7: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
9: use PHPStan\Reflection\ClassMemberAccessAnswerer;
10: use PHPStan\Reflection\TrivialParametersAcceptor;
11: use PHPStan\ShouldNotHappenException;
12: use PHPStan\TrinaryLogic;
13: use PHPStan\Turbo\ShadowedByTurboExtension;
14: use PHPStan\Type\AcceptsResult;
15: use PHPStan\Type\BenevolentUnionType;
16: use PHPStan\Type\BooleanType;
17: use PHPStan\Type\CompoundType;
18: use PHPStan\Type\Constant\ConstantArrayType;
19: use PHPStan\Type\Constant\ConstantBooleanType;
20: use PHPStan\Type\Constant\ConstantIntegerType;
21: use PHPStan\Type\Constant\ConstantStringType;
22: use PHPStan\Type\ErrorType;
23: use PHPStan\Type\FloatType;
24: use PHPStan\Type\GeneralizePrecision;
25: use PHPStan\Type\IntegerType;
26: use PHPStan\Type\IntersectionType;
27: use PHPStan\Type\IsSuperTypeOfResult;
28: use PHPStan\Type\NativeTypePosition;
29: use PHPStan\Type\StringType;
30: use PHPStan\Type\Traits\NonArrayTypeTrait;
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\UndecidedComparisonCompoundTypeTrait;
36: use PHPStan\Type\Type;
37: use PHPStan\Type\TypeCombinator;
38: use PHPStan\Type\UnionType;
39: use PHPStan\Type\ValueSet;
40: use PHPStan\Type\VerbosityLevel;
41:
42: /**
43: * This accessory type is coupled with `Type::isDecimalIntegerString()` method.
44: *
45: * When inverse=false, this represents strings containing decimal integers.
46: * These are guaranteed to be cast to an integer in an array key.
47: * Examples of constant values covered by this type: "0", "1", "1234", "-1"
48: *
49: * When inverse=true, this represents strings containing non-decimal integers and other text.
50: * These are guaranteed to stay as string in an array key.
51: * Examples of constant values covered by this type: "+1", "00", "18E+3", "1.2", "1,3", "foo"
52: *
53: * @api
54: */
55: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/AccessoryDecimalIntegerStringType.cpp')]
56: class AccessoryDecimalIntegerStringType implements CompoundType, AccessoryType
57: {
58:
59: use NonArrayTypeTrait;
60: use NonObjectTypeTrait;
61: use NonIterableTypeTrait;
62: use UndecidedComparisonCompoundTypeTrait;
63: use NonGenericTypeTrait;
64: use OperatorTypeTrait;
65:
66: /** @api */
67: public function __construct(private bool $inverse = false)
68: {
69: }
70:
71: public function getReferencedClasses(): array
72: {
73: return [];
74: }
75:
76: public function getObjectClassNames(): array
77: {
78: return [];
79: }
80:
81: public function getObjectClassReflections(): array
82: {
83: return [];
84: }
85:
86: public function getConstantStrings(): array
87: {
88: return [];
89: }
90:
91: public function accepts(Type $type, bool $strictTypes): AcceptsResult
92: {
93: $isDecimalIntegerString = $type->isDecimalIntegerString();
94:
95: if (
96: $type->isString()->yes()
97: && ($this->inverse ? $isDecimalIntegerString->no() : $isDecimalIntegerString->yes())
98: ) {
99: return AcceptsResult::createYes();
100: }
101:
102: if ($type instanceof CompoundType) {
103: return $type->isAcceptedBy($this, $strictTypes);
104: }
105:
106: $result = $type->isString()->and($this->inverse ? $isDecimalIntegerString->negate() : $isDecimalIntegerString);
107:
108: return new AcceptsResult($result, []);
109: }
110:
111: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
112: {
113: if ($type instanceof CompoundType) {
114: return $type->isSubTypeOf($this);
115: }
116:
117: if ($this->equals($type)) {
118: return IsSuperTypeOfResult::createYes();
119: }
120:
121: $isDecimalIntegerString = $type->isDecimalIntegerString();
122: $result = $type->isString()->and($this->inverse ? $isDecimalIntegerString->negate() : $isDecimalIntegerString);
123:
124: return IsSuperTypeOfResult::createFromTrinaryLogic($result);
125: }
126:
127: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
128: {
129: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
130: return $otherType->isSuperTypeOf($this);
131: }
132:
133: if (
134: (
135: $otherType instanceof AccessoryNumericStringType
136: || $otherType instanceof AccessoryNonEmptyStringType
137: || $otherType instanceof AccessoryLowercaseStringType
138: || $otherType instanceof AccessoryUppercaseStringType
139: )
140: && !$this->inverse
141: ) {
142: return IsSuperTypeOfResult::createYes();
143: }
144:
145: $otherTypeResult = $otherType->isString()->and($this->inverse ? $otherType->isDecimalIntegerString()->negate() : $otherType->isDecimalIntegerString());
146:
147: return IsSuperTypeOfResult::createFromTrinaryLogic(
148: $otherTypeResult->and($otherType->equals($this) ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()),
149: );
150: }
151:
152: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
153: {
154: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
155: }
156:
157: public function equals(Type $type): bool
158: {
159: return $type instanceof self && $this->inverse === $type->inverse;
160: }
161:
162: public function describe(VerbosityLevel $level): string
163: {
164: return $this->inverse ? 'non-decimal-int-string' : 'decimal-int-string';
165: }
166:
167: public function isOffsetAccessible(): TrinaryLogic
168: {
169: return TrinaryLogic::createYes();
170: }
171:
172: public function isOffsetAccessLegal(): TrinaryLogic
173: {
174: return TrinaryLogic::createYes();
175: }
176:
177: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
178: {
179: return $offsetType->isInteger()->and(TrinaryLogic::createMaybe());
180: }
181:
182: public function getOffsetValueType(Type $offsetType): Type
183: {
184: if ($this->hasOffsetValueType($offsetType)->no()) {
185: return new ErrorType();
186: }
187:
188: return new StringType();
189: }
190:
191: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
192: {
193: $stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);
194:
195: if ($stringOffset instanceof ErrorType) {
196: return $stringOffset;
197: }
198:
199: return $this;
200: }
201:
202: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
203: {
204: return $this;
205: }
206:
207: public function unsetOffset(Type $offsetType): Type
208: {
209: return new ErrorType();
210: }
211:
212: public function tryRemove(Type $typeToRemove): ?Type
213: {
214: if ($this->inverse) {
215: return null;
216: }
217:
218: if ($typeToRemove instanceof ConstantStringType && $typeToRemove->getValue() === '0') {
219: return new IntersectionType([new StringType(), $this, new AccessoryNonFalsyStringType()]);
220: }
221:
222: return null;
223: }
224:
225: public function toNumber(): Type
226: {
227: if ($this->inverse) {
228: return new UnionType([
229: $this->toInteger(),
230: $this->toFloat(),
231: ]);
232: }
233:
234: return $this->toInteger();
235: }
236:
237: public function toAbsoluteNumber(): Type
238: {
239: return $this->toNumber()->toAbsoluteNumber();
240: }
241:
242: public function toBitwiseNotType(): Type
243: {
244: // Decimal integer strings are non-empty when not inverted
245: // (`"0"` / `"123"` are still at least one character). `~$s`
246: // returns a string of the same length, so the non-empty flag
247: // survives. The decimal-integer property doesn't survive the
248: // bitwise-not, hence we drop the accessory.
249: return $this->isNonEmptyString()->yes()
250: ? new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()])
251: : new StringType();
252: }
253:
254: public function toBoolean(): BooleanType
255: {
256: return $this->isNonFalsyString()->negate()->toBooleanType();
257: }
258:
259: public function toInteger(): Type
260: {
261: return new IntegerType();
262: }
263:
264: public function toFloat(): Type
265: {
266: return new FloatType();
267: }
268:
269: public function toString(): Type
270: {
271: return $this;
272: }
273:
274: public function toArray(): Type
275: {
276: return new ConstantArrayType(
277: [new ConstantIntegerType(0)],
278: [$this],
279: [1],
280: isList: TrinaryLogic::createYes(),
281: );
282: }
283:
284: public function toArrayKey(): Type
285: {
286: if ($this->inverse) {
287: return $this;
288: }
289:
290: return new IntegerType();
291: }
292:
293: public function toCoercedArgumentType(bool $strictTypes): Type
294: {
295: if (!$strictTypes) {
296: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this, $this->toBoolean());
297: }
298:
299: return $this;
300: }
301:
302: public function isNull(): TrinaryLogic
303: {
304: return TrinaryLogic::createNo();
305: }
306:
307: public function isConstantValue(): TrinaryLogic
308: {
309: return TrinaryLogic::createMaybe();
310: }
311:
312: public function isConstantScalarValue(): TrinaryLogic
313: {
314: return TrinaryLogic::createMaybe();
315: }
316:
317: public function getConstantScalarTypes(): array
318: {
319: return [];
320: }
321:
322: public function getConstantScalarValues(): array
323: {
324: return [];
325: }
326:
327: public function isCallable(): TrinaryLogic
328: {
329: return $this->inverse ? TrinaryLogic::createMaybe() : TrinaryLogic::createNo();
330: }
331:
332: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
333: {
334: if ($this->inverse) {
335: return [new TrivialParametersAcceptor()];
336: }
337:
338: throw new ShouldNotHappenException();
339: }
340:
341: public function isTrue(): TrinaryLogic
342: {
343: return TrinaryLogic::createNo();
344: }
345:
346: public function isFalse(): TrinaryLogic
347: {
348: return TrinaryLogic::createNo();
349: }
350:
351: public function isBoolean(): TrinaryLogic
352: {
353: return TrinaryLogic::createNo();
354: }
355:
356: public function isFloat(): TrinaryLogic
357: {
358: return TrinaryLogic::createNo();
359: }
360:
361: public function isInteger(): TrinaryLogic
362: {
363: return TrinaryLogic::createNo();
364: }
365:
366: public function isString(): TrinaryLogic
367: {
368: return TrinaryLogic::createYes();
369: }
370:
371: public function isNumericString(): TrinaryLogic
372: {
373: return $this->inverse ? TrinaryLogic::createMaybe() : TrinaryLogic::createYes();
374: }
375:
376: public function isDecimalIntegerString(): TrinaryLogic
377: {
378: return TrinaryLogic::createFromBoolean(!$this->inverse);
379: }
380:
381: public function isNonEmptyString(): TrinaryLogic
382: {
383: return $this->inverse ? TrinaryLogic::createMaybe() : TrinaryLogic::createYes();
384: }
385:
386: public function isNonFalsyString(): TrinaryLogic
387: {
388: return TrinaryLogic::createMaybe();
389: }
390:
391: public function isLiteralString(): TrinaryLogic
392: {
393: return TrinaryLogic::createMaybe();
394: }
395:
396: public function isLowercaseString(): TrinaryLogic
397: {
398: return $this->inverse ? TrinaryLogic::createMaybe() : TrinaryLogic::createYes();
399: }
400:
401: public function isUppercaseString(): TrinaryLogic
402: {
403: return $this->inverse ? TrinaryLogic::createMaybe() : TrinaryLogic::createYes();
404: }
405:
406: public function isClassString(): TrinaryLogic
407: {
408: return TrinaryLogic::createNo();
409: }
410:
411: public function getClassStringObjectType(): Type
412: {
413: return new ErrorType();
414: }
415:
416: public function getObjectTypeOrClassStringObjectType(): Type
417: {
418: return new ErrorType();
419: }
420:
421: public function isVoid(): TrinaryLogic
422: {
423: return TrinaryLogic::createNo();
424: }
425:
426: public function isScalar(): TrinaryLogic
427: {
428: return TrinaryLogic::createYes();
429: }
430:
431: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
432: {
433: if ($this->inverse) {
434: // may be numeric ("02", "2.0") or empty (""), so nothing is decidable
435: return new BooleanType();
436: }
437:
438: // a decimal-int-string is a non-empty numeric string, so it compares
439: // like one: never loosely equal to null or to a non-numeric string
440: if ($type->isNull()->yes()) {
441: return new ConstantBooleanType(false);
442: }
443:
444: if ($type->isString()->yes() && $type->isNumericString()->no()) {
445: return new ConstantBooleanType(false);
446: }
447:
448: return new BooleanType();
449: }
450:
451: public function traverse(callable $cb): Type
452: {
453: return $this;
454: }
455:
456: public function traverseSimultaneously(Type $right, callable $cb): Type
457: {
458: return $this;
459: }
460:
461: public function generalize(GeneralizePrecision $precision): Type
462: {
463: return new StringType();
464: }
465:
466: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
467: {
468: return new BenevolentUnionType([
469: new FloatType(),
470: new IntegerType(),
471: ]);
472: }
473:
474: public function getFiniteTypes(): array
475: {
476: return [];
477: }
478:
479: public function getDefaultBaseType(): Type
480: {
481: return new StringType();
482: }
483:
484: public function toPhpDocNode(): TypeNode
485: {
486: return new IdentifierTypeNode($this->inverse ? 'non-decimal-int-string' : 'decimal-int-string');
487: }
488:
489: public function toNativePhpTypeNode(NativeTypePosition $position)
490: {
491: return $position->isScalarAllowed() ? new Identifier('string') : null;
492: }
493:
494: public function hasTemplateOrLateResolvableType(): bool
495: {
496: return false;
497: }
498:
499: public function toValueSet(): ValueSet
500: {
501: return ValueSet::createStrings($this);
502: }
503:
504: }
505: