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

Collection#random generates warning when passing int larger than collection count #1135

Closed
fredrikekelund opened this issue Oct 27, 2016 · 2 comments
Labels

Comments

@fredrikekelund
Copy link
Contributor

If I do this:

$taxonomy_map = $this->grav['taxonomy'];

$collection = $taxonomy_map->findTaxonomy([
    'type' => $term
]);

$collection->random(4);

And $collection contains fewer than 4 pages, I get an error:

array_rand(): Second argument has to be between 1 and the number of elements in the array

Maybe throwing an error is the right thing to do here, if I ask for 4 pages, maybe I should be able to always expect 4 pages. But a manual check in Collection#random that $num is smaller than the collection count, and a more user friendly error message if that's not the case, might be helpful!

@Perlkonig
Copy link
Contributor

The error message seems pretty user friendly. The only problem is the random function itself isn't mentioned. If you pass a number larger than the count, should the function just silently change the number to count(array)?

if ($num > count($this->items)) {
    $num = count($this->items);
}

@flaviocopes
Copy link
Contributor

I think so, so if you ask for 4 but you only got 2, 2 are returned without any error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants