Skip to content

Commit

Permalink
Collection::merge() returns inherited collection
Browse files Browse the repository at this point in the history
  • Loading branch information
kapxapot committed May 1, 2020
1 parent 3a696e5 commit 11e32b3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/TypedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function testAddTypedCollectionDoesntAllowHeteroTypes() : void
$col->add(1);
}

public function testMergeReturnsBaseCollection() : void
public function testMergeReturnsRightCollection() : void
{
$col1 = DummyCollection::make(
[
Expand All @@ -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);
}

Expand Down

0 comments on commit 11e32b3

Please sign in to comment.