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