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