Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1462,8 +1462,8 @@ public function chunk($size, $preserveKeys = true)
/**
* Chunk the collection into chunks with a callback.
*
* @param callable(TValue, TKey, static<int, TValue>): bool $callback
* @return static<int, static<int, TValue>>
* @param callable(TValue, TKey, static<TKey, TValue>): bool $callback
* @return static<int, static<TKey, TValue>>
*/
public function chunkWhile(callable $callback)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ public function splitIn($numberOfGroups)
* Chunk the collection into chunks with a callback.
*
* @param callable(TValue, TKey, Collection<TKey, TValue>): bool $callback
* @return static<int, static<int, TValue>>
* @return static<int, static<TKey, TValue>>
*/
public function chunkWhile(callable $callback)
{
Expand Down
15 changes: 15 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,21 @@ public function testChunkWhileOnContiguouslyIncreasingIntegers($collection)
$this->assertEquals([8 => 19, 9 => 20, 10 => 21], $data->last()->toArray());
}

#[DataProvider('collectionClassProvider')]
public function testChunkWhilePreservingStringKeys($collection)
{
$data = (new $collection(['a' => 1, 'b' => 1, 'c' => 2, 'd' => 2, 'e' => 3, 'f' => 3, 'g' => 3]))
->chunkWhile(function ($current, $key, $chunk) {
return $chunk->last() === $current;
});

$this->assertInstanceOf($collection, $data);
$this->assertInstanceOf($collection, $data->first());
$this->assertEquals(['a' => 1, 'b' => 1], $data->first()->toArray());
$this->assertEquals(['c' => 2, 'd' => 2], $data->get(1)->toArray());
$this->assertEquals(['e' => 3, 'f' => 3, 'g' => 3], $data->last()->toArray());
}

#[DataProvider('collectionClassProvider')]
public function testEvery($collection)
{
Expand Down
Loading