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