Updated Rector to commit 3c51cd8ebebd6840a0374e367308049c36761ee0

3c51cd8ebe [EarlyReturn] Improve RemoveAlwaysElseRector to handle multiple ElseIfs (#8178) (#5521)
This commit is contained in:
Tomas Votruba 2024-01-30 09:02:15 +00:00
parent c828305041
commit 8fe9717aee
2 changed files with 53 additions and 17 deletions

View File

@ -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;
}
}

View File

@ -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
*/