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