Skip to content

Commit 57ee20c

Browse files
committed
TASK: UnionType RequiresAtLeastOneMember
1 parent 1b476e5 commit 57ee20c

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

src/TypeSystem/Type/UnionType/UnionType.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,14 @@ final class UnionType implements TypeInterface, \IteratorAggregate, \Countable
3737

3838
private function __construct(TypeInterface ...$members)
3939
{
40-
if (count($members) < 1) {
41-
throw new \Exception('UnionType can only hold more than one different members');
42-
}
40+
assert(count($members) > 1, 'UnionType must hold at least two different members');
4341
$this->members = $members;
4442
}
4543

46-
public static function of(TypeInterface ...$members): TypeInterface
44+
public static function of(TypeInterface $firstMember, TypeInterface ...$members): TypeInterface
4745
{
4846
$uniqueMembers = [];
49-
foreach ($members as $member) {
47+
foreach ([$firstMember, ...$members] as $member) {
5048
foreach ($uniqueMembers as $uniqueMember) {
5149
if ($member->is($uniqueMember)) {
5250
continue 2;
@@ -82,9 +80,6 @@ public function withoutNull(): TypeInterface
8280
}
8381
$nonNullMembers[] = $member;
8482
}
85-
if (count($nonNullMembers) === 1) {
86-
return $nonNullMembers[0];
87-
}
8883
return self::of(...$nonNullMembers);
8984
}
9085

test/Unit/TypeSystem/Type/UnionType/UnionTypeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030

3131
final class UnionTypeTest extends TestCase
3232
{
33+
/**
34+
* @test
35+
*/
36+
public function unionRequiresAtLeastOneMember(): void
37+
{
38+
$this->expectException(\TypeError::class);
39+
/** @phpstan-ignore-next-line */
40+
UnionType::of();
41+
}
42+
3343
/**
3444
* @test
3545
*/

0 commit comments

Comments
 (0)