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

FormError is not subtype of native type T of FormError|FormErrorIterator.

Open mmarton opened this issue 1 year ago • 7 comments

Hi!

I'm in the process of upgrading to phpstan 2. One error that I couldn't find any solution yet is the following.

It was working on the latest 1.x versions

<?php

namespace App\Controller;

use App\Entity\Customer;
use App\Form\CustomerType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;

class CustomerController extends AbstractController
{
    #[Route('/submit')]
    public function submit(Request $request): JsonResponse
    {
        $form = $this->createForm(CustomerType::class, new Customer());
        $form->handleRequest($request);

        if ($form->isSubmitted() && !$form->isValid()) {
             $errors = [];
            /** @var FormError $error */
            foreach ($form->getErrors(true) as $error) {
                 // process and handle  errors
            }
       }
      
        return $this->json([]);
    }
}

After phpstan 2 I get the following error:

PHPDoc tag @var with type Symfony\Component\Form\FormError is not subtype of native type T of Symfony\Component\Form\FormError|Symfony\Component\Form\FormErrorIterator.

I use the @var typehint for phpstorm, to have autocompletion. How can I fix this or is this a bug?

Thanks for your help

mmarton avatar Nov 14 '24 15:11 mmarton

Same here. When using private methods to join tables but using the joined aliases in the select([]) method, PHPStan complains.

Example:

$qb = $this
    ->createQueryBuilder('foo')
    ->select(['foo', 'bar', 'baz']);

$this
    ->joinBar($qb)
    ->joinBaz($qb);

It complains:

 ------ ------------------------------------------------------------------------------------------------------------------- 
  Line   src/Repository/FooRepository.php                                                                                  
 ------ ------------------------------------------------------------------------------------------------------------------- 
  64     QueryBuilder: [Semantical Error] line 0, col 13 near 'bar, baz,': Error: 'bar' is not defined.  
         🪪  doctrine.dql                                                                                                                                                                                                 
 ------ -------------------------------------------------------------------------------------------------------------------

These are my methods:

private function joinBar(QueryBuilder $qb): self
{
    $qb->join(\sprintf('%s.bar', 'foo'), 'bar');

    return $this;
}

private function joinBaz(QueryBuilder $qb): self
{
    $qb->join(\sprintf('%s.baz', 'foo'), 'baz');

    return $this;
}

Side note: Reason why I am using sprintf() is because actually I am using constants but for presentation reasons here I used pure strings.

siggidiel avatar Jan 27 '25 15:01 siggidiel