Stars: 505
Forks: 42
Pull Requests: 103
Issues: 115
Watchers: 20
Last Updated: 2023-09-11 09:44:57
Extra strict and opinionated rules for PHPStan
License: MIT License
Languages: PHP, Makefile
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:
if
, elseif
, ternary operator, after !
, and on both sides of &&
and ||
.+
and numeric operands in -
/*
//
/**
/%
.$var++
, $var--
, ++$var
and --$var
.$strict
parameter for better type safety, it must be set to true
:
in_array
(3rd parameter)array_search
(3rd parameter)array_keys
(3rd parameter; only if the 2nd parameter $search_value
is provided)base64_decode
(2nd parameter)while
loop condition and for
loop initial assignment cannot be used after the loop.switch
condition and case
value must match. PHP compares them loosely by default and that can lead to unexpected results.empty()
- it's a very loose comparison (see manual), it's recommended to use more strict one.?:
) - implies weak comparison, it's recommended to use null coalesce operator (??
) or ternary operator with strict condition.$$foo
, $this->$method()
etc.)instanceof
, type-checking is_*
functions and strict comparisons ===
/!==
. These checks can be turned off by setting checkAlwaysTrueInstanceof
/checkAlwaysTrueCheckTypeFunctionCall
/checkAlwaysTrueStrictComparison
to false.$ls = `ls -la`
)$this
directly instead of using $this
variable indirectlyAdditional rules are coming in subsequent releases!
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!
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
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
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