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