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

123 lines
3.6 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-10-12 23:32:15 +00:00
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
2018-10-12 23:32:15 +00:00
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
2018-10-12 23:02:47 +00:00
use PhpParser\Node\Expr\BooleanNot;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class SimplifyConditionsRector extends AbstractRector
{
/**
* @var string[]
*/
private $binaryOpClassesToInversedClasses = [
2018-10-12 23:32:15 +00:00
Identical::class => NotIdentical::class,
NotIdentical::class => Identical::class,
Equal::class => NotEqual::class,
NotEqual::class => Equal::class,
Greater::class => SmallerOrEqual::class,
Smaller::class => GreaterOrEqual::class,
GreaterOrEqual::class => Smaller::class,
SmallerOrEqual::class => Greater::class,
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
}
if ($node instanceof Identical) {
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
{
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);
} 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);
} else {
return null;
}
if ($shouldInverse) {
return $this->createInversedBooleanOp($subBinaryOpNode);
}
return $subBinaryOpNode;
}
private function createInversedBooleanOp(BinaryOp $binaryOpNode): BinaryOp
{
$binaryOpNodeClass = get_class($binaryOpNode);
// we can't invert that
if (! isset($this->binaryOpClassesToInversedClasses[$binaryOpNodeClass])) {
return $binaryOpNode;
}
$inversedBinaryOpNodeClass = $this->binaryOpClassesToInversedClasses[$binaryOpNodeClass];
return new $inversedBinaryOpNodeClass($binaryOpNode->left, $binaryOpNode->right);
}
/**
* Skip too nested binary || binary > binary combinations
*/
private function shouldSkip(BinaryOp $binaryOpNode): bool
{
if ($binaryOpNode->left instanceof BinaryOp) {
return true;
}
return $binaryOpNode->right instanceof BinaryOp;
2018-10-12 23:02:47 +00:00
}
}