Skip to content

Commit 88e714c

Browse files
committed
BooleanInBooleanAndRule, BooleanInBooleanOrRule - different identifier and description for logical operators
1 parent 477f53a commit 88e714c

File tree

6 files changed

+57
-14
lines changed

6 files changed

+57
-14
lines changed

rules.neon

+4
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,16 @@ services:
142142

143143
-
144144
class: PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule
145+
arguments:
146+
bleedingEdge: %featureToggles.bleedingEdge%
145147

146148
-
147149
class: PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule
148150

149151
-
150152
class: PHPStan\Rules\BooleansInConditions\BooleanInBooleanOrRule
153+
arguments:
154+
bleedingEdge: %featureToggles.bleedingEdge%
151155

152156
-
153157
class: PHPStan\Rules\BooleansInConditions\BooleanInElseIfConditionRule

src/Rules/BooleansInConditions/BooleanInBooleanAndRule.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ class BooleanInBooleanAndRule implements Rule
1919
/** @var BooleanRuleHelper */
2020
private $helper;
2121

22-
public function __construct(BooleanRuleHelper $helper)
22+
/** @var bool */
23+
private $bleedingEdge;
24+
25+
public function __construct(BooleanRuleHelper $helper, bool $bleedingEdge)
2326
{
2427
$this->helper = $helper;
28+
$this->bleedingEdge = $bleedingEdge;
2529
}
2630

2731
public function getNodeType(): string
@@ -33,21 +37,25 @@ public function processNode(Node $node, Scope $scope): array
3337
{
3438
$originalNode = $node->getOriginalNode();
3539
$messages = [];
40+
$nodeText = $this->bleedingEdge ? $originalNode->getOperatorSigil() : '&&';
41+
$identifierType = $originalNode instanceof Node\Expr\BinaryOp\BooleanAnd ? 'booleanAnd' : 'logicalAnd';
3642
if (!$this->helper->passesAsBoolean($scope, $originalNode->left)) {
3743
$leftType = $scope->getType($originalNode->left);
3844
$messages[] = RuleErrorBuilder::message(sprintf(
39-
'Only booleans are allowed in &&, %s given on the left side.',
45+
'Only booleans are allowed in %s, %s given on the left side.',
46+
$nodeText,
4047
$leftType->describe(VerbosityLevel::typeOnly())
41-
))->identifier('booleanAnd.leftNotBoolean')->build();
48+
))->identifier(sprintf('%s.leftNotBoolean', $identifierType))->build();
4249
}
4350

4451
$rightScope = $node->getRightScope();
4552
if (!$this->helper->passesAsBoolean($rightScope, $originalNode->right)) {
4653
$rightType = $rightScope->getType($originalNode->right);
4754
$messages[] = RuleErrorBuilder::message(sprintf(
48-
'Only booleans are allowed in &&, %s given on the right side.',
55+
'Only booleans are allowed in %s, %s given on the right side.',
56+
$nodeText,
4957
$rightType->describe(VerbosityLevel::typeOnly())
50-
))->identifier('booleanAnd.rightNotBoolean')->build();
58+
))->identifier(sprintf('%s.rightNotBoolean', $identifierType))->build();
5159
}
5260

5361
return $messages;

src/Rules/BooleansInConditions/BooleanInBooleanOrRule.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ class BooleanInBooleanOrRule implements Rule
1919
/** @var BooleanRuleHelper */
2020
private $helper;
2121

22-
public function __construct(BooleanRuleHelper $helper)
22+
/** @var bool */
23+
private $bleedingEdge;
24+
25+
public function __construct(BooleanRuleHelper $helper, bool $bleedingEdge)
2326
{
2427
$this->helper = $helper;
28+
$this->bleedingEdge = $bleedingEdge;
2529
}
2630

