rector/rules/CodeQuality/Rector/Identical/SimplifyConditionsRector.php

107 lines
4.3 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +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\Core\NodeManipulator\BinaryOpManipulator;
use Rector\Core\PhpParser\Node\AssignAndBinaryMap;
use Rector\Core\Rector\AbstractRector;
use Rector\Php71\ValueObject\TwoNodeMatch;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Tests\CodeQuality\Rector\Identical\SimplifyConditionsRector\SimplifyConditionsRectorTest
2019-09-03 09:11:45 +00:00
*/
final class SimplifyConditionsRector extends \Rector\Core\Rector\AbstractRector
2018-10-12 23:02:47 +00:00
{
/**
* @var AssignAndBinaryMap
2018-10-12 23:02:47 +00:00
*/
private $assignAndBinaryMap;
2018-11-07 17:04:38 +00:00
/**
* @var BinaryOpManipulator
2018-11-07 17:04:38 +00:00
*/
private $binaryOpManipulator;
public function __construct(\Rector\Core\PhpParser\Node\AssignAndBinaryMap $assignAndBinaryMap, \Rector\Core\NodeManipulator\BinaryOpManipulator $binaryOpManipulator)
{
$this->assignAndBinaryMap = $assignAndBinaryMap;
$this->binaryOpManipulator = $binaryOpManipulator;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2018-10-12 23:02:47 +00:00
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Simplify conditions', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample("if (! (\$foo !== 'bar')) {...", "if (\$foo === 'bar') {...")]);
2018-10-12 23:02:47 +00:00
}
/**
* @return array<class-string<Node>>
2018-10-12 23:02:47 +00:00
*/
public function getNodeTypes() : array
2018-10-12 23:02:47 +00:00
{
return [\PhpParser\Node\Expr\BooleanNot::class, \PhpParser\Node\Expr\BinaryOp\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(\PhpParser\Node $node) : ?\PhpParser\Node
2018-10-12 23:02:47 +00:00
{
if ($node instanceof \PhpParser\Node\Expr\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);
}
private function processBooleanNot(\PhpParser\Node\Expr\BooleanNot $booleanNot) : ?\PhpParser\Node
{
if (!$booleanNot->expr instanceof \PhpParser\Node\Expr\BinaryOp) {
return null;
2018-10-12 23:32:15 +00:00
}
2019-02-22 17:25:31 +00:00
if ($this->shouldSkip($booleanNot->expr)) {
return null;
}
2019-02-22 17:25:31 +00:00
return $this->createInversedBooleanOp($booleanNot->expr);
}
private function processIdenticalAndNotIdentical(\PhpParser\Node\Expr\BinaryOp\Identical $identical) : ?\PhpParser\Node
{
$twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode($identical, function (\PhpParser\Node $binaryOp) : bool {
return $binaryOp instanceof \PhpParser\Node\Expr\BinaryOp\Identical || $binaryOp instanceof \PhpParser\Node\Expr\BinaryOp\NotIdentical;
}, function (\PhpParser\Node $binaryOp) : bool {
return $this->valueResolver->isTrueOrFalse($binaryOp);
});
if (!$twoNodeMatch instanceof \Rector\Php71\ValueObject\TwoNodeMatch) {
return $twoNodeMatch;
}
2019-02-22 17:25:31 +00:00
/** @var Identical|NotIdentical $subBinaryOp */
$subBinaryOp = $twoNodeMatch->getFirstExpr();
$otherNode = $twoNodeMatch->getSecondExpr();
2021-01-30 23:20:05 +00:00
if ($this->valueResolver->isFalse($otherNode)) {
2019-02-22 17:25:31 +00:00
return $this->createInversedBooleanOp($subBinaryOp);
}
2019-02-22 17:25:31 +00:00
return $subBinaryOp;
}
/**
* Skip too nested binary || binary > binary combinations
*/
private function shouldSkip(\PhpParser\Node\Expr\BinaryOp $binaryOp) : bool
{
if ($binaryOp instanceof \PhpParser\Node\Expr\BinaryOp\BooleanOr) {
return \true;
2018-11-07 01:37:39 +00:00
}
if ($binaryOp->left instanceof \PhpParser\Node\Expr\BinaryOp) {
return \true;
}
return $binaryOp->right instanceof \PhpParser\Node\Expr\BinaryOp;
2018-10-12 23:02:47 +00:00
}
private function createInversedBooleanOp(\PhpParser\Node\Expr\BinaryOp $binaryOp) : ?\PhpParser\Node\Expr\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
}