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 getValue(): int
45: {
46: return $this->value;
47: }
48:
49: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
50: {
51: if ($type instanceof self) {
52: return $this->value === $type->value ? IsSuperTypeOfResult::createYes() : IsSuperTypeOfResult::createNo();
53: }
54:
55: if ($type instanceof IntegerRangeType) {
56: $min = $type->getMin();
57: $max = $type->getMax();
58: if (($min === null || $min <= $this->value) && ($max === null || $this->value <= $max)) {
59: return IsSuperTypeOfResult::createMaybe();
60: }
61:
62: return IsSuperTypeOfResult::createNo();
63: }
64:
65: if ($type instanceof parent) {
66: return IsSuperTypeOfResult::createMaybe();
67: }
68:
69: if ($type instanceof CompoundType) {
70: return $type->isSubTypeOf($this);
71: }
72:
73: return IsSuperTypeOfResult::createNo();
74: }
75:
76: public function describe(VerbosityLevel $level): string
77: {
78: // what VerbosityLevel::handle() would pick, without creating its callbacks
79: return $level->isTypeOnly() ? 'int' : (string) $this->value;
80: }
81:
82: public function toFloat(): Type
83: {
84: return new ConstantFloatType($this->value);
85: }
86:
87: public function toBitwiseNotType(): Type
88: {
89: return new self(~$this->value);
90: }
91:
92: public function toAbsoluteNumber(): Type
93: {
94: if ($this->value === PHP_INT_MIN) {
95: // The absolute value of the smallest integer is not representable as an int.
96: // Checking is_int(abs($this->value)) instead is dead code to PHPStan itself,
97: // which infers abs(int) as int<0, max>.
98: return new ConstantFloatType(-(float) $this->value);
99: }
100:
101: return new self(abs($this->value));
102: }
103:
104: public function toString(): Type
105: {
106: return new ConstantStringType((string) $this->value);
107: }
108:
109: public function toArrayKey(): Type
110: {
111: return $this;
112: }
113:
114: public function toCoercedArgumentType(bool $strictTypes): Type
115: {
116: if (!$strictTypes) {
117: return TypeCombinator::union($this, $this->toFloat(), $this->toString(), $this->toBoolean());
118: }
119:
120: return TypeCombinator::union($this, $this->toFloat());
121: }
122:
123: public function generalize(GeneralizePrecision $precision): Type
124: {
125: return new IntegerType();
126: }
127:
128: /**
129: * @return ConstTypeNode
130: */
131: public function toPhpDocNode(): TypeNode
132: {
133: return new ConstTypeNode(new ConstExprIntegerNode((string) $this->value));
134: }
135:
136: }
137: