From 153e5e105e443d24ee3165fddcaf5fe03a91b01d Mon Sep 17 00:00:00 2001 From: Dean Wunder Date: Thu, 12 Jun 2025 11:48:40 +1000 Subject: [PATCH 1/3] collapseWithKeys - Ensure base case of already flat collection doesn't result in an exception. --- src/Illuminate/Collections/Collection.php | 4 ++++ tests/Support/SupportCollectionTest.php | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 95faa17a7121..3d88988b48d5 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -165,6 +165,10 @@ public function collapseWithKeys() $results[$key] = $values; } + if (!$results) { + return $this; + } + return new static(array_replace(...$results)); } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index ac93ab3f2334..a1c799f378d9 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1772,6 +1772,10 @@ public function testCollapseWithKeys($collection) { $data = new $collection([[1 => 'a'], [3 => 'c'], [2 => 'b'], 'drop']); $this->assertEquals([1 => 'a', 3 => 'c', 2 => 'b'], $data->collapseWithKeys()->all()); + + // Case with an already flat collection + $data = new $collection(['a', 'b', 'c']); + $this->assertEquals(['a', 'b', 'c'], $data->collapseWithKeys()->all()); } #[DataProvider('collectionClassProvider')] From 1e33d1a5cf89761c6348d7154f3039ac2064549f Mon Sep 17 00:00:00 2001 From: Dean Wunder Date: Thu, 12 Jun 2025 11:56:47 +1000 Subject: [PATCH 2/3] 12.x Fix style error --- src/Illuminate/Collections/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 3d88988b48d5..40069cbf1431 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -165,7 +165,7 @@ public function collapseWithKeys() $results[$key] = $values; } - if (!$results) { + if (! $results) { return $this; } From b3e9026230f18ad208e6a9a7a9178ffb2ac71993 Mon Sep 17 00:00:00 2001 From: Dean Wunder Date: Thu, 12 Jun 2025 20:01:17 +1000 Subject: [PATCH 3/3] 12.x Opting to return empty collection in this case instead to ensure functionality of Collection matches LazyCollection --- src/Illuminate/Collections/Collection.php | 2 +- tests/Support/SupportCollectionTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 40069cbf1431..becabe47f608 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -166,7 +166,7 @@ public function collapseWithKeys() } if (! $results) { - return $this; + return new static; } return new static(array_replace(...$results)); diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index a1c799f378d9..9a5433f3f55f 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1775,7 +1775,7 @@ public function testCollapseWithKeys($collection) // Case with an already flat collection $data = new $collection(['a', 'b', 'c']); - $this->assertEquals(['a', 'b', 'c'], $data->collapseWithKeys()->all()); + $this->assertEquals([], $data->collapseWithKeys()->all()); } #[DataProvider('collectionClassProvider')]