-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bleeding edge level 4 - ConstantLooseComparisonRule
- Loading branch information
1 parent
49b8b26
commit 6ebf236
Showing
6 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,65 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Comparison; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\BinaryOp> | ||
*/ | ||
class ConstantLooseComparisonRule implements Rule | ||
{ | ||
|
||
public function __construct(private bool $checkAlwaysTrueLooseComparison) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\BinaryOp::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node instanceof Node\Expr\BinaryOp\Equal && !$node instanceof Node\Expr\BinaryOp\NotEqual) { | ||
return []; | ||
} | ||
|
||
$nodeType = $scope->getType($node); | ||
if (!$nodeType instanceof ConstantBooleanType) { | ||
return []; | ||
} | ||
|
||
$leftType = $scope->getType($node->left); | ||
$rightType = $scope->getType($node->right); | ||
|
||
if (!$nodeType->getValue()) { | ||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Loose comparison using %s between %s and %s will always evaluate to false.', | ||
$node instanceof Node\Expr\BinaryOp\Equal ? '==' : '!=', | ||
$leftType->describe(VerbosityLevel::value()), | ||
$rightType->describe(VerbosityLevel::value()), | ||
))->build(), | ||
]; | ||
} elseif ($this->checkAlwaysTrueLooseComparison) { | ||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Loose comparison using %s between %s and %s will always evaluate to true.', | ||
$node instanceof Node\Expr\BinaryOp\Equal ? '==' : '!=', | ||
$leftType->describe(VerbosityLevel::value()), | ||
$rightType->describe(VerbosityLevel::value()), | ||
))->build(), | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
tests/PHPStan/Rules/Comparison/ConstantLooseComparisonRuleTest.php
This file contains 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,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Comparison; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ConstantLooseComparisonRule> | ||
*/ | ||
class ConstantLooseComparisonRuleTest extends RuleTestCase | ||
{ | ||
|
||
private bool $checkAlwaysTrueStrictComparison; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new ConstantLooseComparisonRule($this->checkAlwaysTrueStrictComparison); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->checkAlwaysTrueStrictComparison = false; | ||
$this->analyse([__DIR__ . '/data/loose-comparison.php'], [ | ||
[ | ||
"Loose comparison using == between 0 and '1' will always evaluate to false.", | ||
20, | ||
], | ||
]); | ||
} | ||
|
||
public function testRuleAlwaysTrue(): void | ||
{ | ||
$this->checkAlwaysTrueStrictComparison = true; | ||
$this->analyse([__DIR__ . '/data/loose-comparison.php'], [ | ||
[ | ||
"Loose comparison using == between 0 and '0' will always evaluate to true.", | ||
16, | ||
], | ||
[ | ||
"Loose comparison using == between 0 and '1' will always evaluate to false.", | ||
20, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains 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,25 @@ | ||
<?php | ||
|
||
namespace ConstantLooseComparison; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(string $s, string $i): void | ||
{ | ||
if ($s == $i) { | ||
|
||
} | ||
if ($s != $i) { | ||
|
||
} | ||
if (0 == "0") { | ||
|
||
} | ||
|
||
if (0 == "1") { | ||
|
||
} | ||
} | ||
|
||
} |