Typical PHPStan configuration file for a Symfony project

Published on 2020-11-11 • Modified on 2020-11-20

Here is a typical PHPStan configuration file for a Symfony project. I have added some links to the documentation, so you know what the purpose of each parameter is. There are some plugins to install. The Symfony one is essential as it allows PHPStan to access and inspect the dependency injection container. The others bring additional rules. Of course, you are free to install them or not. If you start using PHPStan with a legacy project, it will probably raise a lot of warnings. In this case, my advice is to start with level 0 and then increase the level once you have fixed the errors of the current level. Take this as a game! 😀


# configuration/phpstan.neon
includes:
    # require phpstan/extension-installer to avoid inlcuding these lines                                            PHPStan 1.x compat
    #- vendor/ekino/phpstan-banned-code/extension.neon     # https://github.com/ekino/phpstan-banned-code           ✅
    #- vendor/phpstan/phpstan-symfony/extension.neon       # https://github.com/phpstan/phpstan-symfony             ✅
    #- vendor/phpstan/phpstan-deprecation-rules/rules.neon # https://github.com/phpstan/phpstan-deprecation-rules   ✅
    #- vendor/phpstan/phpstan-strict-rules/rules.neon      # https://github.com/phpstan/phpstan-strict-rules        ✅
    #- vendor/phpstan/phpstan/phpstan-doctrine             # https://github.com/phpstan/phpstan-doctrine            ✅

# These are custom rules, check-out: https://www.strangebuzz.com/en/blog/creating-custom-phpstan-rules-for-your-symfony-project
rules:
    - App\PHPStan\ControllerIsFinalRule
    - App\PHPStan\ControllerExtendsSymfonyRule
    #- App\PHPStan\NoNewinControllerRule

parameters:
    # https://phpstan.org/config-reference#rule-level
    level: max # Max is level 9 as of PHPStan 1.0

    # https://phpstan.org/config-reference#analysed-files
    # Note that I have put my configuraiton file in the "./configuration" directory
    # if you have yours at the root of your project remove the "../"
    paths:
        - ../config
        - ../src
        - ../tests
        - ../public

    # https://github.com/phpstan/phpstan-symfony#configuration
    # Specific configuration for the Symfony plugin
    symfony:
        # I use the prod env because I have false positive regarding the tests which
        # are executed in the test environment.
        container_xml_path: ../var/cache/prod/App_KernelProdDebugContainer.xml

    # https://phpstan.org/config-reference#vague-typehints
    checkMissingIterableValueType:           true # https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
    checkGenericClassInNonGenericObjectType: true # this parameter is activated at level 6

    # Nothing ignored! (almost!) 🎉
    ignoreErrors:
        - '#Dead catch - Error is never thrown in the try block.#'
        - '#Variable method call#'

    # I don't use the Symfony PHPUnit bridge in this project, but if you do, you
    # probably will have to add the following bootstrap file:
    #bootstrapFiles:
        #- %rootDir%/../../../vendor/bin/.phpunit/phpunit/vendor/autoload.php

 More on Stackoverflow   Read the doc  More on the web  Random snippet