Skip to content

AbstractType generics in psalm but not in phpstan #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kissifrot opened this issue Dec 30, 2022 · 7 comments
Closed

AbstractType generics in psalm but not in phpstan #320

kissifrot opened this issue Dec 30, 2022 · 7 comments

Comments

@kissifrot
Copy link

kissifrot commented Dec 30, 2022

Hello,

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

  • on psalm (v5) side with psalm-plugin-symfony, it's considered generic, thus adding the annotation /** @extends AbstractType<mixed> */ (or similar ^^) is needed to avoid getting MissingTemplate errors
  • on phpstan side with phpstan-symfony phpstan will complain that AbstractType is not generic

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

@ondrejmirtes
Copy link
Member

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

@kissifrot
Copy link
Author

kissifrot commented Dec 30, 2022

To be honest I'm not sure about this specific addition, but it sems it raises issues such as the one described in psalm/psalm-plugin-symfony#295 (comment)

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
Copy link
Contributor

There already was a PR opened for this #317

And I explained that the stub/generics is currently useless for PHPStan #317 (review)

The solution is maybe to write

/** 
 * @psalm-extends AbstractType<Foo>
 * @phpstan-extends AbstractType
 */

@ondrejmirtes
Copy link
Member

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
Copy link
Contributor

VincentLanglet commented Feb 20, 2023

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
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2023
@ondrejmirtes
Copy link
Member

This PR looks promising #337

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants