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

Fix real text too short #152

Merged
merged 8 commits into from
Dec 19, 2020
Merged

Conversation

krsriq
Copy link

@krsriq krsriq commented Dec 18, 2020

What is the reason for this PR?

This PR is related to #37, a more detailed description of the issues observed can be found there.

realText may produce text that is too short, e.g. by running:

$faker = \Faker\Factory::create();
$faker->seed(252);
$text = $faker->realText(2000, 2);

which returns THE. (even though 2000 characters were requested).

Author's checklist

Summary of changes

This PR fixes the issue mentioned above while adding a new Text::realTextBetween($minNbChars = 160, $maxNbChars = 200, $indexSize = 2) method (as was suggested in #37).

It adds a simple loop to make sure that the returned text from realText is at least 80% the length set with $maxNbChars. The same logic is used to provide the realTextBetween method.
Also made sure we have a maximum number of retries and added tests.

Review checklist

  • All checks have passed
  • Changes are approved by maintainer

@@ -40,7 +61,31 @@ public function realText($maxNbChars = 200, $indexSize = 2)
throw new \InvalidArgumentException('indexSize must be at most 5');
}

if ($minNbChars >= $maxNbChars) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldnt we set a minimum here as well? And check for valid values since an -10 can also be passed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now added a check that $minNbChars is not smaller than 1.

It doesn't make much sense to set $minNbChars to something much smaller than $maxNbChars, but it also doesn't prevent the algorithm from working as expected (producing real text that is between min and max chars long), so I wouldn't prohibit doing it.

@pimjansen pimjansen merged commit cb61fcd into FakerPHP:main Dec 19, 2020
@krsriq
Copy link
Author

krsriq commented Dec 19, 2020

Thank you!

@krsriq krsriq deleted the real_text_min_length branch December 19, 2020 18:14
@bram-pkg
Copy link
Member

@krsriq can you please document this?

@krsriq
Copy link
Author

krsriq commented Dec 19, 2020

@krsriq can you please document this?

@bramceulemans yes, definitely - it's on my todo list :)

@bram-pkg
Copy link
Member

@bramceulemans yes, definitely - it's on my todo list :)

Thanks :)

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.

Feature request: minimum character count for realText
3 participants