Skip to content

Commit

Permalink
Remove NEXT_NODE from ChangeIfElseValueAssignToEarlyReturnRector (#3914)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 21, 2023
1 parent 46f5209 commit a87a9d8
Showing 1 changed file with 41 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\NodeManipulator\StmtsManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -73,57 +72,64 @@ public function run()
*/
public function getNodeTypes(): array
{
return [If_::class];
return [StmtsAwareInterface::class];
}

/**
* @param If_ $node
* @return Stmt[]|null
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node): ?array
public function refactor(Node $node): ?StmtsAwareInterface
{
$nextNode = $node->getAttribute(AttributeKey::NEXT_NODE);
if (! $nextNode instanceof Return_) {
if ($node->stmts === null) {
return null;
}

if (! $nextNode->expr instanceof Expr) {
return null;
}
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Return_) {
continue;
}

if (! $this->ifManipulator->isIfAndElseWithSameVariableAssignAsLastStmts($node, $nextNode->expr)) {
return null;
}
if (! $stmt->expr instanceof Expr) {
continue;
}

$lastIfStmtKey = array_key_last($node->stmts);
$previousStmt = $node->stmts[$key - 1] ?? null;
if (! $previousStmt instanceof If_) {
continue;
}

/** @var Assign $assign */
$assign = $this->stmtsManipulator->getUnwrappedLastStmt($node->stmts);
$if = $previousStmt;

$returnLastIf = new Return_($assign->expr);
$this->mirrorComments($returnLastIf, $assign);
$node->stmts[$lastIfStmtKey] = $returnLastIf;
if (! $this->ifManipulator->isIfAndElseWithSameVariableAssignAsLastStmts($if, $stmt->expr)) {
continue;
}

$else = $node->else;
if (! $else instanceof Else_) {
throw new ShouldNotHappenException();
}
$lastIfStmtKey = array_key_last($if->stmts);

/** @var array<int, Stmt> $elseStmts */
$elseStmts = $else->stmts;
/** @var Assign $assign */
$assign = $this->stmtsManipulator->getUnwrappedLastStmt($if->stmts);

/** @var Assign $assign */
$assign = $this->stmtsManipulator->getUnwrappedLastStmt($elseStmts);
$returnLastIf = new Return_($assign->expr);
$this->mirrorComments($returnLastIf, $assign);
$if->stmts[$lastIfStmtKey] = $returnLastIf;

$lastElseStmtKey = array_key_last($elseStmts);
/** @var Else_ $else */
$else = $if->else;

$returnLastElse = new Return_($assign->expr);
$this->mirrorComments($returnLastElse, $assign);
$elseStmts[$lastElseStmtKey] = $returnLastElse;
/** @var array<int, Stmt> $elseStmts */
$elseStmts = $else->stmts;

$node->else = null;
$this->removeNode($nextNode);
/** @var Assign $assign */
$assign = $this->stmtsManipulator->getUnwrappedLastStmt($elseStmts);

return array_merge([$node], $elseStmts);
$this->mirrorComments($stmt, $assign);

$if->else = null;
$stmt->expr = $assign->expr;

return $node;
}

return null;
}
}

0 comments on commit a87a9d8

Please sign in to comment.