-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check incompatible operand values for greater/smaller operands (boole…
…an value for greater / smaller comparison is not allowed).
- Loading branch information
1 parent
b7edb14
commit 7b569cc
Showing
4 changed files
with
243 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
120 changes: 120 additions & 0 deletions
120
src/Rules/Operators/OperandsIncompatibleGreaterSmallerRule.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,120 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Operators; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\BinaryOp\Greater as BinaryOpGreater; | ||
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual as BinaryOpGreaterOrEqual; | ||
use PhpParser\Node\Expr\BinaryOp\Smaller as BinaryOpSmaller; | ||
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual as BinaryOpSmallerOrEqual; | ||
use PhpParser\Node\Expr\BinaryOp\Spaceship as BinaryOpSpaceship; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\UnionType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<Expr> | ||
*/ | ||
class OperandsIncompatibleGreaterSmallerRule implements Rule | ||
{ | ||
|
||
/** @var bool */ | ||
private $bleedingEdge; | ||
|
||
public function __construct(bool $bleedingEdge) | ||
{ | ||
$this->bleedingEdge = $bleedingEdge; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Expr::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$this->bleedingEdge) { | ||
return []; | ||
} | ||
|
||
if (!$this->nodeIsSpaceship($node) | ||
&& !$this->nodeIsGreater($node) | ||
&& !$this->nodeIsSmaller($node) | ||
) { | ||
return []; | ||
} | ||
|
||
$leftType = $scope->getType($node->left); | ||
$rightType = $scope->getType($node->right); | ||
|
||
if ($this->nodeIsSpaceship($node) && $leftType->isBoolean()->yes() && $rightType->isBoolean()->yes()) { | ||
return []; | ||
} | ||
|
||
if ($leftType->isInteger()->yes() && $this->nodeIsSmaller($node) && $this->containsBoolean($rightType)) { | ||
return []; | ||
} | ||
|
||
if ($rightType->isInteger()->yes() && $this->nodeIsGreater($node) && $this->containsBoolean($leftType)) { | ||
return []; | ||
} | ||
|
||
if ($this->containsBoolean($leftType) || $this->containsBoolean($rightType)) { | ||
return [RuleErrorBuilder::message(sprintf( | ||
'Comparison operator "%s" between %s and %s is not allowed.', | ||
$node->getOperatorSigil(), | ||
$leftType->describe(VerbosityLevel::typeOnly()), | ||
$rightType->describe(VerbosityLevel::typeOnly()) | ||
))->identifier('cmp.hasBool')->build()]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
private function containsBoolean(Type $type): bool | ||
{ | ||
if ($type->isBoolean()->yes()) { | ||
return true; | ||
} | ||
|
||
if ($type instanceof UnionType) { | ||
foreach ($type->getTypes() as $inUnionType) { | ||
if ($inUnionType->isBoolean()->yes()) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @phpstan-assert-if-true BinaryOpSpaceship $node | ||
*/ | ||
private function nodeIsSpaceship(Expr $node): bool | ||
{ | ||
return $node instanceof BinaryOpSpaceship; | ||
} | ||
|
||
/** | ||
* @phpstan-assert-if-true BinaryOpGreater|BinaryOpGreaterOrEqual $node | ||
*/ | ||
private function nodeIsGreater(Expr $node): bool | ||
{ | ||
return $node instanceof BinaryOpGreater || $node instanceof BinaryOpGreaterOrEqual; | ||
} | ||
|
||
/** | ||
* @phpstan-assert-if-true BinaryOpSmaller|BinaryOpSmallerOrEqual $node | ||
*/ | ||
private function nodeIsSmaller(Expr $node): bool | ||
{ | ||
return $node instanceof BinaryOpSmaller || $node instanceof BinaryOpSmallerOrEqual; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
tests/Rules/Operators/OperandsIncompatibleGreaterSmallerRuleTest.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,63 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Operators; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<OperandsIncompatibleGreaterSmallerRule> | ||
*/ | ||
class OperandsIncompatibleGreaterSmallerRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new OperandsIncompatibleGreaterSmallerRule( | ||
true | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/greater-smaller.php'], [ | ||
[ | ||
'Comparison operator ">" between bool and bool is not allowed.', | ||
24, | ||
], | ||
[ | ||
'Comparison operator ">" between int and bool is not allowed.', | ||
25, | ||
], | ||
[ | ||
'Comparison operator ">" between int and int|false is not allowed.', | ||
26, | ||
], | ||
[ | ||
'Comparison operator "<=" between bool|int and int is not allowed.', | ||
29, | ||
], | ||
[ | ||
'Comparison operator "<=" between bool|int and string is not allowed.', | ||
30, | ||
], | ||
[ | ||
'Comparison operator "<=" between int|false and int is not allowed.', | ||
32, | ||
], | ||
[ | ||
'Comparison operator "<=" between int|false and string is not allowed.', | ||
33, | ||
], | ||
[ | ||
'Comparison operator "<=>" between int and int|false is not allowed.', | ||
40, | ||
], | ||
[ | ||
'Comparison operator "<=>" between int|false and int is not allowed.', | ||
41, | ||
], | ||
]); | ||
} | ||
|
||
} |
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,51 @@ | ||
<?php | ||
|
||
namespace Operators; | ||
|
||
use stdClass; | ||
|
||
$int = 123; | ||
$string = '123'; | ||
$object = new stdClass(); | ||
$object2 = new stdClass(); | ||
|
||
/** @var bool $boolean */ | ||
$boolean = foob1(); | ||
|
||
/** @var bool $boolean2 */ | ||
$boolean2 = foob2(); | ||
|
||
/** @var int|false $intOrFalse */ | ||
$intOrFalse = foo(); | ||
|
||
/** @var int|bool $intOrBoolean */ | ||
$intOrBoolean = foo2(); | ||
|
||
$boolean > $boolean2; | ||
$int > $boolean; | ||
$int > $intOrFalse; | ||
|
||
$int <= $intOrBoolean; | ||
$intOrBoolean <= $int; | ||
$intOrBoolean <= $string; | ||
|
||
$intOrFalse <= $int; | ||
$intOrFalse <= $string; | ||
|
||
$object <= $object2; | ||
|
||
for ($i = 0; $i < $intOrFalse; $i++) { | ||
} | ||
|
||
$int <=> $intOrFalse; | ||
$intOrFalse <=> $int; | ||
|
||
$int < $int; | ||
|
||
preg_match_all('~\d+~', 'foo', $matches) > 0; | ||
|
||
filesize('foo') > 0; | ||
0 < filesize('foo'); | ||
|
||
$intOrFalse >= $int; | ||
|