Skip to content
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

fix(Collection::pop()): count < 1 #54340

Merged
merged 2 commits into from
Jan 24, 2025
Merged

Conversation

artumi-richard
Copy link
Contributor

If $count = 0 then this method is returning two items. If count<0 then things got worse than that. I think that in this case allowing $count <1 makes no sense and either we should throw an error, or we should return an empty result. In this PR I've gone with an empty result as I can see empty results being returned by the function anyway, so I would expect end user code to understand it, so I would expect that to be the least problematic solution.

I guess there is danger merging this PR if people are accidentally passing negative numbers through and their code is right by accident, but from the evidence below I think that's a weird consideration to block this PR.

Below is a tinker. I understand cases where $count is 3 and 2, but not what we're getting if it's 0,-1 etc.

> collect([1,2])->pop(3);
= Illuminate\Support\Collection {#7529
    all: [
      2,
      1,
    ],
  }

> collect([1,2])->pop(-1);
= Illuminate\Support\Collection {#7526
    all: [
      2,
      1,
      null,
    ],
  }

> collect([1,2])->pop(2);
= Illuminate\Support\Collection {#7527
    all: [
      2,
      1,
    ],
  }

> collect([1,2])->pop(0);
= Illuminate\Support\Collection {#7516
    all: [
      2,
      1,
    ],
  }

> collect([1,2])->pop(1);
= 2

Also I would have preferred $count=1 to return a Collection, but I'm sure that boat has sailed.

artumi-richard and others added 2 commits January 24, 2025 15:19
If $count = 0 then this method was returning the same as if $count=1. If count<0 then things got worse than that. I think that in this case pop of <1 makes no sense and either we should throw an error, or we should return an empty result. I've gone with an empty result as I can see that being returned by the function anyway.
@taylorotwell taylorotwell merged commit 8a46320 into laravel:11.x Jan 24, 2025
13 of 14 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