phpstan / phpstan-strict-rules

Extra strict and opinionated rules for PHPStan
MIT License
592 stars 46 forks source link
php php7 phpstan safety static-analysis static-code-analysis strongly-typed

Extra strict and opinionated rules for PHPStan

Build Latest Stable Version License

PHPStan focuses on finding bugs in your code. But in PHP there's a lot of leeway in how stuff can be written. This repository contains additional rules that revolve around strictly and strongly typed code with no loose casting for those who want additional safety in extremely defensive programming:

Additional rules are coming in subsequent releases!

Installation

To use this extension, require it in Composer:

composer require --dev phpstan/phpstan-strict-rules

If you also install phpstan/extension-installer then you're all set!

Manual installation If you don't want to use `phpstan/extension-installer`, include rules.neon in your project's PHPStan config: ``` includes: - vendor/phpstan/phpstan-strict-rules/rules.neon ```

Disabling rules

You can disable rules using configuration parameters:

parameters:
    strictRules:
        disallowedLooseComparison: false
        booleansInConditions: false
        uselessCast: false
        requireParentConstructorCall: false
        disallowedConstructs: false
        overwriteVariablesWithLoop: false
        closureUsesThis: false
        matchingInheritedMethodNames: false
        numericOperandsInArithmeticOperators: false
        strictCalls: false
        switchConditionsMatchingType: false
        noVariableVariables: false
        strictArrayFilter: false

Aside from introducing new custom rules, phpstan-strict-rules also change the default values of some configuration parameters that are present in PHPStan itself. These parameters are documented on phpstan.org.

Enabling rules one-by-one

If you don't want to start using all the available strict rules at once but only one or two, you can!

You can disable all rules from the included rules.neon with:

parameters:
    strictRules:
        allRules: false

Then you can re-enable individual rules with configuration parameters:

parameters:
    strictRules:
        allRules: false
        booleansInConditions: true

Even with strictRules.allRules set to false, part of this package is still in effect. That's because phpstan-strict-rules also change the default values of some configuration parameters that are present in PHPStan itself. These parameters are documented on phpstan.org.