2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\BooleanNot;
|
2018-12-08 22:21:42 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeManipulator\BinaryOpManipulator;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-08 22:21:42 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://stackoverflow.com/questions/20043664/de-morgans-law
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\BooleanNot\SimplifyDeMorganBinaryRector\SimplifyDeMorganBinaryRectorTest
|
2018-12-08 22:21:42 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SimplifyDeMorganBinaryRector extends AbstractRector
|
2018-12-08 22:21:42 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\BinaryOpManipulator
|
2018-12-08 22:21:42 +00:00
|
|
|
*/
|
2020-01-05 12:10:01 +00:00
|
|
|
private $binaryOpManipulator;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(BinaryOpManipulator $binaryOpManipulator)
|
2018-12-08 22:21:42 +00:00
|
|
|
{
|
2020-01-05 12:10:01 +00:00
|
|
|
$this->binaryOpManipulator = $binaryOpManipulator;
|
2018-12-08 22:21:42 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-08 22:21:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Simplify negated conditions with de Morgan theorem', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-08 22:21:42 +00:00
|
|
|
$a = 5;
|
|
|
|
$b = 10;
|
|
|
|
$result = !($a > 20 || $b <= 50);
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-08 22:21:42 +00:00
|
|
|
$a = 5;
|
|
|
|
$b = 10;
|
|
|
|
$result = $a <= 20 && $b > 50;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-08 22:21:42 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-08 22:21:42 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-08 22:21:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [BooleanNot::class];
|
2018-12-08 22:21:42 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param BooleanNot $node
|
2018-12-08 22:21:42 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?BinaryOp
|
2018-12-08 22:21:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->expr instanceof BooleanOr) {
|
2021-01-13 11:48:46 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-14 22:29:28 +00:00
|
|
|
return $this->binaryOpManipulator->inverseBooleanOr($node->expr);
|
2018-12-08 22:21:42 +00:00
|
|
|
}
|
|
|
|
}
|