• Stars
    star
    145
  • Rank 254,144 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 4 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Advanced rules for PHPStan

PHPStan Rules

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!