Skip to content

Commit 0e714c2

Browse files
authored
Merge pull request #149 from IonBazan/feature/php8.2-types
Add support for standalone `true`, `false` and `null` types
2 parents 6dbe516 + 764d869 commit 0e714c2

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/Generator/TypeGenerator/AtomicType.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ final class AtomicType
4242
'mixed' => 10,
4343
'void' => 11,
4444
'false' => 12,
45-
'null' => 13,
46-
'never' => 14,
45+
'true' => 13,
46+
'null' => 14,
47+
'never' => 15,
4748
];
4849

4950
/** @psalm-var array<non-empty-string, null> */
5051
private const NOT_NULLABLE_TYPES = [
5152
'null' => null,
5253
'false' => null,
54+
'true' => null,
5355
'void' => null,
5456
'mixed' => null,
5557
'never' => null,
@@ -151,6 +153,17 @@ public function assertCanUnionWith(array $others): void
151153
$other->type
152154
));
153155
}
156+
157+
if (
158+
('true' === $other->type && 'false' === $this->type) ||
159+
('false' === $other->type && 'true' === $this->type)
160+
) {
161+
throw new InvalidArgumentException(sprintf(
162+
'Type "%s" cannot be composed in a union with type "%s"',
163+
$this->type,
164+
$other->type
165+
));
166+
}
154167
}
155168

156169
if (
@@ -222,6 +235,6 @@ public function assertCanBeStandaloneNullable(): void
222235

223236
private function requiresUnionWithStandaloneType(): bool
224237
{
225-
return 'null' === $this->type || 'false' === $this->type;
238+
return false;
226239
}
227240
}

test/Generator/TypeGeneratorTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,24 @@ public function validType()
186186
['null|foo', '\\foo|null'],
187187
['foo|bar|null', '\\bar|\\foo|null'],
188188

189-
// The `false` type can only be used in combination with other types
189+
// Standalone `false` type
190+
['false', 'false'],
190191
['foo|false', '\\foo|false'],
191192
['string|false', 'string|false'],
192193
['string|false|null', 'string|false|null'],
193194

194195
// `false` + `null` requires a third type
195196
['Foo|false|null', '\\Foo|false|null'],
196197

198+
// The `true` type
199+
['foo|true', '\\foo|true'],
200+
['string|true', 'string|true'],
201+
['true', 'true'],
202+
['true|null', 'true|null'],
203+
204+
// Standalone `null` type
205+
['null', 'null'],
206+
197207
// The `static` type should not be turned into a FQCN
198208
['static', 'static'],
199209
['?static', '?static'],
@@ -398,14 +408,11 @@ public function invalidType()
398408
['never&null'],
399409
['never&Foo'],
400410
['never&\\foo'],
401-
402-
// `false` and `null` must always be used as part of a union type
403-
['null'],
404-
['false'],
405411
['?null'],
406-
['?false'],
407-
['false|null'],
408-
['null|false'],
412+
413+
// `false` and `true` cannot be used together
414+
['true|false'],
415+
['false|true'],
409416

410417
// Duplicate types are rejected
411418
['A|A'],

0 commit comments

Comments
 (0)