phpstan / phpstan-symfony

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

AbstractType generics in psalm but not in phpstan #320

Closed kissifrot closed 1 year ago

kissifrot commented 1 year ago

Hello,

I'm having issues with cross-usage of psalm and phpstan for form type classes extending AbstractType:

I'm a bit lost in this situation, as I guess suppressing psalm's MissingTemplate errors is not the good thing to do 😅

ondrejmirtes commented 1 year ago

Hi, please show an example of a class that uses AbstractType. What's the advantage of AbstractType being generic?

kissifrot commented 1 year ago

To be honest I'm not sure about this specific addition, but it sems it raises issues such as the one described in https://github.com/psalm/psalm-plugin-symfony/issues/295#issuecomment-1359526555

An example class would be:

// ...
use Symfony\Component\Form\AbstractType;

class UserBookType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('title', TextType::class, ['trim' => true])
// 
            ->add('save', SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => UserBookCreateDTO::class,
        ]);
    }
}

To then be used in symfony's AbstractController:

        // ...
        $userBookDTO = new UserBookCreateDTO();
        $form = $this->createForm(UserBookType::class, $userBookDTO);
        $form->handleRequest($request);
        // ...
VincentLanglet commented 1 year ago

There already was a PR opened for this https://github.com/phpstan/phpstan-symfony/pull/317

And I explained that the stub/generics is currently useless for PHPStan https://github.com/phpstan/phpstan-symfony/pull/317#pullrequestreview-1227793157

The solution is maybe to write

/** 
 * @psalm-extends AbstractType<Foo>
 * @phpstan-extends AbstractType
 */
ondrejmirtes commented 1 year ago

That suggestion isn't going to work. Please before you suggest something, check it with phpstan.org/try.

The solution is https://phpstan.org/user-guide/ignoring-errors

VincentLanglet commented 1 year ago

That suggestion isn't going to work. Please before you suggest something, check it with phpstan.org/try.

Indeed, it works for param/return type but not for extends https://phpstan.org/r/767eb0ad-18ea-47a1-a204-f027169c5fa3

Dunno if it's worth enough for a feature request...

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

ondrejmirtes commented 1 year ago

This PR looks promising https://github.com/phpstan/phpstan-symfony/pull/337