rector/packages/CodeQuality/src/Rector/Identical/SimplifyConditionsRector.php

126 lines
3.4 KiB
PHP
Raw Normal View History

2018-10-12 23:02:47 +00:00
<?php declare(strict_types=1);
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;
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;
use Rector\PhpParser\Node\AssignAndBinaryMap;
2018-11-07 17:04:38 +00:00
use Rector\PhpParser\Node\Maintainer\BinaryOpMaintainer;
2018-10-12 23:02:47 +00:00
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class SimplifyConditionsRector extends AbstractRector
{
/**
* @var AssignAndBinaryMap
2018-10-12 23:02:47 +00:00
*/
private $assignAndBinaryMap;
2018-11-07 17:04:38 +00:00
/**
* @var BinaryOpMaintainer
*/
private $binaryOpMaintainer;
public function __construct(AssignAndBinaryMap $assignAndBinaryMap, BinaryOpMaintainer $binaryOpMaintainer)
{
$this->assignAndBinaryMap = $assignAndBinaryMap;
2018-11-07 17:04:38 +00:00
$this->binaryOpMaintainer = $binaryOpMaintainer;
}
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
{
return [BooleanNot::class, Identical::class];
2018-10-12 23:02:47 +00:00
}
/**
* @param BooleanNot|Identical $node
2018-10-12 23:02:47 +00:00
*/
public function refactor(Node $node): ?Node
2018-10-12 23:02:47 +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-12 23:32:15 +00:00
private function processBooleanNot(BooleanNot $node): ?Node
{
if (! $node->expr instanceof BinaryOp) {
return null;
2018-10-12 23:32:15 +00:00
}
if ($this->shouldSkip($node->expr)) {
return null;
}
return $this->createInversedBooleanOp($node->expr);
}
private function processIdenticalAndNotIdentical(BinaryOp $node): ?Node
{
2018-11-07 17:04:38 +00:00
$matchedNodes = $this->binaryOpMaintainer->matchFirstAndSecondConditionNode(
$node,
function (Node $node) {
return $node instanceof Identical || $node instanceof NotIdentical;
2018-11-07 17:04:38 +00:00
},
function (Node $node) {
return $this->isBool($node);
}
2018-11-07 17:04:38 +00:00
);
if ($matchedNodes === null) {
return $matchedNodes;
}
2018-11-07 17:04:38 +00:00
/** @var Identical|NotIdentical $subBinaryOpNode */
[$subBinaryOpNode, $otherNode] = $matchedNodes;
if ($this->isFalse($otherNode)) {
return $this->createInversedBooleanOp($subBinaryOpNode);
}
return $subBinaryOpNode;
}
/**
* Skip too nested binary || binary > binary combinations
*/
private function shouldSkip(BinaryOp $binaryOpNode): bool
{
2018-11-07 01:37:39 +00:00
if ($binaryOpNode instanceof BooleanOr) {
return true;
}
if ($binaryOpNode->left instanceof BinaryOp) {
return true;
}
return $binaryOpNode->right instanceof BinaryOp;
2018-10-12 23:02:47 +00:00
}
2018-10-31 15:34:37 +00:00
2018-11-05 00:56:27 +00:00
private function createInversedBooleanOp(BinaryOp $binaryOpNode): ?BinaryOp
2018-10-31 15:34:37 +00:00
{
$inversedBinaryClass = $this->assignAndBinaryMap->getInversed($binaryOpNode);
2018-11-05 00:56:27 +00:00
if ($inversedBinaryClass === null) {
return null;
}
2018-10-31 15:34:37 +00:00
return new $inversedBinaryClass($binaryOpNode->left, $binaryOpNode->right);
}
2018-10-12 23:02:47 +00:00
}