Skip to content

Commit

Permalink
Bleeding edge - check LogicalAnd + LogicalOr for booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 11, 2021
1 parent f820776 commit ee00093
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 8 deletions.
16 changes: 14 additions & 2 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [];
}

Expand Down
17 changes: 16 additions & 1 deletion tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ protected function getRule(): Rule
false,
true
)
)
),
true
);
}

Expand Down Expand Up @@ -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,
],
]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ protected function getRule(): Rule
false,
true
)
)
),
true
);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Rules/BooleansInConditions/data/logical-and.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace BooleanInLogicalAnd;

class HelloWorld
{
/** @return string|false */
public function returnsStringOrFalse(){
return false;
}
public function sayHello(): void
{
if(
($response = $this->returnsStringOrFalse())
and ($ip_geolocation_data = json_decode($response, true))
and ($ip_geolocation_data['status'] !== 'fail')
and (date_default_timezone_set($ip_geolocation_data['timezone']))
){

}
}
}

0 comments on commit ee00093

Please sign in to comment.