Skip to content

Commit

Permalink
Change Collection#slice return type to Collection
Browse files Browse the repository at this point in the history
This commit should resolve doctrine#195
  • Loading branch information
LIQRGV committed Jan 14, 2021
1 parent 8e8233f commit a0c0960
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Collections/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public function clear() : void
*/
public function slice(int $offset, ?int $length = null) : array
{
return array_slice($this->elements, $offset, $length, true);
return $this->createFrom(array_slice($this->elements, $offset, $length, true));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function indexOf($element);
*
* @return array
*
* @psalm-return array<TKey,T>
* @psalm-return Collection<TKey,T>
*/
public function slice(int $offset, ?int $length = null) : array;
}
10 changes: 6 additions & 4 deletions tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Doctrine\Tests\Common\Collections;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\Constraint\IsType;
use PHPUnit\Framework\TestCase;
use stdClass;
use function count;
Expand Down Expand Up @@ -205,14 +207,14 @@ public function testSlice() : void
$this->collection[] = 'three';

$slice = $this->collection->slice(0, 1);
self::assertIsArray($slice);
self::assertEquals(['one'], $slice);
self::assertInstanceOf(Collection::class, $slice);
self::assertEquals(new ArrayCollection(['one']), $slice);

$slice = $this->collection->slice(1);
self::assertEquals([1 => 'two', 2 => 'three'], $slice);
self::assertEquals(new ArrayCollection([1 => 'two', 2 => 'three']), $slice);

$slice = $this->collection->slice(1, 1);
self::assertEquals([1 => 'two'], $slice);
self::assertEquals(new ArrayCollection([1 => 'two']), $slice);
}

protected function fillMatchingFixture() : void
Expand Down

0 comments on commit a0c0960

Please sign in to comment.