phpstan / phpstan-symfony

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

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

Open mmarton opened 2 days ago

mmarton commented 2 days ago

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

ondrejmirtes commented 2 days ago

Not sure what's going on here. The type T of Symfony\Component\Form\FormError|Symfony\Component\Form\FormErrorIterator looks weird.

If I do this:

use Symfony\Component\Form\FormInterface;

function (FormInterface  $form): void {
    $errors = $form->getErrors(true);
    \PHPStan\dumpType($errors);
};

It outputs: Symfony\Component\Form\FormErrorIterator<Symfony\Component\Form\FormError>

Can you try to \PHPStan\dumpType($form->getErrors(true)); in your code?

And tell me the versions of Symfony packages you're using.

ondrejmirtes commented 2 days ago

Also:

    \PHPStan\dumpType($errors);

    foreach ($errors as $error) {
        \PHPStan\dumpType($error);
    }

Correctly outputs:

  9      Dumped type: Symfony\Component\Form\FormErrorIterator<Symfony\Component\Form\FormError>
  12     Dumped type: Symfony\Component\Form\FormError
mmarton commented 1 day ago

Can you try to \PHPStan\dumpType($form->getErrors(true)); in your code?

Dumped type: Symfony\Component\Form\FormErrorIterator<Symfony\Component\Form\FormError>

And tell me the versions of Symfony packages you're using.

