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\Ternary;
|
2018-12-25 17:51:46 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
|
|
use PhpParser\Node\Expr\Ternary;
|
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\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\Ternary\SimplifyTautologyTernaryRector\SimplifyTautologyTernaryRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SimplifyTautologyTernaryRector extends AbstractRector
|
2018-12-25 17:51:46 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\NodeManipulator\BinaryOpManipulator
|
2018-12-25 17:51:46 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $binaryOpManipulator;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(BinaryOpManipulator $binaryOpManipulator)
|
2018-12-25 17:51:46 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->binaryOpManipulator = $binaryOpManipulator;
|
2018-12-25 17:51:46 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-25 17:51:46 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Simplify tautology ternary to value', [new CodeSample('$value = ($fullyQualifiedTypeHint !== $typeHint) ? $fullyQualifiedTypeHint : $typeHint;', '$value = $fullyQualifiedTypeHint;')]);
|
2018-12-25 17:51:46 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-25 17:51:46 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-25 17:51:46 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Ternary::class];
|
2018-12-25 17:51:46 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Ternary $node
|
2018-12-25 17:51:46 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-12-25 17:51:46 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->cond instanceof NotIdentical && !$node->cond instanceof Identical) {
|
2018-12-25 17:51:46 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$twoNodeMatch = $this->binaryOpManipulator->matchFirstAndSecondConditionNode($node->cond, function (Node $leftNode) use($node) : bool {
|
2021-05-09 20:15:43 +00:00
|
|
|
return $this->nodeComparator->areNodesEqual($leftNode, $node->if);
|
2022-06-07 08:22:29 +00:00
|
|
|
}, function (Node $leftNode) use($node) : bool {
|
2021-05-09 20:15:43 +00:00
|
|
|
return $this->nodeComparator->areNodesEqual($leftNode, $node->else);
|
|
|
|
});
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$twoNodeMatch instanceof TwoNodeMatch) {
|
2018-12-25 17:51:46 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-05-02 08:08:51 +00:00
|
|
|
return $node->cond instanceof NotIdentical ? $node->if : $node->else;
|
2018-12-25 17:51:46 +00:00
|
|
|
}
|
|
|
|
}
|