phpstan / phpstan-symfony

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

Template type TData on class Symfony\Component\Form\FormInterface is not covariant #363

Open oleg-andreyev opened 10 months ago

oleg-andreyev commented 10 months ago
❯ composer bin phpstan info phpstan/phpstan-symfony
[bamarni-bin] Checking namespace vendor-bin/phpstanser bin phpstan info phpstan/phpstan-symfony                                                                                                          1 ✘  14:44:46  
name     : phpstan/phpstan-symfony
descrip. : Symfony Framework extensions and rules for PHPStan
keywords : 
versions : * 1.3.4
type     : phpstan-extension
license  : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText
homepage : 
source   : [git] https://github.com/phpstan/phpstan-symfony.git 383855999db6a7d65d0bf580ce2762e17188c2a5
dist     : [zip] https://api.github.com/repos/phpstan/phpstan-symfony/zipball/383855999db6a7d65d0bf580ce2762e17188c2a5 383855999db6a7d65d0bf580ce2762e17188c2a5
path     : /Users/oandreyev/Development/competitors/vendor-bin/phpstan/vendor/phpstan/phpstan-symfony
names    : phpstan/phpstan-symfony

Simple override

<?php

declare(strict_types=1);

namespace App\Symfony\Component\Form;

use EasyCorp\Bundle\EasyAdminBundle\Form\Type\FiltersFormType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactory as OriginalFormFactory;
use Symfony\Component\Form\FormInterface;

class FormFactory extends OriginalFormFactory
{
    public function createNamed(string $name, string $type = FormType::class, mixed $data = null, array $options = []): FormInterface
    {
        if ($name === 'filters' && $type === FiltersFormType::class) {
            $options = array_merge($options, [
                'validation_groups' => ['admin_filter'],
            ]);
        }

        return parent::createNamed($name, $type, $data, $options);
    }
}
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   src/Symfony/Component/Form/FormFactory.php                                                                                                                                           
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  22     Method App\Symfony\Component\Form\FormFactory::createNamed() should return Symfony\Component\Form\FormInterface<TData>|Symfony\Component\Form\FormInterface<TData|null> but returns  
         Symfony\Component\Form\FormInterface<null>|Symfony\Component\Form\FormInterface<TData of mixed>.                                                                                     
         💡 Template type TData on class Symfony\Component\Form\FormInterface is not covariant. Learn more: https://phpstan.org/blog/whats-up-with-template-covariant                         
         💡 Template type TData on class Symfony\Component\Form\FormInterface is not covariant. Learn more: https://phpstan.org/blog/whats-up-with-template-covariant                         
         💡 Template type TData on class Symfony\Component\Form\FormInterface is not covariant. Learn more: https://phpstan.org/blog/whats-up-with-template-covariant                         

 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
oleg-andreyev commented 10 months ago

just by adding @return

<?php

declare(strict_types=1);

namespace App\Symfony\Component\Form;

use EasyCorp\Bundle\EasyAdminBundle\Form\Type\FiltersFormType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactory as OriginalFormFactory;
use Symfony\Component\Form\FormInterface;

class FormFactory extends OriginalFormFactory
{
    /**
     * @return FormInterface
     */
    public function createNamed(string $name, string $type = FormType::class, mixed $data = null, array $options = []): FormInterface
    {
        if ($name === 'filters' && $type === FiltersFormType::class) {
            $options = array_merge($options, [
                'validation_groups' => ['admin_filter'],
            ]);
        }

        return parent::createNamed($name, $type, $data, $options);
    }
}

 [OK] No errors                                                                                                         

but it's getting removed by php-cs-fixer as useless.