@@ -33,6 +33,14 @@ final class UnionType implements TypeInterface
3333 private array $ members ;
3434
3535 private function __construct (TypeInterface ...$ members )
36+ {
37+ if (count ($ members ) < 1 ) {
38+ throw new \Exception ('UnionType can only hold more than one different members ' );
39+ }
40+ $ this ->members = $ members ;
41+ }
42+
43+ public static function of (TypeInterface ...$ members ): TypeInterface
3644 {
3745 $ uniqueMembers = [];
3846 foreach ($ members as $ member ) {
@@ -44,7 +52,12 @@ private function __construct(TypeInterface ...$members)
4452
4553 $ uniqueMembers [] = $ member ;
4654 }
47- $ this ->members = $ uniqueMembers ;
55+
56+ if (count ($ uniqueMembers ) === 1 ) {
57+ return $ uniqueMembers [0 ];
58+ }
59+
60+ return new self (...$ members );
4861 }
4962
5063 public function isNullable (): bool
@@ -72,17 +85,6 @@ public function withoutNullable(): TypeInterface
7285 return self ::of (...$ nonNullMembers );
7386 }
7487
75- public static function of (TypeInterface ...$ members ): TypeInterface
76- {
77- $ union = new self (...$ members );
78-
79- if (count ($ union ->members ) === 1 ) {
80- return $ union ->members [0 ];
81- }
82-
83- return new self (...$ members );
84- }
85-
8688 public function is (TypeInterface $ other ): bool
8789 {
8890 if ($ other instanceof UnionType) {
@@ -94,12 +96,12 @@ public function is(TypeInterface $other): bool
9496 break ;
9597 }
9698 }
97-
99+
98100 if (!$ match ) {
99101 return false ;
100102 }
101103 }
102-
104+
103105 return true ;
104106 } else {
105107 return false ;
0 commit comments