1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Constant;
4:
5: use PHPStan\TrinaryLogic;
6: use PHPStan\Type\BooleanType;
7: use PHPStan\Type\ConstantScalarType;
8: use PHPStan\Type\GeneralizePrecision;
9: use PHPStan\Type\MixedType;
10: use PHPStan\Type\NeverType;
11: use PHPStan\Type\StaticTypeFactory;
12: use PHPStan\Type\Traits\ConstantScalarTypeTrait;
13: use PHPStan\Type\Type;
14: use PHPStan\Type\VerbosityLevel;
15:
16: /** @api */
17: class ConstantBooleanType extends BooleanType implements ConstantScalarType
18: {
19:
20: use ConstantScalarTypeTrait;
21:
22: /** @api */
23: public function __construct(private bool $value)
24: {
25: parent::__construct();
26: }
27:
28: public function getValue(): bool
29: {
30: return $this->value;
31: }
32:
33: public function describe(VerbosityLevel $level): string
34: {
35: return $this->value ? 'true' : 'false';
36: }
37:
38: public function getSmallerType(): Type
39: {
40: if ($this->value) {
41: return StaticTypeFactory::falsey();
42: }
43: return new NeverType();
44: }
45:
46: public function getSmallerOrEqualType(): Type
47: {
48: if ($this->value) {
49: return new MixedType();
50: }
51: return StaticTypeFactory::falsey();
52: }
53:
54: public function getGreaterType(): Type
55: {
56: if ($this->value) {
57: return new NeverType();
58: }
59: return StaticTypeFactory::truthy();
60: }
61:
62: public function getGreaterOrEqualType(): Type
63: {
64: if ($this->value) {
65: return StaticTypeFactory::truthy();
66: }
67: return new MixedType();
68: }
69:
70: public function toBoolean(): BooleanType
71: {
72: return $this;
73: }
74:
75: public function toNumber(): Type
76: {
77: return new ConstantIntegerType((int) $this->value);
78: }
79:
80: public function toString(): Type
81: {
82: return new ConstantStringType((string) $this->value);
83: }
84:
85: public function toInteger(): Type
86: {
87: return new ConstantIntegerType((int) $this->value);
88: }
89:
90: public function toFloat(): Type
91: {
92: return new ConstantFloatType((float) $this->value);
93: }
94:
95: public function toArrayKey(): Type
96: {
97: return new ConstantIntegerType((int) $this->value);
98: }
99:
100: public function isTrue(): TrinaryLogic
101: {
102: return TrinaryLogic::createFromBoolean($this->value === true);
103: }
104:
105: public function isFalse(): TrinaryLogic
106: {
107: return TrinaryLogic::createFromBoolean($this->value === false);
108: }
109:
110: public function generalize(GeneralizePrecision $precision): Type
111: {
112: return new BooleanType();
113: }
114:
115: /**
116: * @param mixed[] $properties
117: */
118: public static function __set_state(array $properties): Type
119: {
120: return new self($properties['value']);
121: }
122:
123: }
124: