2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Identical;
|
2018-10-12 23:02:47 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2022-09-03 08:03:15 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeManipulator\BinaryOpManipulator;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Php71\ValueObject\TwoNodeMatch;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\AssignAndBinaryMap;
|
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Identical\SimplifyConditionsRector\SimplifyConditionsRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SimplifyConditionsRector extends AbstractRector
|
2018-10-12 23:02:47 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\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
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\BinaryOpManipulator
|
2018-11-07 17:04:38 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $binaryOpManipulator;
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
public function __construct(AssignAndBinaryMap $assignAndBinaryMap, BinaryOpManipulator $binaryOpManipulator, ValueResolver $valueResolver)
|
2018-10-27 14:02:49 +00:00
|
|
|
{
|
|
|
|
$this->assignAndBinaryMap = $assignAndBinaryMap;
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->binaryOpManipulator = $binaryOpManipulator;
|
2023-09-20 12:53:23 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2018-10-27 14:02:49 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-12 23:02:47 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Simplify conditions', [new CodeSample("if (! (\$foo !== 'bar')) {...", "if (\$foo === 'bar') {...")]);
|
2018-10-12 23:02:47 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-12 23:02:47 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-12 23:02:47 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [BooleanNot::class, Identical::class];
|
2018-10-12 23:02:47 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param BooleanNot|Identical $node
|
2018-10-12 23:02:47 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-12 23:02:47 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof BooleanNot) {
|
2018-10-15 12:35:45 +00:00
|
|
|
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
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processBooleanNot(BooleanNot $booleanNot) : ?Node
|
2018-10-15 12:35:45 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +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
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processIdenticalAndNotIdentical(Identical $identical) : ?Node
|
2018-10-15 12:35:45 +00:00
|
|
|
{
|
2022-08-23 16:16:02 +00:00
|
|
|
$twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode($identical, static function (Node $node) : bool {
|
|
|
|
return $node instanceof Identical || $node instanceof NotIdentical;
|
|
|
|
}, function (Node $node) : bool {
|
2022-09-03 08:03:15 +00:00
|
|
|
return $node instanceof Expr && $this->valueResolver->isTrueOrFalse($node);
|
2021-05-09 20:15:43 +00:00
|
|
|
});
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$twoNodeMatch instanceof TwoNodeMatch) {
|
2020-08-23 09:39:09 +00:00
|
|
|
return $twoNodeMatch;
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
2022-07-18 15:25:27 +00:00
|
|
|
/** @var Identical|NotIdentical $firstExpr */
|
|
|
|
$firstExpr = $twoNodeMatch->getFirstExpr();
|
|
|
|
$otherExpr = $twoNodeMatch->getSecondExpr();
|
|
|
|
if ($this->valueResolver->isFalse($otherExpr)) {
|
|
|
|
return $this->createInversedBooleanOp($firstExpr);
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
2022-07-18 15:25:27 +00:00
|
|
|
return $firstExpr;
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Skip too nested binary || binary > binary combinations
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkip(BinaryOp $binaryOp) : bool
|
2018-10-15 12:35:45 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($binaryOp instanceof BooleanOr) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2018-11-07 01:37:39 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($binaryOp->left instanceof BinaryOp) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2018-10-15 12:35:45 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return $binaryOp->right instanceof BinaryOp;
|
2018-10-12 23:02:47 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +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
|
|
|
}
|