Skip to content

Commit ab0fc76

Browse files
committed
Change Collection::slice() method to return an instance of Collection
instead of an array.
1 parent 8ad3946 commit ab0fc76

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

UPGRADING-2.0.md

+4
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,7 @@ You can find a list of major changes to public API below.
5656
| before | after |
5757
|-----------------------------:|:-----------------------------------------|
5858
| matching(Criteria $criteria) | matching(Criteria $criteria): Collection |
59+
60+
#### Doctrine\Common\Collections\Collection::slice()
61+
62+
The `Collection::slice()` method returns a new `Collection` instance now instead of an array.

lib/Doctrine/Common/Collections/AbstractLazyCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public function indexOf($element)
268268
/**
269269
* {@inheritDoc}
270270
*/
271-
public function slice(int $offset, ?int $length = null) : array
271+
public function slice(int $offset, ?int $length = null) : Collection
272272
{
273273
$this->initialize();
274274

lib/Doctrine/Common/Collections/ArrayCollection.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,9 @@ public function clear() : void
392392
/**
393393
* {@inheritDoc}
394394
*/
395-
public function slice(int $offset, ?int $length = null) : array
395+
public function slice(int $offset, ?int $length = null) : Collection
396396
{
397-
return array_slice($this->elements, $offset, $length, true);
397+
return $this->createFrom(array_slice($this->elements, $offset, $length, true));
398398
}
399399

400400
/**

lib/Doctrine/Common/Collections/Collection.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ public function indexOf($element);
286286
* @param int $offset The offset to start from.
287287
* @param int|null $length The maximum number of elements to return, or null for no limit.
288288
*
289-
* @return array
289+
* @return Collection
290290
*
291-
* @psalm-return array<TKey,T>
291+
* @psalm-return Collection<TKey, T>
292292
*/
293-
public function slice(int $offset, ?int $length = null) : array;
293+
public function slice(int $offset, ?int $length = null) : self;
294294
}

tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,14 @@ public function testSlice() : void
204204
$this->collection[] = 'two';
205205
$this->collection[] = 'three';
206206

207-
$slice = $this->collection->slice(0, 1);
207+
$slice = $this->collection->slice(0, 1)->toArray();
208208
self::assertInternalType('array', $slice);
209209
self::assertEquals(['one'], $slice);
210210

211-
$slice = $this->collection->slice(1);
211+
$slice = $this->collection->slice(1)->toArray();
212212
self::assertEquals([1 => 'two', 2 => 'three'], $slice);
213213

214-
$slice = $this->collection->slice(1, 1);
214+
$slice = $this->collection->slice(1, 1)->toArray();
215215
self::assertEquals([1 => 'two'], $slice);
216216
}
217217

0 commit comments

Comments
 (0)