Skip to content

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

Open
@mmarton

Description

@mmarton

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions