From ea8c936683a86ea3d4f255c4a2dc6e4e1074ba62 Mon Sep 17 00:00:00 2001 From: "jaap.vd.vis" Date: Tue, 8 Apr 2025 20:12:15 +0200 Subject: [PATCH] Typehint for key-support for Collection::chunkWhile and LazyCollection::chunkWhile --- src/Illuminate/Collections/Collection.php | 4 ++-- src/Illuminate/Collections/LazyCollection.php | 2 +- tests/Support/SupportCollectionTest.php | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 486570ebd71f..23e1af7bbfee 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1462,8 +1462,8 @@ public function chunk($size, $preserveKeys = true) /** * Chunk the collection into chunks with a callback. * - * @param callable(TValue, TKey, static): bool $callback - * @return static> + * @param callable(TValue, TKey, static): bool $callback + * @return static> */ public function chunkWhile(callable $callback) { diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 68d6a4acdfef..601e0717590a 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1431,7 +1431,7 @@ public function splitIn($numberOfGroups) * Chunk the collection into chunks with a callback. * * @param callable(TValue, TKey, Collection): bool $callback - * @return static> + * @return static> */ public function chunkWhile(callable $callback) { diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index b1e3a387f46e..2edd11ca6718 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -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) {