1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Constant;
4:
5: use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
6: use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
7: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
8: use PHPStan\Turbo\ShadowedByTurboExtension;
9: use PHPStan\Type\CompoundType;
10: use PHPStan\Type\ConstantScalarType;
11: use PHPStan\Type\GeneralizePrecision;
12: use PHPStan\Type\IntegerRangeType;
13: use PHPStan\Type\IntegerType;
14: use PHPStan\Type\IsSuperTypeOfResult;
15: use PHPStan\Type\NumberSet;
16: use PHPStan\Type\Traits\ConstantNumericComparisonTypeTrait;
17: use PHPStan\Type\Traits\ConstantScalarTypeTrait;
18: use PHPStan\Type\Type;
19: use PHPStan\Type\TypeCombinator;
20: use PHPStan\Type\VerbosityLevel;
21: use function abs;
22: use const PHP_INT_MIN;
23:
24: /** @api */
25: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/ConstantIntegerType.cpp')]
26: class ConstantIntegerType extends IntegerType implements ConstantScalarType
27: {
28:
29: use ConstantScalarTypeTrait;
30: use ConstantScalarToBooleanTrait;
31: use ConstantNumericComparisonTypeTrait;
32:
33: /** @api */
34: public function __construct(private int $value)
35: {
36: parent::__construct();
37: }
38:
39: public function toNumberSet(): NumberSet
40: {
41: return new NumberSet([[$this->value, $this->value]], [], false);
42: }
43:
44: public function toExactNumberSet(): ?NumberSet
45: {
46: return $this->toNumberSet();
47: }
48:
49: public function getValue(): int
50: {
51: return $this->value;
52: }
53:
54: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
55: {
56: if ($type instanceof self) {
57: return $this->value === $type->value ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createNo();
58: }
59:
60: if ($type instanceof IntegerRangeType) {
61: $min = $type->getMin();
62: $max = $type->getMax();
63: if (($min === null || $min <= $this->value) && ($max === null || $this->value <= $max)) {
64: return IsSuperTypeOfResult::createMaybe();
65: }
66:
67: return IsSuperTypeOfResult::createNo();
68: }
69:
70: if ($type instanceof parent) {
71: return IsSuperTypeOfResult::createMaybe();
72: }
73:
74: if ($type instanceof CompoundType) {
75: return $type->isSubTypeOf($this);
76: }
77:
78: return IsSuperTypeOfResult::createNo();
79: }
80:
81: public function describe(VerbosityLevel $level): string
82: {
83: // what VerbosityLevel::handle() would pick, without creating its callbacks
84: return $level->isTypeOnly() ? 'int' : (string) $this->value;
85: }
86:
87: public function toFloat(): Type
88: {
89: return new ConstantFloatType($this->value);
90: }
91:
92: public function toBitwiseNotType(): Type
93: {
94: return new self(~$this->value);
95: }
96:
97: public function toAbsoluteNumber(): Type
98: {
99: if ($this->value === PHP_INT_MIN) {
100: // The absolute value of the smallest integer is not representable as an int.
101: // Checking is_int(abs($this->value)) instead is dead code to PHPStan itself,
102: // which infers abs(int) as int<0, max>.
103: return new ConstantFloatType(-(float) $this->value);
104: }
105:
106: return new self(abs($this->value));
107: }
108:
109: public function toString(): Type
110: {
111: return new ConstantStringType((string) $this->value);
112: }
113:
114: public function toArrayKey(): Type
115: {
116: return $this;
117: }
118:
119: public function toCoercedArgumentType(bool $strictTypes): Type
120: {
121: if (!$strictTypes) {
122: return TypeCombinator::union($this, $this->toFloat(), $this->toString(), $this->toBoolean());
123: }
124:
125: return TypeCombinator::union($this, $this->toFloat());
126: }
127:
128: public function generalize(GeneralizePrecision $precision): Type
129: {
130: return new IntegerType();
131: }
132:
133: /**
134: * @return ConstTypeNode
135: */
136: public function toPhpDocNode(): TypeNode
137: {
138: return new ConstTypeNode(new ConstExprIntegerNode((string) $this->value));
139: }
140:
141: }
142: