diff --git a/src/Collection.php b/src/Collection.php index c0977548..36e27048 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -74,10 +74,13 @@ public function concat(self $other) : self /** * Merges several heterogenous collections. + * + * @param static[] $collections + * @return static */ - public static function merge(Collection ...$collections) : Collection + public static function merge(self ...$collections) : self { - $merged = Collection::empty(); + $merged = static::make(); foreach ($collections as $collection) { $merged = $merged->concat($collection); diff --git a/src/TypedCollection.php b/src/TypedCollection.php index 51900033..4c025d57 100644 --- a/src/TypedCollection.php +++ b/src/TypedCollection.php @@ -11,7 +11,10 @@ abstract class TypedCollection extends Collection protected function __construct(?array $data) { Assert::notEmpty($this->class); - Assert::allIsInstanceOf($data, $this->class); + + if ($data) { + Assert::allIsInstanceOf($data, $this->class); + } parent::__construct($data); } diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index b4eef7a3..ba780052 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -182,7 +182,7 @@ public function testAddTypedCollectionDoesntAllowHeteroTypes() : void $col->add(1); } - public function testMergeReturnsBaseCollection() : void + public function testMergeReturnsRightCollection() : void { $col1 = DummyCollection::make( [ @@ -199,7 +199,7 @@ public function testMergeReturnsBaseCollection() : void $col = DummyCollection::merge($col1, $col2); - $this->assertEquals(Collection::class, get_class($col)); + $this->assertEquals(DummyCollection::class, get_class($col)); $this->assertCount(3, $col); }