@@ -93,7 +93,7 @@ public function mapTypes(callable $callable): self
9393 {
9494 $ assertTagsCallback = static fn (AssertTag $ tag ): AssertTag => $ tag ->withType ($ callable ($ tag ->getType ()));
9595
96- return new self (array_map ($ assertTagsCallback , $ this ->asserts ));
96+ return self :: create (array_map ($ assertTagsCallback , $ this ->asserts ));
9797 }
9898
9999 /**
@@ -106,20 +106,27 @@ public function intersectWith(Assertions $other): self
106106
107107 public function union (Assertions $ other ): self
108108 {
109- return new self (array_merge ($ this ->getAll (), $ other ->getAll ()));
109+ if ($ this === self ::$ empty ) {
110+ return $ other ;
111+ }
112+ if ($ other === self ::$ empty ) {
113+ return $ this ;
114+ }
115+
116+ return self ::create (array_merge ($ this ->getAll (), $ other ->getAll ()));
110117 }
111118
112119 public function intersect (Assertions $ other ): self
113120 {
114- $ otherAsserts = $ other ->getAll ();
115- if (count ($ otherAsserts ) === 0 ) {
121+ if ($ this === self ::$ empty ) {
122+ return $ other ;
123+ }
124+ if ($ other === self ::$ empty ) {
116125 return $ this ;
117126 }
118127
128+ $ otherAsserts = $ other ->getAll ();
119129 $ thisAsserts = $ this ->getAll ();
120- if (count ($ thisAsserts ) === 0 ) {
121- return $ other ;
122- }
123130
124131 $ merged = [];
125132 foreach ($ thisAsserts as $ thisAssert ) {
@@ -134,7 +141,7 @@ public function intersect(Assertions $other): self
134141 }
135142 }
136143
137- return new self ($ merged );
144+ return self :: create ($ merged );
138145 }
139146
140147 private static function getAssertKey (AssertTag $ assert ): string
@@ -147,6 +154,17 @@ private static function getAssertKey(AssertTag $assert): string
147154 );
148155 }
149156
157+ /**
158+ * @param AssertTag[] $asserts
159+ */
160+ private static function create (array $ asserts ): self
161+ {
162+ if (count ($ asserts ) === 0 ) {
163+ return self ::createEmpty ();
164+ }
165+ return new self ($ asserts );
166+ }
167+
150168 public static function createEmpty (): self
151169 {
152170 $ empty = self ::$ empty ;
@@ -164,11 +182,8 @@ public static function createEmpty(): self
164182 public static function createFromResolvedPhpDocBlock (ResolvedPhpDocBlock $ phpDocBlock ): self
165183 {
166184 $ tags = $ phpDocBlock ->getAssertTags ();
167- if (count ($ tags ) === 0 ) {
168- return self ::createEmpty ();
169- }
170185
171- return new self ($ tags );
186+ return self :: create ($ tags );
172187 }
173188
174189}
0 commit comments