From a87a9d8e026c3499c980facf1bf24bd51b69c924 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 21 May 2023 17:12:55 +0200 Subject: [PATCH] Remove NEXT_NODE from ChangeIfElseValueAssignToEarlyReturnRector (#3914) --- ...geIfElseValueAssignToEarlyReturnRector.php | 76 ++++++++++--------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php b/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php index 3acefd938e8..acca8b86b29 100644 --- a/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php +++ b/rules/EarlyReturn/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php @@ -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; @@ -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 $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 $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; } }