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