Skip to content

Fix real text too short#152

Merged
pimjansen merged 8 commits intoFakerPHP:mainfrom
krsriq:real_text_min_length
Dec 19, 2020
Merged

Fix real text too short#152
pimjansen merged 8 commits intoFakerPHP:mainfrom
krsriq:real_text_min_length

Conversation

@krsriq
Copy link
Copy Markdown

@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

throw new \InvalidArgumentException('indexSize must be at most 5');
}

if ($minNbChars >= $maxNbChars) {
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

@krsriq can you please document this?

@krsriq
Copy link
Copy Markdown
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
Copy Markdown
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