voku/phpstan-rules
Additional PHPStan rules to catch risky and redundant condition logic and comparisons. Detects double negatives, PHP 8 behavior changes (0 vs ''), insane/invalid comparisons (0=='0foo', 0==='0'), and type-mismatch checks (e.g., object vs non-object).
Provides additional rules for phpstan/phpstan.
Run
$ composer require --dev voku/phpstan-rules
All the rules provided (and used) by this library are included in rules.neon.
When you are using phpstan/extension-installer, rules.neon will be automatically included.
Otherwise, you need to include rules.neon in your phpstan.neon:
includes:
- vendor/voku/phpstan-rules/rules.neon
This helper is used by different "condition"-rules: if - and - or - not - ternary
:bulb: We use this "hack" (helper) to run the check for all kind of conditions.
(string)$foo != '' is the same as (string)$foo
(int)$foo != 0 is the same as (int)$foo
(bool)$foo != false is the same as (bool)$foo
0 == '0foo', the behavior was changed in PHP 8
0 === '0' or false && trueif ($a = 0) (see "checkForAssignments")ìf (0 == $a) (see "checkYodaConditions")If you want to configure a list of classes / subclasses that can NOT be used in conditions directly:
e.g.:
if ($emailValueObject->isValid())if ($emailValueObject != '')parameters:
voku:
classesNotInIfConditions: [
AbstractValueObject
]
If you want to check assignments e.g. in "if"-conditions you can use this:
parameters:
voku:
checkForAssignments: true
If you want to check Yoda conditions can use this:
parameters:
voku:
checkYodaConditions: true
This rule will check "+", "*", "/", "-", ... (operators) and "." (concatenation) for compatible types.
It's included in the default rules.neon so that you don't need to add it manually.
This code is copy&pasted from [phpstan/phpstan-src] and I used it to prevent Call to a member function on null errors while I wasn't already on level 8 where all kind of "NULL" checks are already covered by default.
e.g.
rules:
- voku\PHPStan\Rules\DisallowedCallMethodOnNullRule
For support and donations please visit Github | Issues | PayPal | Patreon.
For status updates and release announcements please visit Releases | Twitter | Patreon.
For professional support please contact me.
How can I help you explore Laravel packages today?