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

A::random (Error when the limit is bigger than the array) #5934

Closed
modufolio opened this issue Nov 7, 2023 · 2 comments · Fixed by #6555
Closed

A::random (Error when the limit is bigger than the array) #5934

modufolio opened this issue Nov 7, 2023 · 2 comments · Fixed by #6555
Assignees
Labels
type: enhancement ✨ Suggests an enhancement; improves Kirby
Milestone

Comments

@modufolio
Copy link

Description

Error:

array_rand(): Argument #2 ($num) must be between 1 and the number of elements in argument #1 ($array)

Source

	public static function random(
		array $array,
		int $count = 1,
		bool $shuffle = false
	): array {
		if ($shuffle === true) {
			return array_slice(self::shuffle($array), 0, $count);
		}

		if ($count === 1) {
			$key = array_rand($array);
			return [$key => $array[$key]];
		}

		return self::get($array, array_rand($array, $count));
	}

fix would be:

	public static function random(
		array $array,
		int $count = 1,
		bool $shuffle = false
	): array {
		if ($shuffle === true) {
			return array_slice(self::shuffle($array), 0, $count);
		}

                $arrayCount = count($array);

                // Return the entire array when count is greater or equal to the array size
                if ($count >= $arrayCount) {
                    return $array; 
                }

		if ($count === 1) {
			$key = array_rand($array);
			return [$key => $array[$key]];
		}

		return self::get($array, array_rand($array, $count));
	}

Your setup

kirby cms 4.beta1

@distantnative
Copy link
Member

Not sure I agree with the solution. $count is the number of elements you expect to be returned. I think an error message when requesting too many items is more helpful than magically getting fewer item than requested.

@modufolio
Copy link
Author

modufolio commented Nov 7, 2023

If you say that the error message is more helpful. I suggest to make it visible that the function can throw an error and add a more user friendly message.

Example in Laravel:


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

@lukasbestle lukasbestle added the type: enhancement ✨ Suggests an enhancement; improves Kirby label Nov 7, 2023
@distantnative distantnative self-assigned this Jul 22, 2024
@distantnative distantnative linked a pull request Jul 22, 2024 that will close this issue
4 tasks
@distantnative distantnative added this to the 4.4.0 milestone Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement ✨ Suggests an enhancement; improves Kirby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants