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

Make FormErrorIterator generic #45696

Merged
merged 1 commit into from
Mar 12, 2022
Merged

Make FormErrorIterator generic #45696

merged 1 commit into from
Mar 12, 2022

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Mar 10, 2022

Q A
Branch? 5.4
Bug fix? yes/no
New feature? yes/no
Deprecations? no
Tickets Fix #...
License MIT
Doc PR symfony/symfony-docs#...

I target 5.4, since it's a follow up of #45322 which introduced some errors in my code.

It allows to typehint the Form::getErrors method to return FormErrorIterator<FormError> most of the time (which is possible by psalm/phpstan plugins with ReturnTypeProviders).

@VincentLanglet
Copy link
Contributor Author

I added a commit to fix the psalm build @stof ; but I don't know why it wasn't failing before.

@derrabus
Copy link
Member

I don't understand this change. The current annotations look correct to me and why would FormErrorIterator need a type variable?

@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Mar 10, 2022

I don't understand this change. The current annotations look correct to me and why would FormErrorIterator need a type variable?

Currently $form->getErrors() is returning FormErrorIterator which means a FormErrorIterator<FormError|FormErrorIterator>.

This can be more precise according to
https://github.com/symfony/symfony/blob/6.1/src/Symfony/Component/Form/FormInterface.php#L90-L94

$form->getErrors(), $form->getErrors(false), $form->getErrors(true) or $form->getErrors(true, true) are only returning a flatten result. So it's a FormErrorIterator<FormError> which is more precise than FormErrorIterator<FormError|FormErrorIterator>. This allow to avoid useless checks when iterating on the errors.

ReturnTypeProvider for phpstan/psalm can be implemented and already half done but they require the class to be a generic.

Knowing the result is only a FormErrorIterator can allow to write

        $errors = [];
        foreach ($form->getErrors(true) as $error) {
            $errors[] = $error->getMessage();
        }

without any complaints.

@derrabus
Copy link
Member

$form->getErrors(), $form->getErrors(false), $form->getErrors(true) or $form->getErrors(true, true) are only returning a flatten result.

Oh right. Makes sense, thanks for the explanation.

@fabpot
Copy link
Member

fabpot commented Mar 12, 2022

Thank you @VincentLanglet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants