2018-10-12 23:02:47 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2018-10-17 12:27:22 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Identical;
|
2018-10-12 23:02:47 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
2018-11-07 01:37:39 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
2018-10-13 09:35:23 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
2018-10-12 23:02:47 +00:00
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
2018-11-04 01:17:02 +00:00
|
|
|
use Rector\PhpParser\Node\AssignAndBinaryMap;
|
2019-02-27 21:54:39 +00:00
|
|
|
use Rector\PhpParser\Node\Manipulator\BinaryOpManipulator;
|
2018-10-12 23:02:47 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\RectorDefinition\CodeSample;
|
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\Identical\SimplifyConditionsRector\SimplifyConditionsRectorTest
|
|
|
|
*/
|
2018-10-12 23:02:47 +00:00
|
|
|
final class SimplifyConditionsRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
2018-10-27 14:02:49 +00:00
|
|
|
* @var AssignAndBinaryMap
|
2018-10-12 23:02:47 +00:00
|
|
|
*/
|
2018-10-27 14:02:49 +00:00
|
|
|
private $assignAndBinaryMap;
|
|
|
|
|
2018-11-07 17:04:38 +00:00
|
|
|
/**
|
2019-02-27 21:54:39 +00:00
|
|
|
* @var BinaryOpManipulator
|
2018-11-07 17:04:38 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $binaryOpManipulator;
|
2018-11-07 17:04:38 +00:00
|
|
|
|
2019-02-27 21:54:39 +00:00
|
|
|
public function __construct(AssignAndBinaryMap $assignAndBinaryMap, BinaryOpManipulator $binaryOpManipulator)
|
2018-10-27 14:02:49 +00:00
|
|
|
{
|
|
|
|
$this->assignAndBinaryMap = $assignAndBinaryMap;
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->binaryOpManipulator = $binaryOpManipulator;
|
2018-10-27 14:02:49 +00:00
|
|
|
}
|
2018-10-12 23:02:47 +00:00
|
|
|
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition(
|
|
|
|
'Simplify conditions',
|
|
|
|
[new CodeSample("if (! (\$foo !== 'bar')) {...", "if (\$foo === 'bar') {...")]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2018-10-15 12:35:45 +00:00
|
|
|
return [BooleanNot::class, Identical::class];
|
2018-10-12 23:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-15 12:35:45 +00:00
|
|
|
* @param BooleanNot|Identical $node
|
2018-10-12 23:02:47 +00:00
|
|
|
*/
|
2018-10-15 12:35:45 +00:00
|
|
|
public function refactor(Node $node): ?Node
|
2018-10-12 23:02:47 +00:00
|
|
|
{
|
2018-10-15 12:35:45 +00:00
|
|
|
if ($node instanceof BooleanNot) {
|
|
|
|
return $this->processBooleanNot($node);
|
2018-10-12 23:02:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 17:22:27 +00:00
|
|
|
return $this->processIdenticalAndNotIdentical($node);
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
2018-10-12 23:32:15 +00:00
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
private function processBooleanNot(BooleanNot $booleanNot): ?Node
|
2018-10-15 12:35:45 +00:00
|
|
|
{
|
2019-02-22 17:25:31 +00:00
|
|
|
if (! $booleanNot->expr instanceof BinaryOp) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-12 23:32:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
if ($this->shouldSkip($booleanNot->expr)) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
return $this->createInversedBooleanOp($booleanNot->expr);
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
private function processIdenticalAndNotIdentical(BinaryOp $binaryOp): ?Node
|
2018-10-15 12:35:45 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$matchedNodes = $this->binaryOpManipulator->matchFirstAndSecondConditionNode(
|
2019-02-22 17:25:31 +00:00
|
|
|
$binaryOp,
|
2019-05-09 13:30:39 +00:00
|
|
|
function (Node $binaryOp): bool {
|
2019-02-22 17:25:31 +00:00
|
|
|
return $binaryOp instanceof Identical || $binaryOp instanceof NotIdentical;
|
2018-11-07 17:04:38 +00:00
|
|
|
},
|
2019-05-09 13:30:39 +00:00
|
|
|
function (Node $binaryOp): bool {
|
2019-02-22 17:25:31 +00:00
|
|
|
return $this->isBool($binaryOp);
|
2018-12-31 20:12:17 +00:00
|
|
|
}
|
2018-11-07 17:04:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($matchedNodes === null) {
|
|
|
|
return $matchedNodes;
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
/** @var Identical|NotIdentical $subBinaryOp */
|
|
|
|
[$subBinaryOp, $otherNode] = $matchedNodes;
|
2018-11-07 17:04:38 +00:00
|
|
|
if ($this->isFalse($otherNode)) {
|
2019-02-22 17:25:31 +00:00
|
|
|
return $this->createInversedBooleanOp($subBinaryOp);
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
return $subBinaryOp;
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip too nested binary || binary > binary combinations
|
|
|
|
*/
|
2019-02-22 17:25:31 +00:00
|
|
|
private function shouldSkip(BinaryOp $binaryOp): bool
|
2018-10-15 12:35:45 +00:00
|
|
|
{
|
2019-02-22 17:25:31 +00:00
|
|
|
if ($binaryOp instanceof BooleanOr) {
|
2018-11-07 01:37:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
if ($binaryOp->left instanceof BinaryOp) {
|
2018-10-15 12:35:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-02-22 17:25:31 +00:00
|
|
|
return $binaryOp->right instanceof BinaryOp;
|
2018-10-12 23:02:47 +00:00
|
|
|
}
|
2018-10-31 15:34:37 +00:00
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
private function createInversedBooleanOp(BinaryOp $binaryOp): ?BinaryOp
|
2018-10-31 15:34:37 +00:00
|
|
|
{
|
2019-02-22 17:25:31 +00:00
|
|
|
$inversedBinaryClass = $this->assignAndBinaryMap->getInversed($binaryOp);
|
2018-11-05 00:56:27 +00:00
|
|
|
if ($inversedBinaryClass === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
return new $inversedBinaryClass($binaryOp->left, $binaryOp->right);
|
2018-10-31 15:34:37 +00:00
|
|
|
}
|
2018-10-12 23:02:47 +00:00
|
|
|
}
|