-
Notifications
You must be signed in to change notification settings - Fork 54
Added rule to disallow weak comparisons #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
87417a7
Added rule to disallow weak comparisons
nagmat84 98202c4
Code style
nagmat84 c19efb5
Added test
nagmat84 385c020
Added PhpStan errors in test class to baseline.
nagmat84 02152d2
Reverted PhpStan baseline and added template parameter to test class
nagmat84 56a7475
Applied suggested changes from code review.
nagmat84 e2401eb
Renamed classes and files
nagmat84 ecc05ce
Renamed forgotten references
nagmat84 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\DisallowedConstructs; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\BinaryOp; | ||
| use PhpParser\Node\Expr\BinaryOp\Equal; | ||
| use PhpParser\Node\Expr\BinaryOp\NotEqual; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
|
|
||
| /** | ||
| * @implements Rule<BinaryOp> | ||
| */ | ||
| class DisallowedLooseComparisonRule implements Rule | ||
| { | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return BinaryOp::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if ($node instanceof Equal) { | ||
| return [ | ||
| RuleErrorBuilder::message( | ||
| 'Loose comparison via "==" is not allowed.' | ||
| )->tip('Use strict comparison via "===" instead.')->build(), | ||
| ]; | ||
| } | ||
| if ($node instanceof NotEqual) { | ||
| return [ | ||
| RuleErrorBuilder::message( | ||
| 'Loose comparison via "!=" is not allowed.' | ||
| )->tip('Use strict comparison via "!==" instead.')->build(), | ||
| ]; | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
tests/Rules/DisallowedConstructs/DisallowedLooseComparisonRuleTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\DisallowedConstructs; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<DisallowedLooseComparisonRule> | ||
| */ | ||
| class DisallowedLooseComparisonRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new DisallowedLooseComparisonRule(); | ||
| } | ||
|
|
||
| public function testRule(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/weak-comparison.php'], [ | ||
| [ | ||
| 'Loose comparison via "==" is not allowed.', | ||
| 3, | ||
| 'Use strict comparison via "===" instead.', | ||
| ], | ||
| [ | ||
| 'Loose comparison via "!=" is not allowed.', | ||
| 5, | ||
| 'Use strict comparison via "!==" instead.', | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <?php | ||
|
|
||
| $bool1 = 123 == 456; | ||
| $bool2 = 123 === 456; | ||
| $bool3 = 123 != 456; | ||
| $bool4 = 123 !== 456; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes We are using bleeding edge to test
@readonlyand stuff in phpstan, but this rule would create thousands of errors for us in legacy apps.wouldn‘t it make sense to have a additional feature flag, so one can enable bleeding edge but don‘t enable this rule?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either we want to have this enabled for everyone when PHPStan 2.0 + phpstan-strict-rules 2.0 are released, or we don't.
In the first case (enable for everyone), the additional feature toggle doesn't make sense. Bleeding edge is a preview of what's to come in the next major version.
I made a Twitter poll about this: https://twitter.com/OndrejMirtes/status/1537418180888514560
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW - instead of setting a feature toggle to false or whatever, you can always write a regex to ignore these problems.