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