phpstan-symfony icon indicating copy to clipboard operation
phpstan-symfony copied to clipboard

False positive with Symfony Forms

Open aturki opened this issue 7 years ago • 8 comments

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

aturki avatar Sep 12 '18 10:09 aturki

FIxed in 1b66d8e

lookyman avatar Oct 23 '18 08:10 lookyman

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.

lookyman avatar Oct 25 '18 13:10 lookyman

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 ;)

aturki avatar Oct 26 '18 15:10 aturki

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.

althaus avatar Apr 03 '19 13:04 althaus

I opened a ticket for this at Symfony, see https://github.com/symfony/symfony/issues/35277

ThomasLandauer avatar Jan 09 '20 10:01 ThomasLandauer

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();

ThomasLandauer avatar Jan 12 '20 11:01 ThomasLandauer