phpstan-symfony
phpstan-symfony copied to clipboard
False positive with Symfony Forms
Hello all,
I get a "Call to an undefined method Symfony\Component\Form\FormInterface::getClickedButton()." error when working with Symfony Forms using the following code (within a Symfony Controller class):
$form = $this->createForm(...); $clickedButton = $form->getClickedButton();
The second line will raise the error since the createForm method returns a FormInterface
FIxed in 1b66d8e
Actually it wasn't, I completely screwed that up. @aturki can you show me what parameters (especially the first one) are you calling createForm with, your FormType class, and anything else you feel I could need? Thanks.
Hi @lookyman, createForm is called as:
$this->createFormat(DocumentType::class, $entity, $parameters);
Note the DocumentType is a subclass of the Symfony standard Symfony\Component\Form\AbstractType
Let me know if you need extra infos ;)
I think it's no false positive, but something ugly in the Form component:
The FormInterface returned by the builder does not include the getClickedButton() method which is part of the Form class. So phpstan would have to inspect the returned form object.
I opened a ticket for this at Symfony, see https://github.com/symfony/symfony/issues/35277
For now, the solution is to add a PHPDoc - see https://github.com/symfony/symfony/issues/35277#issuecomment-572540892 :
use Symfony\Component\Form\Form;
/** @var Form $form */
$button = $form->getClickedButton();