Skip to content

Commit d3bb37d

Browse files
committed
[CodeQuality] Add NegatedAndsToOrRector for !(A && B) => !A || !B
1 parent 04e2b5f commit d3bb37d

10 files changed

Lines changed: 165 additions & 13 deletions

File tree

rules-tests/CodeQuality/Rector/BooleanNot/SimplifyDeMorganBinaryRector/Fixture/binary_and.php.inc renamed to rules-tests/CodeQuality/Rector/BooleanNot/NegatedAndsToOrRector/Fixture/empty_in_array.php.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector\Fixture;
3+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToOrRector\Fixture;
44

5-
class BinaryAnd
5+
class EmptyInArray
66
{
77
public static function validateAssetDownload($assetId, array $limitToAssets): bool
88
{
@@ -14,9 +14,9 @@ class BinaryAnd
1414
-----
1515
<?php
1616

17-
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector\Fixture;
17+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToOrRector\Fixture;
1818

19-
class BinaryAnd
19+
class EmptyInArray
2020
{
2121
public static function validateAssetDownload($assetId, array $limitToAssets): bool
2222
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToOrRector\Fixture;
4+
5+
class Fixture
6+
{
7+
public function run($a, $b, $c)
8+
{
9+
$result = !($a > 20 && $b <= 50);
10+
$d = !($a === null && $b < $c);
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToOrRector\Fixture;
19+
20+
class Fixture
21+
{
22+
public function run($a, $b, $c)
23+
{
24+
$result = $a <= 20 || $b > 50;
25+
$d = $a !== null || $b >= $c;
26+
}
27+
}
28+
29+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToOrRector\Fixture;
4+
5+
class SkipNestedAnd
6+
{
7+
public function run($a, $b, $c)
8+
{
9+
$result = !($a > 20 && $b < $c && $a !== null);
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToOrRector\Fixture;
4+
5+
class SkipOr
6+
{
7+
public function run($a, $b)
8+
{
9+
$result = !($a > 20 || $b <= 50);
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToOrRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class NegatedAndsToOrRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\BooleanNot\NegatedAndsToOrRector;
6+
use Rector\Config\RectorConfig;
7+
8+
return RectorConfig::configure()
9+
->withRules([NegatedAndsToOrRector::class]);

‎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;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\CodeQuality\Rector\BooleanNot;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\BinaryOp;
9+
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
10+
use PhpParser\Node\Expr\BooleanNot;
11+
use Rector\NodeManipulator\BinaryOpManipulator;
12+
use Rector\Rector\AbstractRector;
13+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
16+
/**
17+
* @see \Rector\Tests\CodeQuality\Rector\BooleanNot\NegatedAndsToOrRector\NegatedAndsToOrRectorTest
18+
*/
19+
final class NegatedAndsToOrRector extends AbstractRector
20+
{
21+
public function __construct(
22+
private readonly BinaryOpManipulator $binaryOpManipulator
23+
) {
24+
}
25+
26+
public function getRuleDefinition(): RuleDefinition
27+
{
28+
return new RuleDefinition(
29+
'Simplify negated "and" conditions to "or" with de Morgan theorem',
30+
[
31+
new CodeSample(
32+
<<<'CODE_SAMPLE'
33+
$a = 5;
34+
$b = 10;
35+
$result = !($a > 20 && $b <= 50);
36+
CODE_SAMPLE
37+
,
38+
<<<'CODE_SAMPLE'
39+
$a = 5;
40+
$b = 10;
41+
$result = $a <= 20 || $b > 50;
42+
CODE_SAMPLE
43+
),
44+
]
45+
);
46+
}
47+
48+
/**
49+
* @return array<class-string<Node>>
50+
*/
51+
public function getNodeTypes(): array
52+
{
53+
return [BooleanNot::class];
54+
}
55+
56+
/**
57+
* @param BooleanNot $node
58+
*/
59+
public function refactor(Node $node): ?BinaryOp
60+
{
61+
if (! $node->expr instanceof BooleanAnd) {
62+
return null;
63+
}
64+
65+
return $this->binaryOpManipulator->inverseBooleanAnd($node->expr);
66+
}
67+
}

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

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

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\BinaryOp;
9-
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
109
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
1110
use PhpParser\Node\Expr\BooleanNot;
1211
use Rector\NodeManipulator\BinaryOpManipulator;
@@ -59,14 +58,10 @@ public function getNodeTypes(): array
5958
*/
6059
public function refactor(Node $node): ?BinaryOp
6160
{
62-
if ($node->expr instanceof BooleanOr) {
63-
return $this->binaryOpManipulator->inverseBooleanOr($node->expr);
61+
if (! $node->expr instanceof BooleanOr) {
62+
return null;
6463
}
6564

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

‎src/Config/Level/CodeQualityLevel.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Rector\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector;
1111
use Rector\CodeQuality\Rector\BooleanAnd\RepeatedAndNotEqualToNotInArrayRector;
1212
use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
13+
use Rector\CodeQuality\Rector\BooleanNot\NegatedAndsToOrRector;
1314
use Rector\CodeQuality\Rector\BooleanNot\ReplaceConstantBooleanNotRector;
1415
use Rector\CodeQuality\Rector\BooleanNot\ReplaceMultipleBooleanNotRector;
1516
use Rector\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector;
@@ -132,6 +133,7 @@ final class CodeQualityLevel
132133
UnnecessaryTernaryExpressionRector::class,
133134
RemoveExtraParametersRector::class,
134135
SimplifyDeMorganBinaryRector::class,
136+
NegatedAndsToOrRector::class,
135137
SimplifyTautologyTernaryRector::class,
136138
SingleInArrayToCompareRector::class,
137139
SimplifyIfElseToTernaryRector::class,

0 commit comments

Comments
 (0)