-
Notifications
You must be signed in to change notification settings - Fork 57
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
Form get errors #243
Conversation
Hi @seferov, this is the equivalent of the phpstan dynamic return type phpstan/phpstan-symfony#228
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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)]),
]),
]);
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 :)
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.
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?
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.
orklah
commented
Feb 4, 2022
yeah, please do. $error1 should definitely not be FormError|FormErrorIterator
$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();
orklah
commented
Feb 4, 2022
That's what I would have done too at least :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you
No description provided.