1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
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\TrinaryLogic;
10: use PHPStan\Turbo\ShadowedByTurboExtension;
11: use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
12: use PHPStan\Type\Constant\ConstantArrayType;
13: use PHPStan\Type\Constant\ConstantBooleanType;
14: use PHPStan\Type\Constant\ConstantIntegerType;
15: use PHPStan\Type\Generic\TemplateIntegerRangeType;
16: use PHPStan\Type\Traits\NonArrayTypeTrait;
17: use PHPStan\Type\Traits\NonCallableTypeTrait;
18: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
19: use PHPStan\Type\Traits\NonGenericTypeTrait;
20: use PHPStan\Type\Traits\NonIterableTypeTrait;
21: use PHPStan\Type\Traits\NonObjectTypeTrait;
22: use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait;
23: use PHPStan\Type\Traits\OperatorTypeTrait;
24: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
25: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
26:
27: /** @api */
28: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/IntegerType.cpp')]
29: #[InstanceofDeprecated(insteadUse: 'Type::isInteger()')]
30: class IntegerType implements Type
31: {
32:
33: use JustNullableTypeTrait;
34: use NonArrayTypeTrait;
35: use NonCallableTypeTrait;
36: use NonIterableTypeTrait;
37: use NonObjectTypeTrait;
38: use UndecidedBooleanTypeTrait;
39: use UndecidedComparisonTypeTrait;
40: use NonGenericTypeTrait;
41: use NonOffsetAccessibleTypeTrait;
42: use NonGeneralizableTypeTrait;
43: use OperatorTypeTrait;
44:
45: /** @api */
46: public function __construct()
47: {
48: }
49:
50: public function describe(VerbosityLevel $level): string
51: {
52: return 'int';
53: }
54:
55: public function getConstantStrings(): array
56: {
57: return [];
58: }
59:
60: public function toNumber(): Type
61: {
62: return $this;
63: }
64:
65: public function toNumberSet(): NumberSet
66: {
67: return new NumberSet([[null, null]], [], false);
68: }
69:
70: /** A subclass may hold numbers the set does not describe, so only the subclasses that override this give theirs. */
71: public function toExactNumberSet(): ?NumberSet
72: {
73: return static::class === self::class ? $this->toNumberSet() : null;
74: }
75:
76: public function toBitwiseNotType(): Type
77: {
78: return new IntegerType();
79: }
80:
81: public function toAbsoluteNumber(): Type
82: {
83: return IntegerRangeType::createAllGreaterThanOrEqualTo(0);
84: }
85:
86: public function toFloat(): Type
87: {
88: return new FloatType();
89: }
90:
91: public function toInteger(): Type
92: {
93: return $this;
94: }
95:
96: public function toString(): Type
97: {
98: return new IntersectionType([
99: new StringType(),
100: new AccessoryDecimalIntegerStringType(),
101: ]);
102: }
103:
104: public function toArray(): Type
105: {
106: return new ConstantArrayType(
107: [new ConstantIntegerType(0)],
108: [$this],
109: [1],
110: isList: TrinaryLogic::createYes(),
111: );
112: }
113:
114: public function toArrayKey(): Type
115: {
116: return $this;
117: }
118:
119: public function toCoercedArgumentType(bool $strictTypes): Type
120: {
121: if (!$strictTypes) {
122: return TypeCombinator::union($this, $this->toFloat(), $this->toString(), $this->toBoolean());
123: }
124:
125: return TypeCombinator::union($this, $this->toFloat());
126: }
127:
128: public function isOffsetAccessLegal(): TrinaryLogic
129: {
130: return TrinaryLogic::createYes();
131: }
132:
133: public function getKindMask(): int
134: {
135: return TypeKind::INT;
136: }
137:
138: public function isNull(): TrinaryLogic
139: {
140: return TrinaryLogic::createNo();
141: }
142:
143: public function isTrue(): TrinaryLogic
144: {
145: return TrinaryLogic::createNo();
146: }
147:
148: public function isFalse(): TrinaryLogic
149: {
150: return TrinaryLogic::createNo();
151: }
152:
153: public function isBoolean(): TrinaryLogic
154: {
155: return TrinaryLogic::createNo();
156: }
157:
158: public function isFloat(): TrinaryLogic
159: {
160: return TrinaryLogic::createNo();
161: }
162:
163: public function isInteger(): TrinaryLogic
164: {
165: return TrinaryLogic::createYes();
166: }
167:
168: public function isScalar(): TrinaryLogic
169: {
170: return TrinaryLogic::createYes();
171: }
172:
173: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
174: {
175: if ($type->isArray()->yes()) {
176: return new ConstantBooleanType(false);
177: }
178:
179: if (
180: $phpVersion->nonNumericStringAndIntegerIsFalseOnLooseComparison()
181: && $type->isString()->yes()
182: && $type->isNumericString()->no()
183: ) {
184: return new ConstantBooleanType(false);
185: }
186:
187: return new BooleanType();
188: }
189:
190: public function tryRemove(Type $typeToRemove): ?Type
191: {
192: // a template stands for an unknown part of its range, not for all of it
193: if ($typeToRemove instanceof TemplateIntegerRangeType) {
194: return null;
195: }
196:
197: if ($typeToRemove instanceof IntegerRangeType || $typeToRemove instanceof ConstantIntegerType) {
198: if ($typeToRemove instanceof IntegerRangeType) {
199: $removeValueMin = $typeToRemove->getMin();
200: $removeValueMax = $typeToRemove->getMax();
201: } else {
202: $removeValueMin = $typeToRemove->getValue();
203: $removeValueMax = $typeToRemove->getValue();
204: }
205: $lowerPart = $removeValueMin !== null ? IntegerRangeType::fromInterval(null, $removeValueMin, -1) : null;
206: $upperPart = $removeValueMax !== null ? IntegerRangeType::fromInterval($removeValueMax, null, +1) : null;
207: if ($lowerPart !== null && $upperPart !== null) {
208: return new UnionType([$lowerPart, $upperPart]);
209: }
210: return $lowerPart ?? $upperPart ?? new NeverType();
211: }
212:
213: return null;
214: }
215:
216: public function getFiniteTypes(): array
217: {
218: return [];
219: }
220:
221: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
222: {
223: return $this->toValueSet()->arithmeticPower($exponent->toValueSet(), $phpVersion);
224: }
225:
226: public function toPhpDocNode(): TypeNode
227: {
228: return new IdentifierTypeNode('int');
229: }
230:
231: public function toNativePhpTypeNode(NativeTypePosition $position)
232: {
233: return $position->isScalarAllowed() ? new Identifier('int') : null;
234: }
235:
236: public function hasTemplateOrLateResolvableType(): bool
237: {
238: return false;
239: }
240:
241: public function hasUnresolvedTemplateArgumentType(): bool
242: {
243: return false;
244: }
245:
246: public function toValueSet(): ValueSet
247: {
248: return ValueSet::createNumbers($this->toNumberSet());
249: }
250:
251: public function tryUnion(Type $otherType): ?Type
252: {
253: return null;
254: }
255:
256: public function tryIntersect(Type $otherType): ?Type
257: {
258: return null;
259: }
260:
261: }
262: