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-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;
|
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
|
|
|
|
{
|
|
|
|
/**
|
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;
|
|
|
|
|
|
|
|
public function __construct(AssignAndBinaryMap $assignAndBinaryMap)
|
|
|
|
{
|
|
|
|
$this->assignAndBinaryMap = $assignAndBinaryMap;
|
|
|
|
}
|
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-10-15 12:35:45 +00:00
|
|
|
if ($node instanceof Identical) {
|
|
|
|
return $this->processIdenticalAndNotIdentical($node);
|
|
|
|
}
|
2018-10-27 14:02:49 +00:00
|
|
|
|
|
|
|
return null;
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
2018-10-12 23:32:15 +00:00
|
|
|
|
2018-10-21 10:19:14 +00:00
|
|
|
private function processBooleanNot(BooleanNot $node): ?Node
|
2018-10-15 12:35:45 +00:00
|
|
|
{
|
|
|
|
if (! $node->expr instanceof BinaryOp) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-12 23:32:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 12:35:45 +00:00
|
|
|
if ($this->shouldSkip($node->expr)) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->createInversedBooleanOp($node->expr);
|
|
|
|
}
|
|
|
|
|
2018-10-21 10:19:14 +00:00
|
|
|
private function processIdenticalAndNotIdentical(BinaryOp $node): ?Node
|
2018-10-15 12:35:45 +00:00
|
|
|
{
|
|
|
|
if ($node->left instanceof Identical || $node->left instanceof NotIdentical) {
|
|
|
|
$subBinaryOpNode = $node->left;
|
2018-10-17 11:01:33 +00:00
|
|
|
$shouldInverse = $this->isFalse($node->right);
|
2018-10-15 12:35:45 +00:00
|
|
|
} elseif ($node->right instanceof Identical || $node->right instanceof NotIdentical) {
|
|
|
|
$subBinaryOpNode = $node->right;
|
2018-10-17 11:01:33 +00:00
|
|
|
$shouldInverse = $this->isFalse($node->left);
|
2018-10-15 12:35:45 +00:00
|
|
|
} else {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($shouldInverse) {
|
|
|
|
return $this->createInversedBooleanOp($subBinaryOpNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $subBinaryOpNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip too nested binary || binary > binary combinations
|
|
|
|
*/
|
|
|
|
private function shouldSkip(BinaryOp $binaryOpNode): bool
|
|
|
|
{
|
|
|
|
if ($binaryOpNode->left instanceof BinaryOp) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-27 12:54:27 +00:00
|
|
|
return $binaryOpNode->right instanceof BinaryOp;
|
2018-10-12 23:02:47 +00:00
|
|
|
}
|
2018-10-31 15:34:37 +00:00
|
|
|
|
|
|
|
private function createInversedBooleanOp(BinaryOp $binaryOpNode): BinaryOp
|
|
|
|
{
|
|
|
|
$inversedBinaryClass = $this->assignAndBinaryMap->getInversed($binaryOpNode);
|
|
|
|
return new $inversedBinaryClass($binaryOpNode->left, $binaryOpNode->right);
|
|
|
|
}
|
2018-10-12 23:02:47 +00:00
|
|
|
}
|