2731
public function getNodeType(): string
@@ -33,21 +37,25 @@ public function processNode(Node $node, Scope $scope): array
3337
{
3438
$originalNode = $node->getOriginalNode();
3539
$messages = [];
40+
$nodeText = $this->bleedingEdge ? $originalNode->getOperatorSigil() : '||';
41+
$identifierType = $originalNode instanceof Node\Expr\BinaryOp\BooleanOr ? 'booleanOr' : 'logicalOr';
3642
if (!$this->helper->passesAsBoolean($scope, $originalNode->left)) {
3743
$leftType = $scope->getType($originalNode->left);
3844
$messages[] = RuleErrorBuilder::message(sprintf(
39-
'Only booleans are allowed in ||, %s given on the left side.',
45+
'Only booleans are allowed in %s, %s given on the left side.',
46+
$nodeText,
4047
$leftType->describe(VerbosityLevel::typeOnly())
41-
))->identifier('booleanOr.leftNotBoolean')->build();
48+
))->identifier(sprintf('%s.leftNotBoolean', $identifierType))->build();
4249
}
4350

4451
$rightScope = $node->getRightScope();
4552
if (!$this->helper->passesAsBoolean($rightScope, $originalNode->right)) {
4653
$rightType = $rightScope->getType($originalNode->right);
4754
$messages[] = RuleErrorBuilder::message(sprintf(
48-
'Only booleans are allowed in ||, %s given on the right side.',
55+
'Only booleans are allowed in %s, %s given on the right side.',
56+
$nodeText,
4957
$rightType->describe(VerbosityLevel::typeOnly())
50-
))->identifier('booleanOr.rightNotBoolean')->build();
58+
))->identifier(sprintf('%s.rightNotBoolean', $identifierType))->build();
5159
}
5260

5361
return $messages;

tests/Rules/BooleansInConditions/BooleanInBooleanAndRuleTest.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ protected function getRule(): Rule
1717
return new BooleanInBooleanAndRule(
1818
new BooleanRuleHelper(
1919
self::getContainer()->getByType(RuleLevelHelper::class)
20-
)
20+
),
21+
true
2122
);
2223
}
2324

@@ -44,6 +45,14 @@ public function testRule(): void
4445
'Only booleans are allowed in &&, mixed given on the right side.',
4546
19,
4647
],
48+
[
49+
'Only booleans are allowed in and, mixed given on the right side.',
50+
47,
51+
],
52+
[
53+
'Only booleans are allowed in and, mixed given on the left side.',
54+
48,
55+
],
4756
]);
4857
}
4958

@@ -61,11 +70,11 @@ public function testLogicalAnd(): void
6170
{
6271
$this->analyse([__DIR__ . '/data/logical-and.php'], [
6372
[
64-
'Only booleans are allowed in &&, string|false given on the left side.',
73+
'Only booleans are allowed in and, string|false given on the left side.',
6574
14,
6675
],
6776
[
68-
'Only booleans are allowed in &&, mixed given on the right side.',
77+
'Only booleans are allowed in and, mixed given on the right side.',
6978
14,
7079
],
7180
]);

tests/Rules/BooleansInConditions/BooleanInBooleanOrRuleTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ protected function getRule(): Rule
1717
return new BooleanInBooleanOrRule(
1818
new BooleanRuleHelper(
1919
self::getContainer()->getByType(RuleLevelHelper::class)
20-
)
20+
),
21+
true
2122
);
2223
}
2324

@@ -40,6 +41,14 @@ public function testRule(): void
4041
'Only booleans are allowed in ||, mixed given on the right side.',
4142
29,
4243
],
44+
[
45+
'Only booleans are allowed in or, mixed given on the right side.',
46+
49,
47+
],
48+
[
49+
'Only booleans are allowed in or, mixed given on the left side.',
50+
50,
51+
],
4352
]);
4453
}
4554

tests/Rules/BooleansInConditions/data/conditions.php

+5
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@
4343
$bool ? 1 : 2;
4444
$string ? 1 : 2;
4545
$string ?: null;
46+
47+
$bool and $explicitMixed;
48+
$explicitMixed and $bool;
49+
$bool or $explicitMixed;
50+
$explicitMixed or $bool;

0 commit comments

Comments
 (0)