Skip to content

Commit

Permalink
[DeadCode] Improve RemoveUnreachableStatementRector performance by re…
Browse files Browse the repository at this point in the history
…turn after array_splice early (#2193)

* [DeadCode] Improve RemoveUnreachableStatementRector by flag removed early on loop

* fill, return early

* use array_splice
  • Loading branch information
samsonasik authored Apr 29, 2022
1 parent c411410 commit 4d7c087
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public function refactor(Node $node): ?Node
*/
private function processCleanUpUnreachabelStmts(array $stmts): array
{
$originalStmts = $stmts;
foreach ($stmts as $key => $stmt) {
if (! isset($stmts[$key - 1])) {
continue;
Expand All @@ -133,17 +132,12 @@ private function processCleanUpUnreachabelStmts(array $stmts): array
$previousStmt = $stmts[$key - 1];

if ($this->shouldRemove($previousStmt, $stmt)) {
unset($stmts[$key]);
break;
array_splice($stmts, $key);
return $stmts;
}
}

if ($originalStmts === $stmts) {
return $originalStmts;
}

$stmts = array_values($stmts);
return $this->processCleanUpUnreachabelStmts($stmts);
return $stmts;
}

private function shouldRemove(Stmt $previousStmt, Stmt $currentStmt): bool
Expand Down

0 comments on commit 4d7c087

Please sign in to comment.