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

[5.4] fix for requesting 0 randoms on empty collection #20402

Merged
merged 1 commit into from
Aug 2, 2017
Merged

[5.4] fix for requesting 0 randoms on empty collection #20402

merged 1 commit into from
Aug 2, 2017

Conversation

browner12
Copy link
Contributor

previously if you requested 0 random items from an empty collection, it would mistakedly think we were requesting 1 item.

this commit moves the check for a request of zero random items to the beginning, so we don't run into this issue.

also add a test.

previously if you requested 0 random items from an empty collection, it would mistakedly think we were requesting 1 item.

this commit moves the check for a request of zero random items to the beginning, so we don't run into this issue.

also add a test.
@browner12
Copy link
Contributor Author

#20396

@taylorotwell taylorotwell merged commit 8053238 into laravel:5.4 Aug 2, 2017
if ($amount === 0) {
return new static;
}

if (($requested = $amount ?: 1) > ($count = $this->count())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that 0 is a supported value, this ?: looks a bit dangerous. With PHP 7 we could just replace it with ??, but here with PHP 5 support, I'd suggest the following:

$requested = is_null($amount) ? 1 : $amount;
$count = $this->count();

if ($requested > $count) {
    throw new InvalidArgumentException(
        "You requested {$requested} items, but there are only {$count} items in the collection."
    );
}

@browner12 browner12 deleted the zero-randoms-patch branch August 3, 2017 04:11
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.

3 participants