Skip to content

Commit ee00093

Browse files
committed
Bleeding edge - check LogicalAnd + LogicalOr for booleans
1 parent f820776 commit ee00093

File tree

6 files changed

+66
-8
lines changed

6 files changed

+66
-8
lines changed

rules.neon

+14-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ parameters:
1111
reportStaticMethodSignatures: true
1212

1313
rules:
14-
- PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule
1514
- PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule
16-
- PHPStan\Rules\BooleansInConditions\BooleanInBooleanOrRule
1715
- PHPStan\Rules\BooleansInConditions\BooleanInElseIfConditionRule
1816
- PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule
1917
- PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule
@@ -46,6 +44,20 @@ conditionalTags:
4644
phpstan.rules.rule: %featureToggles.bleedingEdge%
4745

4846
services:
47+
-
48+
class: PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule
49+
arguments:
50+
checkLogicalAndConstantCondition: %featureToggles.checkLogicalAndConstantCondition%
51+
tags:
52+
- phpstan.rules.rule
53+
54+
-
55+
class: PHPStan\Rules\BooleansInConditions\BooleanInBooleanOrRule
56+
arguments:
57+
checkLogicalOrConstantCondition: %featureToggles.checkLogicalOrConstantCondition%
58+
tags:
59+
- phpstan.rules.rule
60+
4961
-
5062
class: PHPStan\Rules\BooleansInConditions\BooleanRuleHelper
5163

src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ class BooleanInBooleanAndRule implements \PHPStan\Rules\Rule
1515
/** @var BooleanRuleHelper */
1616
private $helper;
1717

18-
public function __construct(BooleanRuleHelper $helper)
18+
/** @var bool */
19+
private $checkLogicalAndConstantCondition;
20+
21+
public function __construct(BooleanRuleHelper $helper, bool $checkLogicalAndConstantCondition)
1922
{
2023
$this->helper = $helper;
24+
$this->checkLogicalAndConstantCondition = $checkLogicalAndConstantCondition;
2125
}
2226

2327
public function getNodeType(): string
@@ -28,7 +32,7 @@ public function getNodeType(): string
2832
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
2933
{
3034
$originalNode = $node->getOriginalNode();
31-
if (!$originalNode instanceof BooleanAnd) {
35+
if (!$originalNode instanceof BooleanAnd && !$this->checkLogicalAndConstantCondition) {
3236
return [];
3337
}
3438

src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ class BooleanInBooleanOrRule implements \PHPStan\Rules\Rule
1515
/** @var BooleanRuleHelper */
1616
private $helper;
1717

18-
public function __construct(BooleanRuleHelper $helper)
18+
/** @var bool */
19+
private $checkLogicalOrConstantCondition;
20+
21+
public function __construct(BooleanRuleHelper $helper, bool $checkLogicalOrConstantCondition)
1922
{
2023
$this->helper = $helper;
24+
$this->checkLogicalOrConstantCondition = $checkLogicalOrConstantCondition;
2125
}
2226

2327
public function getNodeType(): string
@@ -28,7 +32,7 @@ public function getNodeType(): string
2832
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
2933
{
3034
$originalNode = $node->getOriginalNode();
31-
if (!$originalNode instanceof BooleanOr) {
35+
if (!$originalNode instanceof BooleanOr && !$this->checkLogicalOrConstantCondition) {
3236
return [];
3337
}
3438

tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ protected function getRule(): Rule
2121
false,
2222
true
2323
)
24-
)
24+
),
25+
true
2526
);
2627
}
2728

@@ -61,4 +62,18 @@ public function testBug104(): void
6162
]);
6263
}
6364

65+
public function testLogicalAnd(): void
66+
{
67+
$this->analyse([__DIR__ . '/data/logical-and.php'], [
68+
[
69+
'Only booleans are allowed in &&, string|false given on the left side.',
70+
14,
71+
],
72+
[
73+
'Only booleans are allowed in &&, mixed given on the right side.',
74+
14,
75+
],
76+
]);
77+
}
78+
6479
}

tests/Rules/BooleansInConditions/BooleanInBooleanOrRuleTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ protected function getRule(): Rule
2121
false,
2222
true
2323
)
24-
)
24+
),
25+
true
2526
);
2627
}
2728

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace BooleanInLogicalAnd;
4+
5+
class HelloWorld
6+
{
7+
/** @return string|false */
8+
public function returnsStringOrFalse(){
9+
return false;
10+
}
11+
public function sayHello(): void
12+
{
13+
if(
14+
($response = $this->returnsStringOrFalse())
15+
and ($ip_geolocation_data = json_decode($response, true))
16+
and ($ip_geolocation_data['status'] !== 'fail')
17+
and (date_default_timezone_set($ip_geolocation_data['timezone']))
18+
){
19+
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)