symplify/phpstan-rules

Set of Symplify rules for PHPStan

Installs: 2 440 410

Dependents: 84

Suggesters: 2

Security: 0

Stars: 118

Watchers: 4

Forks: 21

Open Issues: 0

Type:phpstan-extension

12.3.1 2023-09-29 08:20 UTC

This package is auto-updated.

Last update: 2023-09-30 12:51:25 UTC


README

Downloads

Set of rules for PHPStan used by Symplify projects

Install

composer require symplify/phpstan-rules --dev

1. Add Static Rules to phpstan.neon

Some of rules here require configuration, some not. We recommend to start with rules that do not require any configuration, because there is just one way to use them:

# phpstan.neon
includes:
    - vendor/symplify/phpstan-rules/config/static-rules.neon

Give it couple of days, before extending.


Some rules require extra services. To avoid service duplications, they're in the separate config that you can easily include:

includes:
    - vendor/symplify/phpstan-rules/config/services/services.neon

2. Pick from Prepared Sets

Do you know prepared sets from ECS or Rector? Bunch of rules in single set. We use the same approach here:

includes:
    - vendor/symplify/phpstan-rules/config/code-complexity-rules.neon
    - vendor/symplify/phpstan-rules/config/naming-rules.neon
    - vendor/symplify/phpstan-rules/config/regex-rules.neon
    - vendor/symplify/phpstan-rules/config/forbidden-static-rules.neon
    - vendor/symplify/phpstan-rules/config/test-rules.neon

Pick what you need, drop the rest.

3. How we use Configurable Rules

Last but not least, configurable rules with saints defaults:

# phpstan.neon
includes:
    - vendor/symplify/phpstan-rules/config/configurable-rules.neon

Would you like to configure them yourself?

  • Pick one and put it to your phpstan.neon manually.
  • Configure it to your specific needs and re-run PHPStan.

E.g. ForbiddenNodeRule:

services:
    -
        class: Symplify\PHPStanRules\Rules\ForbiddenNodeRule
        tags: [phpstan.rules.rule]
        arguments:
            forbiddenNodes:
                - PhpParser\Node\Expr\Empty_
                - PhpParser\Node\Stmt\Switch_
                - PhpParser\Node\Expr\ErrorSuppress

Happy coding!