composer show symfony/*
symfony/apache-pack                1.0.1  A pack for Apache support in Symfony
symfony/asset                      7.1.6  Manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files
symfony/browser-kit                7.1.6  Simulates the behavior of a web browser, allowing you to make requests, click on links and submit forms programmatically
symfony/cache                      7.1.7  Provides extended PSR-6, PSR-16 (and tags) implementations
symfony/cache-contracts            3.5.0  Generic abstractions related to caching
symfony/clock                      7.1.6  Decouples applications from the system clock
symfony/config                     7.1.7  Helps you find, load, combine, autofill and validate configuration values of any kind
symfony/console                    7.1.7  Eases the creation of beautiful and testable command line interfaces
symfony/css-selector               7.1.6  Converts CSS selectors to XPath expressions
symfony/debug-bundle               7.1.6  Provides a tight integration of the Symfony VarDumper component and the ServerLogCommand from MonologBridge into the Symfony full-stack framework
symfony/dependency-injection       7.1.6  Allows you to standardize and centralize the way objects are constructed in your application
symfony/deprecation-contracts      3.5.0  A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge            7.1.6  Provides integration for Doctrine with various Symfony components
symfony/doctrine-messenger         7.1.6  Symfony Doctrine Messenger Bridge
symfony/dom-crawler                7.1.6  Eases DOM navigation for HTML and XML documents
symfony/dotenv                     7.1.6  Registers environment variables from a .env file
symfony/emoji                      7.1.6  Provides access to emoji characters and sequences from the Unicode CLDR
symfony/error-handler              7.1.7  Provides tools to manage errors and ease debugging PHP code
symfony/event-dispatcher           7.1.6  Provides tools that allow your application components to communicate with each other by dispatching events and listening to them
symfony/event-dispatcher-contracts 3.5.0  Generic abstractions related to dispatching event
symfony/expression-language        7.1.6  Provides an engine that can compile and evaluate expressions
symfony/filesystem                 7.1.6  Provides basic utilities for the filesystem
symfony/finder                     7.1.6  Finds files and directories via an intuitive fluent interface
symfony/flex                       2.4.7  Composer plugin for Symfony
symfony/form                       7.1.6  Allows to easily create, process and reuse HTML forms
symfony/framework-bundle           7.1.6  Provides a tight integration between Symfony components and the Symfony full-stack framework
symfony/http-client                7.1.7  Provides powerful methods to fetch HTTP resources synchronously or asynchronously
symfony/http-client-contracts      3.5.0  Generic abstractions related to HTTP clients
symfony/http-foundation            7.1.7  Defines an object-oriented layer for the HTTP specification
symfony/http-kernel                7.1.7  Provides a structured process for converting a Request into a Response
symfony/intl                       7.1.7  Provides access to the localization data of the ICU library
symfony/lock                       7.1.6  Creates and manages locks, a mechanism to provide exclusive access to a shared resource
symfony/mailer                     7.1.6  Helps sending emails
symfony/maker-bundle               1.61.0 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/messenger                  7.1.6  Helps applications send and receive messages to/from other applications or via message queues
symfony/mime                       7.1.6  Allows manipulating MIME messages
symfony/monolog-bridge             7.1.6  Provides integration for Monolog with various Symfony components
symfony/monolog-bundle             3.10.0 Symfony MonologBundle
symfony/options-resolver           7.1.6  Provides an improved replacement for the array_replace PHP function
symfony/password-hasher            7.1.6  Provides password hashing utilities
symfony/phpunit-bridge             7.1.6  Provides utilities for PHPUnit, especially user deprecation notices management
symfony/polyfill-intl-grapheme     1.31.0 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          1.31.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          1.31.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   1.31.0 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          1.31.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php83             1.31.0 Symfony polyfill backporting some PHP 8.3+ features to lower PHP versions
symfony/polyfill-uuid              1.31.0 Symfony polyfill for uuid functions
symfony/process                    7.1.7  Executes commands in sub-processes
symfony/property-access            7.1.6  Provides functions to read and write from/to an object or array using a simple string notation
symfony/property-info              7.1.6  Extracts information about PHP class' properties using metadata of popular sources
symfony/routing                    7.1.6  Maps an HTTP request to a set of configuration variables
symfony/runtime                    7.1.7  Enables decoupling PHP applications from global state
symfony/security-bundle            7.1.6  Provides a tight integration of the Security component into the Symfony full-stack framework
symfony/security-core              7.1.6  Symfony Security Component - Core Library
symfony/security-csrf              7.1.6  Symfony Security Component - CSRF Library
symfony/security-http              7.1.7  Symfony Security Component - HTTP Integration
symfony/serializer                 7.1.6  Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
symfony/service-contracts          3.5.0  Generic abstractions related to writing services
symfony/stimulus-bundle            2.21.0 Integration with your Symfony app & Stimulus!
symfony/stopwatch                  7.1.6  Provides a way to profile code
symfony/string                     7.1.6  Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way
symfony/translation                7.1.6  Provides tools to internationalize your application
symfony/translation-contracts      3.5.0  Generic abstractions related to translation
symfony/twig-bridge                7.1.6  Provides integration for Twig with various Symfony components
symfony/twig-bundle                7.1.6  Provides a tight integration of Twig into the Symfony full-stack framework
symfony/type-info                  7.1.6  Extracts PHP types information.
symfony/uid                        7.1.6  Provides an object-oriented API to generate and represent UIDs
symfony/ux-autocomplete            2.21.0 JavaScript Autocomplete functionality for Symfony
symfony/ux-live-component          2.21.0 Live components for Symfony
symfony/ux-twig-component          2.21.0 Twig components for Symfony
symfony/validator                  7.1.7  Provides tools to validate values
symfony/var-dumper                 7.1.7  Provides mechanisms for walking through any arbitrary PHP variable
symfony/var-exporter               7.1.6  Allows exporting any serializable PHP data structure to plain PHP code
symfony/web-link                   7.1.6  Manages links between resources
symfony/web-profiler-bundle        7.1.7  Provides a development tool that gives detailed information about the execution of any request
symfony/webpack-encore-bundle      2.2.0  Integration of your Symfony app with Webpack Encore
symfony/yaml                       7.1.6  Loads and dumps YAML files

and for phpstan:

phpstan/phpdoc-parser             1.33.0 PHPDoc parser with support for nullable, intersection and generic types
phpstan/phpstan                   2.0.1  PHPStan - PHP Static Analysis Tool
phpstan/phpstan-deprecation-rules 2.0.0  PHPStan rules for detecting usage of deprecated classes, methods, properties, constants and traits.
phpstan/phpstan-doctrine          2.0.0  Doctrine extensions for PHPStan
phpstan/phpstan-strict-rules      2.0.0  Extra strict and opinionated rules for PHPStan
phpstan/phpstan-symfony           2.0.0  Symfony Framework extensions and rules for PHPStan
ondrejmirtes commented 1 day ago

I'm not able to reproduce this problem. Please create a small reproducing repository that shows this problem for you. Thanks.

mmarton commented 1 day ago

Sure thing, here it is: https://github.com/mmarton/poc-phpstansymfony-416