1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
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\Constant\ConstantArrayType;
12: use PHPStan\Type\Constant\ConstantBooleanType;
13: use PHPStan\Type\Constant\ConstantFloatType;
14: use PHPStan\Type\Constant\ConstantIntegerType;
15: use PHPStan\Type\Constant\ConstantStringType;
16: use PHPStan\Type\Traits\NonArrayTypeTrait;
17: use PHPStan\Type\Traits\NonCallableTypeTrait;
18: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
19: use PHPStan\Type\Traits\NonGenericTypeTrait;
20: use PHPStan\Type\Traits\NonIterableTypeTrait;
21: use PHPStan\Type\Traits\NonObjectTypeTrait;
22: use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait;
23: use PHPStan\Type\Traits\OperatorTypeTrait;
24: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
25: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
26:
27: /** @api */
28: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/BooleanType.cpp')]
29: #[InstanceofDeprecated(insteadUse: 'Type::isBoolean()')]
30: class BooleanType implements Type
31: {
32:
33: use JustNullableTypeTrait;
34: use NonArrayTypeTrait;
35: use NonCallableTypeTrait;
36: use NonIterableTypeTrait;
37: use NonObjectTypeTrait;
38: use UndecidedBooleanTypeTrait;
39: use UndecidedComparisonTypeTrait;
40: use NonGenericTypeTrait;
41: use NonOffsetAccessibleTypeTrait;
42: use NonGeneralizableTypeTrait;
43: use OperatorTypeTrait;
44:
45: /** @api */
46: public function __construct()
47: {
48: }
49:
50: public function getConstantStrings(): array
51: {
52: return [];
53: }
54:
55: public function getConstantScalarTypes(): array
56: {
57: return [new ConstantBooleanType(true), new ConstantBooleanType(false)];
58: }
59:
60: public function getConstantScalarValues(): array
61: {
62: return [true, false];
63: }
64:
65: public function describe(VerbosityLevel $level): string
66: {
67: return 'bool';
68: }
69:
70: public function toNumber(): Type
71: {
72: return $this->toInteger();
73: }
74:
75: public function toBitwiseNotType(): Type
76: {
77: return new ErrorType();
78: }
79:
80: public function toAbsoluteNumber(): Type
81: {
82: return $this->toNumber()->toAbsoluteNumber();
83: }
84:
85: public function toString(): Type
86: {
87: return new UnionType([
88: new ConstantStringType(''),
89: new ConstantStringType('1'),
90: ]);
91: }
92:
93: public function toInteger(): Type
94: {
95: return new UnionType([
96: new ConstantIntegerType(0),
97: new ConstantIntegerType(1),
98: ]);
99: }
100:
101: public function toFloat(): Type
102: {
103: return new UnionType([
104: new ConstantFloatType(0.0),
105: new ConstantFloatType(1.0),
106: ]);
107: }
108:
109: public function toArray(): Type
110: {
111: return new ConstantArrayType(
112: [new ConstantIntegerType(0)],
113: [$this],
114: [1],
115: isList: TrinaryLogic::createYes(),
116: );
117: }
118:
119: public function toArrayKey(): Type
120: {
121: return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(1)]);
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 isOffsetAccessLegal(): TrinaryLogic
134: {
135: return TrinaryLogic::createYes();
136: }
137:
138: public function isNull(): TrinaryLogic
139: {
140: return TrinaryLogic::createNo();
141: }
142:
143: public function isTrue(): TrinaryLogic
144: {
145: return TrinaryLogic::createMaybe();
146: }
147:
148: public function isFalse(): TrinaryLogic
149: {
150: return TrinaryLogic::createMaybe();
151: }
152:
153: public function isBoolean(): TrinaryLogic
154: {
155: return TrinaryLogic::createYes();
156: }
157:
158: public function isScalar(): TrinaryLogic
159: {
160: return TrinaryLogic::createYes();
161: }
162:
163: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
164: {
165: return new BooleanType();
166: }
167:
168: public function tryRemove(Type $typeToRemove): ?Type
169: {
170: if ($typeToRemove instanceof ConstantBooleanType) {
171: return new ConstantBooleanType(!$typeToRemove->getValue());
172: }
173:
174: return null;
175: }
176:
177: public function getFiniteTypes(): array
178: {
179: return [
180: new ConstantBooleanType(true),
181: new ConstantBooleanType(false),
182: ];
183: }
184:
185: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
186: {
187: return $this->toValueSet()->arithmeticPower($exponent->toValueSet(), $phpVersion);
188: }
189:
190: public function toPhpDocNode(): TypeNode
191: {
192: return new IdentifierTypeNode('bool');
193: }
194:
195: public function toNativePhpTypeNode(NativeTypePosition $position)
196: {
197: return $position->isScalarAllowed() ? new Identifier('bool') : null;
198: }
199:
200: public function toTrinaryLogic(): TrinaryLogic
201: {
202: if ($this->isTrue()->yes()) {
203: return TrinaryLogic::createYes();
204: }
205: if ($this->isFalse()->yes()) {
206: return TrinaryLogic::createNo();
207: }
208:
209: return TrinaryLogic::createMaybe();
210: }
211:
212: public function hasTemplateOrLateResolvableType(): bool
213: {
214: return false;
215: }
216:
217: public function toValueSet(): ValueSet
218: {
219: return ValueSet::createBooleans(true, true);
220: }
221:
222: }
223: