1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
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\Constant\ConstantArrayType;
10: use PHPStan\Type\Constant\ConstantBooleanType;
11: use PHPStan\Type\Constant\ConstantFloatType;
12: use PHPStan\Type\Constant\ConstantIntegerType;
13: use PHPStan\Type\Constant\ConstantStringType;
14: use PHPStan\Type\Traits\NonArrayTypeTrait;
15: use PHPStan\Type\Traits\NonCallableTypeTrait;
16: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
17: use PHPStan\Type\Traits\NonGenericTypeTrait;
18: use PHPStan\Type\Traits\NonIterableTypeTrait;
19: use PHPStan\Type\Traits\NonObjectTypeTrait;
20: use PHPStan\Type\Traits\NonOffsetAccessibleTypeTrait;
21: use PHPStan\Type\Traits\UndecidedBooleanTypeTrait;
22: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
23:
24: /** @api */
25: class BooleanType implements Type
26: {
27:
28: use JustNullableTypeTrait;
29: use NonArrayTypeTrait;
30: use NonCallableTypeTrait;
31: use NonIterableTypeTrait;
32: use NonObjectTypeTrait;
33: use UndecidedBooleanTypeTrait;
34: use UndecidedComparisonTypeTrait;
35: use NonGenericTypeTrait;
36: use NonOffsetAccessibleTypeTrait;
37: use NonGeneralizableTypeTrait;
38:
39: /** @api */
40: public function __construct()
41: {
42: }
43:
44: public function getConstantStrings(): array
45: {
46: return [];
47: }
48:
49: public function describe(VerbosityLevel $level): string
50: {
51: return 'bool';
52: }
53:
54: public function toNumber(): Type
55: {
56: return $this->toInteger();
57: }
58:
59: public function toString(): Type
60: {
61: return TypeCombinator::union(
62: new ConstantStringType(''),
63: new ConstantStringType('1'),
64: );
65: }
66:
67: public function toInteger(): Type
68: {
69: return TypeCombinator::union(
70: new ConstantIntegerType(0),
71: new ConstantIntegerType(1),
72: );
73: }
74:
75: public function toFloat(): Type
76: {
77: return TypeCombinator::union(
78: new ConstantFloatType(0.0),
79: new ConstantFloatType(1.0),
80: );
81: }
82:
83: public function toArray(): Type
84: {
85: return new ConstantArrayType(
86: [new ConstantIntegerType(0)],
87: [$this],
88: [1],
89: [],
90: TrinaryLogic::createYes(),
91: );
92: }
93:
94: public function toArrayKey(): Type
95: {
96: return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(1)]);
97: }
98:
99: public function isOffsetAccessLegal(): TrinaryLogic
100: {
101: return TrinaryLogic::createYes();
102: }
103:
104: public function isNull(): TrinaryLogic
105: {
106: return TrinaryLogic::createNo();
107: }
108:
109: public function isTrue(): TrinaryLogic
110: {
111: return TrinaryLogic::createMaybe();
112: }
113:
114: public function isFalse(): TrinaryLogic
115: {
116: return TrinaryLogic::createMaybe();
117: }
118:
119: public function isBoolean(): TrinaryLogic
120: {
121: return TrinaryLogic::createYes();
122: }
123:
124: public function isScalar(): TrinaryLogic
125: {
126: return TrinaryLogic::createYes();
127: }
128:
129: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
130: {
131: return new BooleanType();
132: }
133:
134: public function tryRemove(Type $typeToRemove): ?Type
135: {
136: if ($typeToRemove instanceof ConstantBooleanType) {
137: return new ConstantBooleanType(!$typeToRemove->getValue());
138: }
139:
140: return null;
141: }
142:
143: public function getFiniteTypes(): array
144: {
145: return [
146: new ConstantBooleanType(true),
147: new ConstantBooleanType(false),
148: ];
149: }
150:
151: public function exponentiate(Type $exponent): Type
152: {
153: return ExponentiateHelper::exponentiate($this, $exponent);
154: }
155:
156: public function toPhpDocNode(): TypeNode
157: {
158: return new IdentifierTypeNode('bool');
159: }
160:
161: /**
162: * @param mixed[] $properties
163: */
164: public static function __set_state(array $properties): Type
165: {
166: return new self();
167: }
168:
169: }
170: