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 toAbsoluteNumber(): Type
61: {
62: return IntegerRangeType::createAllGreaterThanOrEqualTo(0);
63: }
64:
65: public function toFloat(): Type
66: {
67: return new FloatType();
68: }
69:
70: public function toInteger(): Type
71: {
72: return $this;
73: }
74:
75: public function toString(): Type
76: {
77: return new IntersectionType([
78: new StringType(),
79: new AccessoryLowercaseStringType(),
80: new AccessoryUppercaseStringType(),
81: new AccessoryNumericStringType(),
82: ]);
83: }
84:
85: public function toArray(): Type
86: {
87: return new ConstantArrayType(
88: [new ConstantIntegerType(0)],
89: [$this],
90: [1],
91: isList: TrinaryLogic::createYes(),
92: );
93: }
94:
95: public function toArrayKey(): Type
96: {
97: return $this;
98: }
99:
100: public function toCoercedArgumentType(bool $strictTypes): Type
101: {
102: if (!$strictTypes) {
103: return TypeCombinator::union($this, $this->toFloat(), $this->toString(), $this->toBoolean());
104: }
105:
106: return TypeCombinator::union($this, $this->toFloat());
107: }
108:
109: public function isOffsetAccessLegal(): TrinaryLogic
110: {
111: return TrinaryLogic::createYes();
112: }
113:
114: public function isNull(): TrinaryLogic
115: {
116: return TrinaryLogic::createNo();
117: }
118:
119: public function isTrue(): TrinaryLogic
120: {
121: return TrinaryLogic::createNo();
122: }
123:
124: public function isFalse(): TrinaryLogic
125: {
126: return TrinaryLogic::createNo();
127: }
128:
129: public function isBoolean(): TrinaryLogic
130: {
131: return TrinaryLogic::createNo();
132: }
133:
134: public function isFloat(): TrinaryLogic
135: {
136: return TrinaryLogic::createNo();
137: }
138:
139: public function isInteger(): TrinaryLogic
140: {
141: return TrinaryLogic::createYes();
142: }
143:
144: public function isScalar(): TrinaryLogic
145: {
146: return TrinaryLogic::createYes();
147: }
148:
149: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
150: {
151: if ($type->isArray()->yes()) {
152: return new ConstantBooleanType(false);
153: }
154:
155: if (
156: $phpVersion->nonNumericStringAndIntegerIsFalseOnLooseComparison()
157: && $type->isString()->yes()
158: && $type->isNumericString()->no()
159: ) {
160: return new ConstantBooleanType(false);
161: }
162:
163: return new BooleanType();
164: }
165:
166: public function tryRemove(Type $typeToRemove): ?Type
167: {
168: if ($typeToRemove instanceof IntegerRangeType || $typeToRemove instanceof ConstantIntegerType) {
169: if ($typeToRemove instanceof IntegerRangeType) {
170: $removeValueMin = $typeToRemove->getMin();
171: $removeValueMax = $typeToRemove->getMax();
172: } else {
173: $removeValueMin = $typeToRemove->getValue();
174: $removeValueMax = $typeToRemove->getValue();
175: }
176: $lowerPart = $removeValueMin !== null ? IntegerRangeType::fromInterval(null, $removeValueMin, -1) : null;
177: $upperPart = $removeValueMax !== null ? IntegerRangeType::fromInterval($removeValueMax, null, +1) : null;
178: if ($lowerPart !== null && $upperPart !== null) {
179: return new UnionType([$lowerPart, $upperPart]);
180: }
181: return $lowerPart ?? $upperPart ?? new NeverType();
182: }
183:
184: return null;
185: }
186:
187: public function getFiniteTypes(): array
188: {
189: return [];
190: }
191:
192: public function exponentiate(Type $exponent): Type
193: {
194: return ExponentiateHelper::exponentiate($this, $exponent);
195: }
196:
197: public function toPhpDocNode(): TypeNode
198: {
199: return new IdentifierTypeNode('int');
200: }
201:
202: }
203: