Skip to content

[Core] Fix Helper::numerify#256

Merged
pimjansen merged 1 commit intoFakerPHP:mainfrom
krsriq:fix_helper_numerify
Jan 13, 2021
Merged

[Core] Fix Helper::numerify#256
pimjansen merged 1 commit intoFakerPHP:mainfrom
krsriq:fix_helper_numerify

Conversation

@krsriq
Copy link
Copy Markdown

@krsriq krsriq commented Jan 8, 2021

What is the reason for this PR?

There's a bug in Helper::numerify caused by a - compared to Base::numerify - changed use of mt_rand:
Base::numerify uses randomNumber($size), where the argument $size is the number of digits while Helper::numerify uses mt_rand(0, $size), where $size is the highest possible value to be returned.

Thus executing Helper::numerify(str_repeat('#', 10)) always returns strings like 0000000050:

Factory::create()->seed(1);
echo Helper::numerify(str_repeat('#', 10)); // "0000000050"
echo BaseProvider::numerify(str_repeat('#', 10)); // "7203244899"
  • Fixed an issue

Author's checklist

Summary of changes

This PR fixes the issue and adds a test to verify functionality. The test is based on a very high probability that Helper::numerify(str_repeat('#', 10)) should return large numbers.

Since mt_rand(0, 10 ** $size - 1) is not as easy to read as static::randomNumber($size) (at least for me), I'd be in favor of replacing the change done with this PR once the number extension is implemented, but this is out of scope for this bugfix PR.

Review checklist

  • All checks have passed
  • Changes are approved by maintainer

@krsriq krsriq mentioned this pull request Jan 8, 2021
5 tasks
@bram-pkg bram-pkg changed the title Fix Helper::numerify [Core] Fix Helper::numerify Jan 8, 2021
@pimjansen pimjansen added the bug Something isn't working label Jan 8, 2021
@pimjansen
Copy link
Copy Markdown

Do we have an example on the diff output of botht he old and new method?

@krsriq
Copy link
Copy Markdown
Author

krsriq commented Jan 13, 2021

Do we have an example on the diff output of botht he old and new method?

To reproduce this you can try

Factory::create()->seed(1);
echo Helper::numerify('##########'); // "0000000050"
echo BaseProvider::numerify('##########'); // "7203244899"

@pimjansen pimjansen merged commit 41f699e into FakerPHP:main Jan 13, 2021
@pimjansen
Copy link
Copy Markdown

Thanks!

@krsriq
Copy link
Copy Markdown
Author

krsriq commented Jan 14, 2021

Thanks @pimjansen

@krsriq krsriq deleted the fix_helper_numerify branch January 14, 2021 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants