1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Constant;
4:
5: use PHPStan\TrinaryLogic;
6: use PHPStan\Type\CompoundType;
7: use PHPStan\Type\ConstantScalarType;
8: use PHPStan\Type\GeneralizePrecision;
9: use PHPStan\Type\IntegerRangeType;
10: use PHPStan\Type\IntegerType;
11: use PHPStan\Type\Traits\ConstantNumericComparisonTypeTrait;
12: use PHPStan\Type\Traits\ConstantScalarTypeTrait;
13: use PHPStan\Type\Type;
14: use PHPStan\Type\VerbosityLevel;
15: use function sprintf;
16:
17: /** @api */
18: class ConstantIntegerType extends IntegerType implements ConstantScalarType
19: {
20:
21: use ConstantScalarTypeTrait;
22: use ConstantScalarToBooleanTrait;
23: use ConstantNumericComparisonTypeTrait;
24:
25: /** @api */
26: public function __construct(private int $value)
27: {
28: parent::__construct();
29: }
30:
31: public function getValue(): int
32: {
33: return $this->value;
34: }
35:
36: public function isSuperTypeOf(Type $type): TrinaryLogic
37: {
38: if ($type instanceof self) {
39: return $this->value === $type->value ? TrinaryLogic::createYes() : TrinaryLogic::createNo();
40: }
41:
42: if ($type instanceof IntegerRangeType) {
43: $min = $type->getMin();
44: $max = $type->getMax();
45: if (($min === null || $min <= $this->value) && ($max === null || $this->value <= $max)) {
46: return TrinaryLogic::createMaybe();
47: }
48:
49: return TrinaryLogic::createNo();
50: }
51:
52: if ($type instanceof parent) {
53: return TrinaryLogic::createMaybe();
54: }
55:
56: if ($type instanceof CompoundType) {
57: return $type->isSubTypeOf($this);
58: }
59:
60: return TrinaryLogic::createNo();
61: }
62:
63: public function describe(VerbosityLevel $level): string
64: {
65: return $level->handle(
66: static fn (): string => 'int',
67: fn (): string => sprintf('%s', $this->value),
68: );
69: }
70:
71: public function toFloat(): Type
72: {
73: return new ConstantFloatType($this->value);
74: }
75:
76: public function toString(): Type
77: {
78: return new ConstantStringType((string) $this->value);
79: }
80:
81: public function toArrayKey(): Type
82: {
83: return $this;
84: }
85:
86: public function generalize(GeneralizePrecision $precision): Type
87: {
88: return new IntegerType();
89: }
90:
91: /**
92: * @param mixed[] $properties
93: */
94: public static function __set_state(array $properties): Type
95: {
96: return new self($properties['value']);
97: }
98:
99: }
100: