voku / phpstan-rules
Provides additional rules for phpstan/phpstan.
Fund package maintenance!
voku
Patreon
Tidelift
www.paypal.me/moelleken
Installs: 47 890
Dependents: 4
Suggesters: 0
Security: 0
Stars: 28
Watchers: 3
Forks: 1
Open Issues: 6
Type:phpstan-extension
Requires
- php: ^7.2 || ^8.0
- phpstan/phpstan: ^1.9.0
Requires (Dev)
- phpstan/phpstan-phpunit: ^1.1.0
- phpunit/phpunit: ~7.0 || ~9.0
- dev-master
- 3.5.0
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.12
- 3.1.11
- 3.1.10
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.0
- 2.0.0
- 1.10.0
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.0
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.0
- dev-renovate/actions-cache-4.x
- dev-renovate/codecov-codecov-action-5.x
- dev-renovate/major-phpstan-packages
- dev-renovate/phpunit-phpunit-11.x
This package is auto-updated.
Last update: 2024-12-05 19:13:03 UTC
README
phpstan-rules
Provides additional rules for phpstan/phpstan
.
Installation
Run
$ composer require --dev voku/phpstan-rules
Usage
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
Rules
IfConditionHelper
This helper is used by different "condition"-rules: if - and - or - not - ternary
💡 We use this "hack" (helper) to run the check for all kind of conditions.
- double negative string conditions. e.g.
(string)$foo != ''
is the same as(string)$foo
- double negative integer conditions. e.g.
(int)$foo != 0
is the same as(int)$foo
- double negative boolean conditions. e.g.
(bool)$foo != false
is the same as(bool)$foo
- double negative null conditions. Use "!==" instead if needed
- check 0 vs '' conditions, the behavior was changed in PHP 8
- check possible insane comparisons. e.g.
0 == '0foo'
, the behavior was changed in PHP 8 - check insane comparisons. e.g.
0 === '0'
orfalse && true
- check non-empty string is never empty
- check non-empty string is always empty
- check non-empty array is never empty
- do not compare objects with another type
- do not use magic string-concat for objects with "__toString()"
- do not allow assignments. e.g.
if ($a = 0)
(see "checkForAssignments") - do not allow Yoda conditions. e.g.
ìf (0 == $a)
(see "checkYodaConditions")
Configuration
If you want to configure a list of classes / subclasses that can NOT be used in conditions directly:
e.g.:
- ok:
if ($emailValueObject->isValid())
- error:
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
ExtendedBinaryOpRule
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.
DisallowedCallMethodOnNull
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
Support
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.
Thanks
- Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
- Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
- Thanks to Travis CI for being the most awesome, easiest continous integration tool out there!
- Thanks to StyleCI for the simple but powerfull code style check.
- Thanks to PHPStan && Psalm for relly great Static analysis tools and for discover bugs in the code!