2020-01-05 12:10:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-12-12 16:59:09 +00:00
|
|
|
namespace Rector\EarlyReturn\Rector\If_;
|
2020-01-05 12:10:01 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
2020-01-12 21:44:42 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
2020-01-05 21:19:26 +00:00
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
2020-01-05 12:10:01 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2021-02-08 12:33:17 +00:00
|
|
|
use Rector\Core\NodeManipulator\IfManipulator;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-12-12 16:59:09 +00:00
|
|
|
use Rector\EarlyReturn\NodeTransformer\ConditionInverter;
|
2020-01-05 12:10:01 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-01-05 12:10:01 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-12 16:59:09 +00:00
|
|
|
* @see \Rector\EarlyReturn\Tests\Rector\If_\ChangeNestedIfsToEarlyReturnRector\ChangeNestedIfsToEarlyReturnRectorTest
|
2020-01-05 12:10:01 +00:00
|
|
|
*/
|
|
|
|
final class ChangeNestedIfsToEarlyReturnRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var IfManipulator
|
|
|
|
*/
|
|
|
|
private $ifManipulator;
|
|
|
|
|
|
|
|
/**
|
2020-02-22 12:35:26 +00:00
|
|
|
* @var ConditionInverter
|
2020-01-05 12:10:01 +00:00
|
|
|
*/
|
2020-02-22 12:35:26 +00:00
|
|
|
private $conditionInverter;
|
2020-01-05 12:10:01 +00:00
|
|
|
|
2020-07-26 07:49:22 +00:00
|
|
|
public function __construct(ConditionInverter $conditionInverter, IfManipulator $ifManipulator)
|
2020-01-05 12:10:01 +00:00
|
|
|
{
|
|
|
|
$this->ifManipulator = $ifManipulator;
|
2020-02-22 12:35:26 +00:00
|
|
|
$this->conditionInverter = $conditionInverter;
|
2020-01-05 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2020-01-05 12:10:01 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Change nested ifs to early return', [
|
2020-01-05 12:10:01 +00:00
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2020-01-05 12:10:01 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if ($value === 5) {
|
|
|
|
if ($value2 === 10) {
|
|
|
|
return 'yes';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'no';
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-01-05 12:10:01 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2020-01-05 12:10:01 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if ($value !== 5) {
|
|
|
|
return 'no';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value2 === 10) {
|
|
|
|
return 'yes';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'no';
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-01-05 12:10:01 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-01-05 12:10:01 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [If_::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param If_ $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
// A. next node is return
|
|
|
|
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
|
|
|
|
if (! $nextNode instanceof Return_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nestedIfsWithOnlyReturn = $this->ifManipulator->collectNestedIfsWithOnlyReturn($node);
|
|
|
|
if ($nestedIfsWithOnlyReturn === []) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-02-22 12:35:26 +00:00
|
|
|
$this->processNestedIfsWithOnlyReturn($node, $nestedIfsWithOnlyReturn, $nextNode);
|
2020-01-05 17:19:27 +00:00
|
|
|
$this->removeNode($node);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param If_[] $nestedIfsWithOnlyReturn
|
|
|
|
*/
|
2020-02-22 12:35:26 +00:00
|
|
|
private function processNestedIfsWithOnlyReturn(If_ $if, array $nestedIfsWithOnlyReturn, Return_ $nextReturn): void
|
2020-01-05 17:19:27 +00:00
|
|
|
{
|
2020-01-05 12:10:01 +00:00
|
|
|
// add nested if openly after this
|
|
|
|
$nestedIfsWithOnlyReturnCount = count($nestedIfsWithOnlyReturn);
|
|
|
|
|
2020-01-05 17:19:27 +00:00
|
|
|
/** @var int $key */
|
2020-01-05 12:10:01 +00:00
|
|
|
foreach ($nestedIfsWithOnlyReturn as $key => $nestedIfWithOnlyReturn) {
|
|
|
|
// last item → the return node
|
|
|
|
if ($nestedIfsWithOnlyReturnCount === $key + 1) {
|
2020-01-05 17:19:27 +00:00
|
|
|
$this->addNodeAfterNode($nestedIfWithOnlyReturn, $if);
|
2020-01-05 12:10:01 +00:00
|
|
|
} else {
|
2020-01-05 21:19:26 +00:00
|
|
|
$this->addStandaloneIfsWithReturn($nestedIfWithOnlyReturn, $if, $nextReturn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-05 17:19:27 +00:00
|
|
|
|
2020-01-05 21:19:26 +00:00
|
|
|
private function addStandaloneIfsWithReturn(If_ $nestedIfWithOnlyReturn, If_ $if, Return_ $return): void
|
|
|
|
{
|
|
|
|
$return = clone $return;
|
|
|
|
|
2020-02-22 12:35:26 +00:00
|
|
|
$invertedCondition = $this->conditionInverter->createInvertedCondition($nestedIfWithOnlyReturn->cond);
|
2020-01-05 21:19:26 +00:00
|
|
|
|
|
|
|
// special case
|
2020-01-19 19:45:01 +00:00
|
|
|
if ($invertedCondition instanceof BooleanNot && $invertedCondition->expr instanceof BooleanAnd) {
|
|
|
|
$booleanNotPartIf = new If_(new BooleanNot($invertedCondition->expr->left));
|
|
|
|
$booleanNotPartIf->stmts = [clone $return];
|
|
|
|
$this->addNodeAfterNode($booleanNotPartIf, $if);
|
2020-07-04 23:58:13 +00:00
|
|
|
|
2020-01-19 19:45:01 +00:00
|
|
|
$booleanNotPartIf = new If_(new BooleanNot($invertedCondition->expr->right));
|
|
|
|
$booleanNotPartIf->stmts = [clone $return];
|
|
|
|
$this->addNodeAfterNode($booleanNotPartIf, $if);
|
|
|
|
return;
|
2020-01-05 12:10:01 +00:00
|
|
|
}
|
2020-01-05 21:19:26 +00:00
|
|
|
|
|
|
|
$nestedIfWithOnlyReturn->cond = $invertedCondition;
|
|
|
|
$nestedIfWithOnlyReturn->stmts = [clone $return];
|
|
|
|
|
|
|
|
$this->addNodeAfterNode($nestedIfWithOnlyReturn, $if);
|
2020-01-05 12:10:01 +00:00
|
|
|
}
|
|
|
|
}
|