From 8fe9717aeea956dce2700616cd9ace2a56ad7dc8 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 30 Jan 2024 09:02:15 +0000 Subject: [PATCH] Updated Rector to commit 3c51cd8ebebd6840a0374e367308049c36761ee0 https://github.com/rectorphp/rector-src/commit/3c51cd8ebebd6840a0374e367308049c36761ee0 [EarlyReturn] Improve RemoveAlwaysElseRector to handle multiple ElseIfs (#8178) (#5521) --- .../Rector/If_/RemoveAlwaysElseRector.php | 66 ++++++++++++++----- src/Application/VersionResolver.php | 4 +- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php index 48279287e67..ca5b1cdd52b 100644 --- a/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php +++ b/rules/EarlyReturn/Rector/If_/RemoveAlwaysElseRector.php @@ -67,21 +67,7 @@ CODE_SAMPLE return null; } if ($node->elseifs !== []) { - $originalNode = clone $node; - $if = new If_($node->cond); - $if->stmts = $node->stmts; - $this->mirrorComments($if, $node); - /** @var ElseIf_ $firstElseIf */ - $firstElseIf = \array_shift($node->elseifs); - $node->cond = $firstElseIf->cond; - $node->stmts = $firstElseIf->stmts; - $this->mirrorComments($node, $firstElseIf); - $nodesToReturnAfterNode = $this->getStatementsElseIfs($node); - if ($originalNode->else instanceof Else_) { - $node->else = null; - $nodesToReturnAfterNode = \array_merge($nodesToReturnAfterNode, [$originalNode->else]); - } - return \array_merge([$if, $node], $nodesToReturnAfterNode); + return $this->handleElseIfs($node); } if ($node->else instanceof Else_) { $stmts = $node->else->stmts; @@ -90,6 +76,39 @@ CODE_SAMPLE } return null; } + /** + * @return Node[] + */ + private function handleElseIfs(If_ $if) : array + { + $nodesToReturn = []; + $originalIf = clone $if; + $firstIf = $this->createIfFromNode($if); + $nodesToReturn[] = $firstIf; + while ($if->elseifs !== []) { + /** @var ElseIf_ $currentElseIf */ + $currentElseIf = \array_shift($if->elseifs); + // If the last statement in the `elseif` breaks flow, merge it into the original `if` and stop processing + if ($this->doesLastStatementBreakFlow($currentElseIf)) { + $this->updateIfWithElseIf($if, $currentElseIf); + $nodesToReturn = \array_merge(\is_array($nodesToReturn) ? $nodesToReturn : \iterator_to_array($nodesToReturn), [$if], $this->getStatementsElseIfs($if)); + break; + } + $isLastElseIf = $if->elseifs === []; + // If it's the last `elseif`, merge it with the original `if` to keep the formatting + if ($isLastElseIf) { + $this->updateIfWithElseIf($if, $currentElseIf); + $nodesToReturn[] = $if; + break; + } + // Otherwise, create a separate `if` node for `elseif` + $nodesToReturn[] = $this->createIfFromNode($currentElseIf); + } + if ($originalIf->else instanceof Else_) { + $nodesToReturn[] = $originalIf->else; + } + return $nodesToReturn; + } /** * @return ElseIf_[] */ @@ -124,4 +143,21 @@ CODE_SAMPLE } return !($lastStmt instanceof Return_ || $lastStmt instanceof Throw_ || $lastStmt instanceof Continue_ || $lastStmt instanceof Expression && $lastStmt->expr instanceof Exit_); } + /** + * @param \PhpParser\Node\Stmt\If_|\PhpParser\Node\Stmt\ElseIf_ $node + */ + private function createIfFromNode($node) : If_ + { + $if = new If_($node->cond); + $if->stmts = $node->stmts; + $this->mirrorComments($if, $node); + return $if; + } + private function updateIfWithElseIf(If_ $if, ElseIf_ $elseIf) : void + { + $if->cond = $elseIf->cond; + $if->stmts = $elseIf->stmts; + $this->mirrorComments($if, $elseIf); + $if->else = null; + } } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 0a772505162..51229ef596e 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '58ef131772f7745a52c3990aeaea34a4892b6ae5'; + public const PACKAGE_VERSION = '3c51cd8ebebd6840a0374e367308049c36761ee0'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-01-30 12:35:43'; + public const RELEASE_DATE = '2024-01-30 16:00:02'; /** * @var int */