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