Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[12.x] collapseWithKeys - Prevent exception in base case #56002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 12, 2025

Conversation

DeanWunder
Copy link
Contributor

@DeanWunder DeanWunder commented Jun 12, 2025

Currently the collapseWithKeys method of Collections will throw an exception if the collection is already flat (i.e. none of the top level elements are arrays or collections).

Before the changes in this PR:

Psy Shell v0.12.8 (PHP 8.3.21 — cli) by Justin Hileman
> collect(['a', 'b', 'c'])->collapseWithKeys();

   ArgumentCountError  array_replace() expects at least 1 argument, 0 given.

After the changes in this PR:

Psy Shell v0.12.8 (PHP 8.3.21 — cli) by Justin Hileman
> collect(['a', 'b', 'c'])->collapseWithKeys();
= Illuminate\Support\Collection {#12283
    all: [],
  }

This PR handles this case by returning an empty collection, similar to what LazyCollection does.

This PR should be considered for merging because it allows collapseWithKeys to operate over a larger scope of potential inputs sensibly without causing exceptions to be thrown. The user of collapseWithKeys should be able to use it on collections without having to check the structure of the collection first to prevent exceptions.

Happy to make any adjustments, no hard feelings if closure.

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@DeanWunder DeanWunder changed the title collapseWithKeys - Prevent exception in base case [12.x] collapseWithKeys - Prevent exception in base case Jun 12, 2025
… functionality of Collection matches LazyCollection
@DeanWunder DeanWunder marked this pull request as ready for review June 12, 2025 10:17
@taylorotwell taylorotwell merged commit 829394f into laravel:12.x Jun 12, 2025
60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.