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