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

Add Faker optional() by default for nullable fields #536

Open
MrYamous opened this issue Dec 6, 2023 · 6 comments
Open

Add Faker optional() by default for nullable fields #536

MrYamous opened this issue Dec 6, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@MrYamous
Copy link
Contributor

MrYamous commented Dec 6, 2023

When using make:factory --all-fields Foundry generate default values for all fields according to their type.
WDYT about adding optional() method from Faker by default for those values ?

For example if i have an entity with a nullable field phone currently the default generated is :

'phone' => self::faker()->text(),

My proposal would be to have :

'phone' => self::faker()->optional()->text(),

@nikophil
Copy link
Member

nikophil commented Dec 6, 2023

Hi @MrYamous

This sounds good! I assume you'd want that only for nullable properties?

@MrYamous
Copy link
Contributor Author

MrYamous commented Dec 6, 2023

Hi @MrYamous

This sounds good! I assume you'd want that only for nullable properties?

Hi,

Yes only for nullable properties
IIUC it will be when --all-fields flag is added as this option is for generate default for nullable too

@nikophil
Copy link
Member

nikophil commented Dec 6, 2023

you're absolutely right!

are you willing to provide a PR?

@MrYamous
Copy link
Contributor Author

MrYamous commented Dec 7, 2023

Yes i'll work on it

@MrYamous
Copy link
Contributor Author

I've started looking how to do it but I'm not sure I'm going in the right direction.

To start with DoctrineScalarFieldsDefaultPropertiesGuesser i think I have to add in const DEFAULTS a way to know if ->optional() should be added, for a string it could be
'STRING' => 'self::faker(){optional}->text({length}),'
And then use str_replace to add optional method from Faker or just let the default value

@nikophil
Copy link
Member

maybe str_replace() on {faker}->text({length})?

In this class you can know if the field is nullable from doctrine's metadata

be careful, you'd need to modify ObjectDefaultPropertiesGuesser as well

@nikophil nikophil added the enhancement New feature or request label Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

2 participants