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 three plugins to install. The Symfony one is essential as it allows PHPStan to access and inspect the dependency injection container. The two 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! 😀


includes:
    # require phpstan/extension-installer to avoid inlcuding these lines
    #- 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

parameters:
    # https://phpstan.org/config-reference#rule-level
    level: max

    # https://phpstan.org/config-reference#analysed-files
    paths:
        - config
        - src
        - tests
        - public

    # https://github.com/phpstan/phpstan-symfony#configuration
    symfony:
        container_xml_path: %rootDir%/../../../var/cache/dev/App_KernelDevDebugContainer.xml

    # https://phpstan.org/config-reference#vague-typehints
    checkMissingIterableValueType: false           # this is my next todo
    #checkGenericClassInNonGenericObjectType: true # is parameter is activated at level 6

    # It's a special "dev" project, I have to ignore the following warnings because
    # the use of this functions is intended.
    # @see https://phpstan.org/user-guide/ignoring-errors
    ignoreErrors:
        - '#Should not use node with type "Expr_Exit", please change the code.#' # warnings for "die()"
        - '#Should not use node with type "Stmt_Echo", please change the code.#' # warnings for "echo()"
        - '#Should not use function "print_r", please change the code.#'         # warnings for "print_r()"
        - '#Should not use function "var_dump", please change the code.#'        # warnings for "var_dump()"
        - '#Should not use function "phpinfo", please change the code.#'         # warnings for "phpinfo()"
        - '#Service "http_client" is not registered in the container.#'          # bug

    # 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