diff --git a/lib/Doctrine/Common/Collections/ArrayCollection.php b/lib/Doctrine/Common/Collections/ArrayCollection.php index 46a35667d..3f9bd80fa 100644 --- a/lib/Doctrine/Common/Collections/ArrayCollection.php +++ b/lib/Doctrine/Common/Collections/ArrayCollection.php @@ -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)); } /** diff --git a/lib/Doctrine/Common/Collections/Collection.php b/lib/Doctrine/Common/Collections/Collection.php index f435448ac..3f356bdf1 100644 --- a/lib/Doctrine/Common/Collections/Collection.php +++ b/lib/Doctrine/Common/Collections/Collection.php @@ -289,7 +289,7 @@ public function indexOf($element); * * @return array * - * @psalm-return array + * @psalm-return Collection */ public function slice(int $offset, ?int $length = null) : array; } diff --git a/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php b/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php index d563ade5e..f873f3e05 100644 --- a/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php +++ b/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php @@ -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; @@ -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