PHPStan

Edit on GitHub

PHPStan is a static code analyzer that introspects the code without running it and catches various classes of bugs prior to unit testing.

Install PHPStan

composer require --dev phpstan/phpstan

Use PHPStan

  1. Generate autocompletion and prevent any error messages that might occur because of incomplete classes:
vendor/bin/console dev:ide:generate-auto-completion
  1. Start analyzing:
php -d memory_limit=2048M vendor/bin/phpstan analyze -l 6 -c phpstan.neon src/`

Demo shops are by default configured with PHPStan at level 6 in phpstan.neon. We highly recommend this configuration.

Baseline

If you need to raise the level, add new rules or extensions, you can generate a baseline and enable it for future changes.

For more information, see PHPStan baseline.

Main configuration file inheritance

You can extend phpstat.neon and define per-module configuration in the parameters section to reuse the matching configuration.

./[ROOT]/phpstan.neon

parameters:
    excludes_analyse:
        - %rootDir%/../../../src/Generated/*
        - %rootDir%/../../../src/Orm/*

bootstrap: %rootDir%/../../../phpstan-bootstrap.php

services:
    -
        class: PhpStan\DynamicType\FacadeDynamicTypeExtension
        tags:
            - phpstan.broker.dynamicMethodReturnTypeExtension
...

./[Module]/phpstan.neon

parameters:
    ignoreErrors:
    - '#.+ has invalid typehint type Symfony\\Component\\OptionsResolver\\OptionsResolverInterface.#'
    - '#Argument of an invalid type .+RolesTransfer supplied for foreach, only iterables are supported.#'