Skip to content

Commit a581fea

Browse files
committed
include negated nested in Demorgan binary
1 parent a181f3f commit a581fea

4 files changed

Lines changed: 53 additions & 4 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector\Fixture;
4+
5+
class BinaryAnd
6+
{
7+
public static function validateAssetDownload($assetId, array $limitToAssets): bool
8+
{
9+
return !(!empty($limitToAssets) && !in_array($assetId, $limitToAssets));
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector\Fixture;
18+
19+
class BinaryAnd
20+
{
21+
public static function validateAssetDownload($assetId, array $limitToAssets): bool
22+
{
23+
return empty($limitToAssets) || in_array($assetId, $limitToAssets);
24+
}
25+
}
26+
27+
?>

‎rules-tests/CodeQuality/Rector/BooleanNot/SimplifyDeMorganBinaryRector/Fixture/fixture.php.inc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function deMorgan()
2626

2727
$c = 1000;
2828
$d = $a !== null && $b >= $c;
29-
$d = !($a === null && $b < $c);
29+
$d = $a !== null || $b >= $c;
3030

3131
$e = $a && $b === $c;
3232
$f = $a <= $b && $c > $d;

‎rules/CodeQuality/Rector/BooleanNot/SimplifyDeMorganBinaryRector.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\BinaryOp;
9+
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
910
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
1011
use PhpParser\Node\Expr\BooleanNot;
1112
use Rector\NodeManipulator\BinaryOpManipulator;
@@ -58,10 +59,14 @@ public function getNodeTypes(): array
5859
*/
5960
public function refactor(Node $node): ?BinaryOp
6061
{
61-
if (! $node->expr instanceof BooleanOr) {
62-
return null;
62+
if ($node->expr instanceof BooleanOr) {
63+
return $this->binaryOpManipulator->inverseBooleanOr($node->expr);
6364
}
6465

65-
return $this->binaryOpManipulator->inverseBooleanOr($node->expr);
66+
if ($node->expr instanceof BooleanAnd) {
67+
return $this->binaryOpManipulator->inverseBooleanAnd($node->expr);
68+
}
69+
70+
return null;
6671
}
6772
}

‎src/NodeManipulator/BinaryOpManipulator.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ public function inverseBooleanOr(BooleanOr $booleanOr): ?BinaryOp
7676
return new $inversedNodeClass($firstInversedExpr, $secondInversedExpr);
7777
}
7878

79+
public function inverseBooleanAnd(BooleanAnd $booleanAnd): ?BinaryOp
80+
{
81+
// no nesting
82+
if ($booleanAnd->left instanceof BooleanAnd) {
83+
return null;
84+
}
85+
86+
if ($booleanAnd->right instanceof BooleanAnd) {
87+
return null;
88+
}
89+
90+
$firstInversedExpr = $this->inverseNode($booleanAnd->left);
91+
$secondInversedExpr = $this->inverseNode($booleanAnd->right);
92+
93+
return new BooleanOr($firstInversedExpr, $secondInversedExpr);
94+
}
95+
7996
public function invertCondition(BinaryOp $binaryOp): ?BinaryOp
8097
{
8198
// no nesting

0 commit comments

Comments
 (0)