1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Constant;
4:
5: use PhpParser\Node\Identifier;
6: use PHPStan\Php\PhpVersion;
7: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
9: use PHPStan\TrinaryLogic;
10: use PHPStan\Turbo\ShadowedByTurboExtension;
11: use PHPStan\Type\BooleanType;
12: use PHPStan\Type\ConstantScalarType;
13: use PHPStan\Type\ErrorType;
14: use PHPStan\Type\GeneralizePrecision;
15: use PHPStan\Type\InstanceofDeprecated;
16: use PHPStan\Type\MixedType;
17: use PHPStan\Type\NativeTypePosition;
18: use PHPStan\Type\NeverType;
19: use PHPStan\Type\StaticTypeFactory;
20: use PHPStan\Type\Traits\ConstantScalarTypeTrait;
21: use PHPStan\Type\Type;
22: use PHPStan\Type\TypeCombinator;
23: use PHPStan\Type\TypeKind;
24: use PHPStan\Type\ValueSet;
25: use PHPStan\Type\VerbosityLevel;
26:
27: /** @api */
28: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/ConstantBooleanType.cpp')]
29: #[InstanceofDeprecated(insteadUse: 'Type::isTrue() or Type::isFalse()')]
30: class ConstantBooleanType extends BooleanType implements ConstantScalarType
31: {
32:
33: use ConstantScalarTypeTrait {
34: looseCompare as private scalarLooseCompare;
35: }
36:
37: /** @api */
38: public function __construct(private bool $value)
39: {
40: parent::__construct();
41: }
42:
43: public function getValue(): bool
44: {
45: return $this->value;
46: }
47:
48: public function describe(VerbosityLevel $level): string
49: {
50: return $this->value ? 'true' : 'false';
51: }
52:
53: public function getSmallerType(PhpVersion $phpVersion): Type
54: {
55: if ($this->value) {
56: return StaticTypeFactory::falsey();
57: }
58: return new NeverType();
59: }
60:
61: public function getSmallerOrEqualType(PhpVersion $phpVersion): Type
62: {
63: if ($this->value) {
64: return new MixedType();
65: }
66: return StaticTypeFactory::falsey();
67: }
68:
69: public function getGreaterType(PhpVersion $phpVersion): Type
70: {
71: if ($this->value) {
72: return new NeverType();
73: }
74: return StaticTypeFactory::truthy();
75: }
76:
77: public function getGreaterOrEqualType(PhpVersion $phpVersion): Type
78: {
79: if ($this->value) {
80: return StaticTypeFactory::truthy();
81: }
82: return new MixedType();
83: }
84:
85: public function toBoolean(): BooleanType
86: {
87: return $this;
88: }
89:
90: public function toNumber(): Type
91: {
92: return new ConstantIntegerType((int) $this->value);
93: }
94:
95: public function toBitwiseNotType(): Type
96: {
97: return new ErrorType();
98: }
99:
100: public function toAbsoluteNumber(): Type
101: {
102: return $this->toNumber()->toAbsoluteNumber();
103: }
104:
105: public function toString(): Type
106: {
107: return new ConstantStringType((string) $this->value);
108: }
109:
110: public function toInteger(): Type
111: {
112: return new ConstantIntegerType((int) $this->value);
113: }
114:
115: public function toFloat(): Type
116: {
117: return new ConstantFloatType((float) $this->value);
118: }
119:
120: public function toArrayKey(): Type
121: {
122: return new ConstantIntegerType((int) $this->value);
123: }
124:
125: public function toCoercedArgumentType(bool $strictTypes): Type
126: {
127: if (!$strictTypes) {
128: return TypeCombinator::union($this->toInteger(), $this->toFloat(), $this->toString(), $this);
129: }
130:
131: return $this;
132: }
133:
134: public function getKindMask(): int
135: {
136: return $this->value ? TypeKind::TRUE : TypeKind::FALSE;
137: }
138:
139: public function isTrue(): TrinaryLogic
140: {
141: return TrinaryLogic::createFromBoolean($this->value === true);
142: }
143:
144: public function isFalse(): TrinaryLogic
145: {
146: return TrinaryLogic::createFromBoolean($this->value === false);
147: }
148:
149: public function generalize(GeneralizePrecision $precision): Type
150: {
151: return new BooleanType();
152: }
153:
154: public function toPhpDocNode(): TypeNode
155: {
156: return new IdentifierTypeNode($this->value ? 'true' : 'false');
157: }
158:
159: public function toNativePhpTypeNode(NativeTypePosition $position)
160: {
161: if ($this->value ? $position->isTrueAllowed() : $position->isFalseAllowed()) {
162: return new Identifier($this->value ? 'true' : 'false');
163: }
164:
165: return parent::toNativePhpTypeNode($position);
166: }
167:
168: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
169: {
170: if ($type->isObject()->yes()) {
171: return $this;
172: }
173:
174: return $this->scalarLooseCompare($type, $phpVersion);
175: }
176:
177: public function toValueSet(): ValueSet
178: {
179: return ValueSet::createBooleans(!$this->getValue(), $this->getValue());
180: }
181:
182: }
183: