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