Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Form get errors #243

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

Merged
seferov merged 4 commits into psalm:master from VincentLanglet:formGetErrors
Feb 8, 2022
Merged

Form get errors #243

seferov merged 4 commits into psalm:master from VincentLanglet:formGetErrors
Feb 8, 2022

Conversation

@VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Jan 30, 2022

No description provided.

@VincentLanglet VincentLanglet marked this pull request as ready for review January 30, 2022 13:48
Copy link
Contributor Author

VincentLanglet commented Jan 30, 2022
edited
Loading

Hi @seferov, this is the equivalent of the phpstan dynamic return type phpstan/phpstan-symfony#228

/** @psalm-trace $error6 */
}

foreach ($form->getErrors(true, false) as $error7) {
Copy link
Member

@seferov seferov Feb 3, 2022

Choose a reason for hiding this comment

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

looks good to me but I don't understand how error5 (L:31) is different than this one?

Copy link
Contributor Author

@VincentLanglet VincentLanglet Feb 3, 2022

Choose a reason for hiding this comment

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

Nice catch ; seems like this is not working and the test is wrong.

Every error are still Symfony\Component\Form\FormError|Symfony\Component\Form\FormErrorIterator
and the test is only checking it contains Symfony\Component\Form\FormError.

I'll update the test

@VincentLanglet VincentLanglet marked this pull request as draft February 3, 2022 22:39
Copy link
Contributor Author

Any idea was could be wrong with my code @orklah ?

When I add dump inside the return type provider, it's executed and returning

return new Type\Union([
 new Type\Atomic\TGenericObject(FormErrorIterator::class, [
 new Type\Union([new TNamedObject(FormError::class)]),
 ]),
 ]);

Copy link

orklah commented Feb 4, 2022

Can you give me more details please? What are you trying to do, what is the issue you encounter? Note: I don't use symfony a lot so don't assume I know what those classes do :)

Copy link
Contributor Author

Can you give me more details please? What are you trying to do, what is the issue you encounter? Note: I don't use symfony a lot so don't assume I know what those classes do :)

Sure,

The method FormInterface::getErrors is returning a FormErrorIterator which is an Iterator of FormError|FormErrorIterator.

When you call getErrors(true, false) you really get a FormErrorIterator<FormError|FormErrorIterator>.
But in every other case, getErrors is returning a FormErrorIterator<FormError>., so you should be able to iterator on the FormErrorIterator and call getMessage (which is a method of a FormError) on every item.

So I tried to introduce a dynamic return type provider, which seems to return the correct value according to my dumps. I even added a stub for FormErrorIterator to be sure it's a generic for the items it iterate over. And still,

 foreach ($form->getErrors() as $error1) {
 $messages[] = $error1->getMessage();
 }

is reported as an error.

Copy link

orklah commented Feb 4, 2022

I can't see your Trace in the CI, but the fact Psalm emits a MixedAssignment is not a good sign. It may be because of the recursion on @template T of FormError|FormErrorIterator

Can you tell me what the Trace is returning on your side?

Copy link
Contributor Author

With

 foreach ($form->getErrors() as $error1) {
 /** @psalm-trace $error1 */
 $messages[] = $error1->getMessage();
 }

I had $error1: FormError|FormErrorIterator

I didn't try to trace $foo = $form->getErrors(), but I will.

Copy link

orklah commented Feb 4, 2022

yeah, please do. $error1 should definitely not be FormError|FormErrorIterator

Copy link
Contributor Author

$errors = $form->getErrors();
/** @psalm-trace $errors */
foreach ($errors as $error6) {
 /** @psalm-trace $error6 */
 $messages[] = $error6->getMessage();
}

gives

$errors: Symfony\Component\Form\FormErrorIterator<Symfony\Component\Form\FormError>
$error6: Symfony\Component\Form\FormError|Symfony\Component\Form\FormErrorIterator<Symfony\Component\Form\FormError|Symfony\Component\Form\FormErrorIterator<Symfony\Component\Form\FormError|Symfony\Component\Form\FormErrorIterator>> 

I discovered I think it was related to the definition of the current method

 /**
 * Returns the current element of the iterator.
 *
 * @return FormError|self An error or an iterator containing nested errors
 */
 public function current()
 {
 return current($this->errors);
 }

So I overrade the phpdoc in the stub to

 /**
 * @return T
 */
 public function current();

@VincentLanglet VincentLanglet marked this pull request as ready for review February 4, 2022 18:33
Copy link
Contributor Author

I'd like the confirmation by @orklah I did the right fix but I think this is ready to be merged @seferov :)

Copy link

orklah commented Feb 4, 2022

That's what I would have done too at least :)

VincentLanglet reacted with thumbs up emoji

Copy link
Member

@seferov seferov left a comment

Choose a reason for hiding this comment

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

thank you

@seferov seferov merged commit f5348e9 into psalm:master Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@seferov seferov seferov approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /