Skip to content

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

Merged
taylorotwell merged 1 commit into
laravel:5.4from
browner12:zero-randoms-patch
Aug 2, 2017
Merged

[5.4] fix for requesting 0 randoms on empty collection#20402
taylorotwell merged 1 commit into
laravel:5.4from
browner12:zero-randoms-patch

Conversation

@browner12

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

#20396

@taylorotwell
taylorotwell merged commit 8053238 into laravel:5.4 Aug 2, 2017
return new static;
}

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

Copy link
Copy Markdown
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