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\AccessoryNumericStringType;
10: use PHPStan\Type\Constant\ConstantArrayType;
11: use PHPStan\Type\Constant\ConstantIntegerType;
12: use PHPStan\Type\Traits\NonArrayTypeTrait;
13: use PHPStan\Type\Traits\NonCallableTypeTrait;
14: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
15: use PHPStan\Type\Traits\NonGenericTypeTrait;
16: use PHPStan\Type\Traits\NonIterableTypeTrait;
17: use PHPStan\Type\Traits\NonObjectTypeTrait;
18: use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait;
19: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
20: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
21:
22: /** @api */
23: class IntegerType implements Type
24: {
25:
26: use JustNullableTypeTrait;
27: use NonArrayTypeTrait;
28: use NonCallableTypeTrait;
29: use NonIterableTypeTrait;
30: use NonObjectTypeTrait;
31: use UndecidedBooleanTypeTrait;
32: use UndecidedComparisonTypeTrait;
33: use NonGenericTypeTrait;
34: use NonOffsetAccessibleTypeTrait;
35: use NonGeneralizableTypeTrait;
36:
37: /** @api */
38: public function __construct()
39: {
40: }
41:
42: public function describe(VerbosityLevel $level): string
43: {
44: return 'int';
45: }
46:
47: public function getConstantStrings(): array
48: {
49: return [];
50: }
51:
52: /**
53: * @param mixed[] $properties
54: */
55: public static function __set_state(array $properties): Type
56: {
57: return new self();
58: }
59:
60: public function toNumber(): Type
61: {
62: return $this;
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 AccessoryNumericStringType(),
85: ]);
86: }
87:
88: public function toArray(): Type
89: {
90: return new ConstantArrayType(
91: [new ConstantIntegerType(0)],
92: [$this],
93: [1],
94: [],
95: TrinaryLogic::createYes(),
96: );
97: }
98:
99: public function toArrayKey(): Type
100: {
101: return $this;
102: }
103:
104: public function isOffsetAccessLegal(): TrinaryLogic
105: {
106: return TrinaryLogic::createYes();
107: }
108:
109: public function isNull(): TrinaryLogic
110: {
111: return TrinaryLogic::createNo();
112: }
113:
114: public function isTrue(): TrinaryLogic
115: {
116: return TrinaryLogic::createNo();
117: }
118:
119: public function isFalse(): TrinaryLogic
120: {
121: return TrinaryLogic::createNo();
122: }
123:
124: public function isBoolean(): TrinaryLogic
125: {
126: return TrinaryLogic::createNo();
127: }
128:
129: public function isFloat(): TrinaryLogic
130: {
131: return TrinaryLogic::createNo();
132: }
133:
134: public function isInteger(): TrinaryLogic
135: {
136: return TrinaryLogic::createYes();
137: }
138:
139: public function isScalar(): TrinaryLogic
140: {
141: return TrinaryLogic::createYes();
142: }
143:
144: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
145: {
146: return new BooleanType();
147: }
148:
149: public function tryRemove(Type $typeToRemove): ?Type
150: {
151: if ($typeToRemove instanceof IntegerRangeType || $typeToRemove instanceof ConstantIntegerType) {
152: if ($typeToRemove instanceof IntegerRangeType) {
153: $removeValueMin = $typeToRemove->getMin();
154: $removeValueMax = $typeToRemove->getMax();
155: } else {
156: $removeValueMin = $typeToRemove->getValue();
157: $removeValueMax = $typeToRemove->getValue();
158: }
159: $lowerPart = $removeValueMin !== null ? IntegerRangeType::fromInterval(null, $removeValueMin, -1) : null;
160: $upperPart = $removeValueMax !== null ? IntegerRangeType::fromInterval($removeValueMax, null, +1) : null;
161: if ($lowerPart !== null && $upperPart !== null) {
162: return new UnionType([$lowerPart, $upperPart]);
163: }
164: return $lowerPart ?? $upperPart ?? new NeverType();
165: }
166:
167: return null;
168: }
169:
170: public function getFiniteTypes(): array
171: {
172: return [];
173: }
174:
175: public function exponentiate(Type $exponent): Type
176: {
177: return ExponentiateHelper::exponentiate($this, $exponent);
178: }
179:
180: public function toPhpDocNode(): TypeNode
181: {
182: return new IdentifierTypeNode('int');
183: }
184:
185: }
186: