From ee0009302ff5312ca6b92a7330a6b66da0c70f12 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 11 Jan 2021 14:32:43 +0100 Subject: [PATCH] Bleeding edge - check LogicalAnd + LogicalOr for booleans --- rules.neon | 16 ++++++++++++-- .../BooleanInBooleanAndRule.php | 8 +++++-- .../BooleanInBooleanOrRule.php | 8 +++++-- .../BooleanInBooleanAndRuleTest.php | 17 +++++++++++++- .../BooleanInBooleanOrRuleTest.php | 3 ++- .../BooleansInConditions/data/logical-and.php | 22 +++++++++++++++++++ 6 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 tests/Rules/BooleansInConditions/data/logical-and.php diff --git a/rules.neon b/rules.neon index b4768f71..e737eaa8 100644 --- a/rules.neon +++ b/rules.neon @@ -11,9 +11,7 @@ parameters: reportStaticMethodSignatures: true rules: - - PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule - PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule - - PHPStan\Rules\BooleansInConditions\BooleanInBooleanOrRule - PHPStan\Rules\BooleansInConditions\BooleanInElseIfConditionRule - PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule - PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule @@ -46,6 +44,20 @@ conditionalTags: phpstan.rules.rule: %featureToggles.bleedingEdge% services: + - + class: PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule + arguments: + checkLogicalAndConstantCondition: %featureToggles.checkLogicalAndConstantCondition% + tags: + - phpstan.rules.rule + + - + class: PHPStan\Rules\BooleansInConditions\BooleanInBooleanOrRule + arguments: + checkLogicalOrConstantCondition: %featureToggles.checkLogicalOrConstantCondition% + tags: + - phpstan.rules.rule + - class: PHPStan\Rules\BooleansInConditions\BooleanRuleHelper diff --git a/src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php b/src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php index be661639..c2de6c1c 100644 --- a/src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php +++ b/src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php @@ -15,9 +15,13 @@ class BooleanInBooleanAndRule implements \PHPStan\Rules\Rule /** @var BooleanRuleHelper */ private $helper; - public function __construct(BooleanRuleHelper $helper) + /** @var bool */ + private $checkLogicalAndConstantCondition; + + public function __construct(BooleanRuleHelper $helper, bool $checkLogicalAndConstantCondition) { $this->helper = $helper; + $this->checkLogicalAndConstantCondition = $checkLogicalAndConstantCondition; } public function getNodeType(): string @@ -28,7 +32,7 @@ public function getNodeType(): string public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array { $originalNode = $node->getOriginalNode(); - if (!$originalNode instanceof BooleanAnd) { + if (!$originalNode instanceof BooleanAnd && !$this->checkLogicalAndConstantCondition) { return []; } diff --git a/src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php b/src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php index deb64152..de59ca2d 100644 --- a/src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php +++ b/src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php @@ -15,9 +15,13 @@ class BooleanInBooleanOrRule implements \PHPStan\Rules\Rule /** @var BooleanRuleHelper */ private $helper; - public function __construct(BooleanRuleHelper $helper) + /** @var bool */ + private $checkLogicalOrConstantCondition; + + public function __construct(BooleanRuleHelper $helper, bool $checkLogicalOrConstantCondition) { $this->helper = $helper; + $this->checkLogicalOrConstantCondition = $checkLogicalOrConstantCondition; } public function getNodeType(): string @@ -28,7 +32,7 @@ public function getNodeType(): string public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array { $originalNode = $node->getOriginalNode(); - if (!$originalNode instanceof BooleanOr) { + if (!$originalNode instanceof BooleanOr && !$this->checkLogicalOrConstantCondition) { return []; } diff --git a/tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php b/tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php index 4df87758..d0173664 100644 --- a/tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php +++ b/tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php @@ -21,7 +21,8 @@ protected function getRule(): Rule false, true ) - ) + ), + true ); } @@ -61,4 +62,18 @@ public function testBug104(): void ]); } + public function testLogicalAnd(): void + { + $this->analyse([__DIR__ . '/data/logical-and.php'], [ + [ + 'Only booleans are allowed in &&, string|false given on the left side.', + 14, + ], + [ + 'Only booleans are allowed in &&, mixed given on the right side.', + 14, + ], + ]); + } + } diff --git a/tests/Rules/BooleansInConditions/BooleanInBooleanOrRuleTest.php b/tests/Rules/BooleansInConditions/BooleanInBooleanOrRuleTest.php index 3c8ccaf7..9ddef28b 100644 --- a/tests/Rules/BooleansInConditions/BooleanInBooleanOrRuleTest.php +++ b/tests/Rules/BooleansInConditions/BooleanInBooleanOrRuleTest.php @@ -21,7 +21,8 @@ protected function getRule(): Rule false, true ) - ) + ), + true ); } diff --git a/tests/Rules/BooleansInConditions/data/logical-and.php b/tests/Rules/BooleansInConditions/data/logical-and.php new file mode 100644 index 00000000..88cb90d9 --- /dev/null +++ b/tests/Rules/BooleansInConditions/data/logical-and.php @@ -0,0 +1,22 @@ +returnsStringOrFalse()) + and ($ip_geolocation_data = json_decode($response, true)) + and ($ip_geolocation_data['status'] !== 'fail') + and (date_default_timezone_set($ip_geolocation_data['timezone'])) + ){ + + } + } +}