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\ConstantIntegerType;
14: use PHPStan\Type\Traits\NonArrayTypeTrait;
15: use PHPStan\Type\Traits\NonCallableTypeTrait;
16: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
17: use PHPStan\Type\Traits\NonGenericTypeTrait;
18: use PHPStan\Type\Traits\NonIterableTypeTrait;
19: use PHPStan\Type\Traits\NonObjectTypeTrait;
20: use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait;
21: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
22: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
23:
24: /** @api */
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 toAbsoluteNumber(): Type
60: {
61: return IntegerRangeType::createAllGreaterThanOrEqualTo(0);
62: }
63:
64: public function toFloat(): Type
65: {
66: return new FloatType();
67: }
68:
69: public function toInteger(): Type
70: {
71: return $this;
72: }
73:
74: public function toString(): Type
75: {
76: return new IntersectionType([
77: new StringType(),
78: new AccessoryLowercaseStringType(),
79: new AccessoryUppercaseStringType(),
80: new AccessoryNumericStringType(),
81: ]);
82: }
83:
84: public function toArray(): Type
85: {
86: return new ConstantArrayType(
87: [new ConstantIntegerType(0)],
88: [$this],
89: [1],
90: [],
91: TrinaryLogic::createYes(),
92: );
93: }
94:
95: public function toArrayKey(): Type
96: {
97: return $this;
98: }
99:
100: public function isOffsetAccessLegal(): TrinaryLogic
101: {
102: return TrinaryLogic::createYes();
103: }
104:
105: public function isNull(): TrinaryLogic
106: {
107: return TrinaryLogic::createNo();
108: }
109:
110: public function isTrue(): TrinaryLogic
111: {
112: return TrinaryLogic::createNo();
113: }
114:
115: public function isFalse(): TrinaryLogic
116: {
117: return TrinaryLogic::createNo();
118: }
119:
120: public function isBoolean(): TrinaryLogic
121: {
122: return TrinaryLogic::createNo();
123: }
124:
125: public function isFloat(): TrinaryLogic
126: {
127: return TrinaryLogic::createNo();
128: }
129:
130: public function isInteger(): TrinaryLogic
131: {
132: return TrinaryLogic::createYes();
133: }
134:
135: public function isScalar(): TrinaryLogic
136: {
137: return TrinaryLogic::createYes();
138: }
139:
140: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
141: {
142: return new BooleanType();
143: }
144:
145: public function tryRemove(Type $typeToRemove): ?Type
146: {
147: if ($typeToRemove instanceof IntegerRangeType || $typeToRemove instanceof ConstantIntegerType) {
148: if ($typeToRemove instanceof IntegerRangeType) {
149: $removeValueMin = $typeToRemove->getMin();
150: $removeValueMax = $typeToRemove->getMax();
151: } else {
152: $removeValueMin = $typeToRemove->getValue();
153: $removeValueMax = $typeToRemove->getValue();
154: }
155: $lowerPart = $removeValueMin !== null ? IntegerRangeType::fromInterval(null, $removeValueMin, -1) : null;
156: $upperPart = $removeValueMax !== null ? IntegerRangeType::fromInterval($removeValueMax, null, +1) : null;
157: if ($lowerPart !== null && $upperPart !== null) {
158: return new UnionType([$lowerPart, $upperPart]);
159: }
160: return $lowerPart ?? $upperPart ?? new NeverType();
161: }
162:
163: return null;
164: }
165:
166: public function getFiniteTypes(): array
167: {
168: return [];
169: }
170:
171: public function exponentiate(Type $exponent): Type
172: {
173: return ExponentiateHelper::exponentiate($this, $exponent);
174: }
175:
176: public function toPhpDocNode(): TypeNode
177: {
178: return new IdentifierTypeNode('int');
179: }
180:
181: }
182: