2020-01-05 12:10:01 +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\EarlyReturn\Rector\If_;
|
2020-01-05 12:10:01 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
|
|
|
|
use Rector\Core\NodeManipulator\IfManipulator;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\EarlyReturn\NodeTransformer\ConditionInverter;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-01-05 12:10:01 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\EarlyReturn\Rector\If_\ChangeNestedIfsToEarlyReturnRector\ChangeNestedIfsToEarlyReturnRectorTest
|
2020-01-05 12:10:01 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ChangeNestedIfsToEarlyReturnRector extends AbstractRector
|
2020-01-05 12:10:01 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\EarlyReturn\NodeTransformer\ConditionInverter
|
2020-01-05 12:10:01 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $conditionInverter;
|
2020-01-05 12:10:01 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\NodeManipulator\IfManipulator
|
2020-01-05 12:10:01 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $ifManipulator;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ConditionInverter $conditionInverter, IfManipulator $ifManipulator)
|
2020-01-05 12:10:01 +00:00
|
|
|
{
|
2020-02-22 12:35:26 +00:00
|
|
|
$this->conditionInverter = $conditionInverter;
|
2021-05-10 23:39:21 +00:00
|
|
|
$this->ifManipulator = $ifManipulator;
|
2020-01-05 12:10:01 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-01-05 12:10:01 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change nested ifs to early return', [new CodeSample(<<<'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
|
2021-05-09 20:15:43 +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
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
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
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-01-05 12:10:01 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2020-01-05 12:10:01 +00:00
|
|
|
}
|
2022-05-08 20:57:50 +00:00
|
|
|
/**
|
|
|
|
* @param StmtsAwareInterface $node
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-01-05 12:10:01 +00:00
|
|
|
{
|
2022-05-07 09:28:12 +00:00
|
|
|
$stmts = $node->stmts;
|
|
|
|
if ($stmts === null) {
|
2020-01-05 12:10:01 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-05-07 09:28:12 +00:00
|
|
|
foreach ($stmts as $key => $stmt) {
|
|
|
|
$nextStmt = $stmts[$key + 1] ?? null;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$nextStmt instanceof Return_) {
|
2022-05-07 09:28:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$stmt instanceof If_) {
|
2022-05-07 09:28:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$nestedIfsWithOnlyReturn = $this->ifManipulator->collectNestedIfsWithOnlyReturn($stmt);
|
|
|
|
if ($nestedIfsWithOnlyReturn === []) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$node->stmts = $this->processNestedIfsWithOnlyReturn($nestedIfsWithOnlyReturn, $nextStmt);
|
|
|
|
return $node;
|
2020-01-05 12:10:01 +00:00
|
|
|
}
|
2020-01-05 17:19:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param If_[] $nestedIfsWithOnlyReturn
|
2022-05-07 09:28:12 +00:00
|
|
|
* @return Stmt[]
|
2020-01-05 17:19:27 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processNestedIfsWithOnlyReturn(array $nestedIfsWithOnlyReturn, Return_ $nextReturn) : array
|
2020-01-05 17:19:27 +00:00
|
|
|
{
|
2020-01-05 12:10:01 +00:00
|
|
|
// add nested if openly after this
|
2021-05-09 20:15:43 +00:00
|
|
|
$nestedIfsWithOnlyReturnCount = \count($nestedIfsWithOnlyReturn);
|
2022-05-07 09:28:12 +00:00
|
|
|
$newStmts = [];
|
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) {
|
2022-05-07 09:28:12 +00:00
|
|
|
$newStmts[] = $nestedIfWithOnlyReturn;
|
2020-01-05 12:10:01 +00:00
|
|
|
} else {
|
2022-05-07 09:28:12 +00:00
|
|
|
$standaloneIfs = $this->createStandaloneIfsWithReturn($nestedIfWithOnlyReturn, $nextReturn);
|
|
|
|
$newStmts = \array_merge($newStmts, $standaloneIfs);
|
2020-01-05 21:19:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-07 09:28:12 +00:00
|
|
|
$newStmts[] = $nextReturn;
|
|
|
|
return $newStmts;
|
2020-01-05 21:19:26 +00:00
|
|
|
}
|
2022-05-07 09:28:12 +00:00
|
|
|
/**
|
|
|
|
* @return Stmt[]
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createStandaloneIfsWithReturn(If_ $nestedIfWithOnlyReturn, Return_ $return) : array
|
2020-01-05 21:19:26 +00:00
|
|
|
{
|
2022-07-18 15:25:27 +00:00
|
|
|
$invertedCondExpr = $this->conditionInverter->createInvertedCondition($nestedIfWithOnlyReturn->cond);
|
2020-01-05 21:19:26 +00:00
|
|
|
// special case
|
2022-07-18 15:25:27 +00:00
|
|
|
if ($invertedCondExpr instanceof BooleanNot && $invertedCondExpr->expr instanceof BooleanAnd) {
|
|
|
|
$booleanNotPartIf = new If_(new BooleanNot($invertedCondExpr->expr->left));
|
2020-01-19 19:45:01 +00:00
|
|
|
$booleanNotPartIf->stmts = [clone $return];
|
2022-07-18 15:25:27 +00:00
|
|
|
$secondBooleanNotPartIf = new If_(new BooleanNot($invertedCondExpr->expr->right));
|
2021-11-24 20:49:18 +00:00
|
|
|
$secondBooleanNotPartIf->stmts = [clone $return];
|
2022-05-07 09:28:12 +00:00
|
|
|
return [$booleanNotPartIf, $secondBooleanNotPartIf];
|
2020-01-05 12:10:01 +00:00
|
|
|
}
|
2022-07-18 15:25:27 +00:00
|
|
|
$nestedIfWithOnlyReturn->cond = $invertedCondExpr;
|
2022-05-07 09:28:12 +00:00
|
|
|
$nestedIfWithOnlyReturn->stmts = [$return];
|
|
|
|
return [$nestedIfWithOnlyReturn];
|
2020-01-05 12:10:01 +00:00
|
|
|
}
|
|
|
|
}
|