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: | |
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: | |
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: | [], |
92: | TrinaryLogic::createYes(), |
93: | ); |
94: | } |
95: | |
96: | public function toArrayKey(): Type |
97: | { |
98: | return $this; |
99: | } |
100: | |
101: | public function isOffsetAccessLegal(): TrinaryLogic |
102: | { |
103: | return TrinaryLogic::createYes(); |
104: | } |
105: | |
106: | public function isNull(): TrinaryLogic |
107: | { |
108: | return TrinaryLogic::createNo(); |
109: | } |
110: | |
111: | public function isTrue(): TrinaryLogic |
112: | { |
113: | return TrinaryLogic::createNo(); |
114: | } |
115: | |
116: | public function isFalse(): TrinaryLogic |
117: | { |
118: | return TrinaryLogic::createNo(); |
119: | } |
120: | |
121: | public function isBoolean(): TrinaryLogic |
122: | { |
123: | return TrinaryLogic::createNo(); |
124: | } |
125: | |
126: | public function isFloat(): TrinaryLogic |
127: | { |
128: | return TrinaryLogic::createNo(); |
129: | } |
130: | |
131: | public function isInteger(): TrinaryLogic |
132: | { |
133: | return TrinaryLogic::createYes(); |
134: | } |
135: | |
136: | public function isScalar(): TrinaryLogic |
137: | { |
138: | return TrinaryLogic::createYes(); |
139: | } |
140: | |
141: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
142: | { |
143: | if ($type->isArray()->yes()) { |
144: | return new ConstantBooleanType(false); |
145: | } |
146: | |
147: | if ( |
148: | $phpVersion->nonNumericStringAndIntegerIsFalseOnLooseComparison() |
149: | && $type->isString()->yes() |
150: | && $type->isNumericString()->no() |
151: | ) { |
152: | return new ConstantBooleanType(false); |
153: | } |
154: | |
155: | return new BooleanType(); |
156: | } |
157: | |
158: | public function tryRemove(Type $typeToRemove): ?Type |
159: | { |
160: | if ($typeToRemove instanceof IntegerRangeType || $typeToRemove instanceof ConstantIntegerType) { |
161: | if ($typeToRemove instanceof IntegerRangeType) { |
162: | $removeValueMin = $typeToRemove->getMin(); |
163: | $removeValueMax = $typeToRemove->getMax(); |
164: | } else { |
165: | $removeValueMin = $typeToRemove->getValue(); |
166: | $removeValueMax = $typeToRemove->getValue(); |
167: | } |
168: | $lowerPart = $removeValueMin !== null ? IntegerRangeType::fromInterval(null, $removeValueMin, -1) : null; |
169: | $upperPart = $removeValueMax !== null ? IntegerRangeType::fromInterval($removeValueMax, null, +1) : null; |
170: | if ($lowerPart !== null && $upperPart !== null) { |
171: | return new UnionType([$lowerPart, $upperPart]); |
172: | } |
173: | return $lowerPart ?? $upperPart ?? new NeverType(); |
174: | } |
175: | |
176: | return null; |
177: | } |
178: | |
179: | public function getFiniteTypes(): array |
180: | { |
181: | return []; |
182: | } |
183: | |
184: | public function exponentiate(Type $exponent): Type |
185: | { |
186: | return ExponentiateHelper::exponentiate($this, $exponent); |
187: | } |
188: | |
189: | public function toPhpDocNode(): TypeNode |
190: | { |
191: | return new IdentifierTypeNode('int'); |
192: | } |
193: | |
194: | } |
195: | |