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