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