phpstan / phpstan-symfony

Symfony extension for PHPStan
MIT License
698 stars 89 forks source link

Form "has invalid type Symfony\Component\Form\TData" after patch upgrade #377

Open Jean85 opened 7 months ago

Jean85 commented 7 months ago

I'm getting the same that arose in #337 after merge during a PHPStan patch upgrade with Renovate:

Package Type Update Change
phpstan/phpstan require-dev patch 1.10.55 -> 1.10.56
phpstan/phpstan-symfony require-dev patch 1.3.6 -> 1.3.7

Before I used @template-extends AbstractType<void>, now it fails every time, with or without the annotation.

Code example:

<?php

declare(strict_types=1);

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\File;

/**
 * @template-extends AbstractType<mixed>
 */
class UploadFileType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add(
            'file',
            FileType::class,
            [
                'label' => 'Zip',
                'required' => true,
                'constraints' => [
                    new File([
                        'mimeTypes' => [
                            'application/zip',
                        ],
                        'mimeTypesMessage' => 'Please upload a valid zip file',
                    ]),
                ],
            ]
        );

        $builder->add('submit', SubmitType::class, ['attr' => ['class' => 'btn-primary'], 'label' => 'Save']);
        $builder->setMethod(Request::METHOD_POST);

        parent::buildForm($builder, $options);
    }
}

Error:

$ vendor/bin/phpstan analyze src/Form/UploadFileType.php 
Note: Using configuration file /var/www/***/***/phpstan.neon.
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ------------------------------------------------------------------------------------------ 
  Line   UploadFileType.php                                                                        
 ------ ------------------------------------------------------------------------------------------ 
  19     Parameter $builder of method AppBundle\Form\UploadFileType::buildForm() has invalid type  
         Symfony\Component\Form\TData.                                                             
 ------ ------------------------------------------------------------------------------------------ 

 [ERROR] Found 1 error