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

Crash when an empty Traversable is entered into randomElements() #604

Closed
KentarouTakeda opened this issue Feb 24, 2023 · 3 comments · Fixed by #605
Closed

Crash when an empty Traversable is entered into randomElements() #604

KentarouTakeda opened this issue Feb 24, 2023 · 3 comments · Fixed by #605

Comments

@KentarouTakeda
Copy link

Summary

Crash when an empty Traversable is entered into randomElements()

Versions

Version
PHP 8.2.3
fakerphp/faker 1.21.0

Self-enclosed code snippet for reproduction

# replace this with a self-enclosed usage example
BaseProvider::randomElements(new \ArrayIterator(), 0);

Expected output

# replace this with the expected output of your self-enclosed example

// Empty array should be returned.

Actual output

# replace this with the actual output of your self-enclosed example
TypeError: array_keys(): Argument #1 ($array) must be of type array, ArrayIterator given

Faker/src/Faker/Provider/Base.php:202
@pimjansen
Copy link

It expects an array and not a traversable. I do not really why this is a bug?

@KentarouTakeda
Copy link
Author

randomElements() are designed to handle Traversable. And it actually works (unless it is zero length).
https://github.com/FakerPHP/Faker/blob/v1.21.0/src/Faker/Provider/Base.php#L194-L198

Then i think it should be fixed that it crashes only under certain conditions.

If we want consistency and correctness of behavior, we could remove the Traversable implementation and only accept array. But that would break a lot of code that has already been written.

@stale
Copy link

stale bot commented Mar 18, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 1 week if no further activity occurs. Thank you for your contributions.

@stale stale bot closed this as completed Apr 2, 2023
@localheinz localheinz reopened this Apr 2, 2023
@stale stale bot removed the lifecycle/stale label Apr 2, 2023
@localheinz localheinz mentioned this issue Apr 2, 2023
5 tasks
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 a pull request may close this issue.

3 participants