[EarlyReturn] Refactor RemoveAlwaysElseRector to return array of Nodes to avoid node in else marked as removed (#1840)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-02-20 17:44:34 +07:00 committed by GitHub
parent c44bfcc00e
commit 5fa9dd95a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 15 deletions

View File

@ -81,10 +81,6 @@ CODE_SAMPLE
return null;
}
if ($this->nodesToAddCollector->isActive()) {
return null;
}
if ($this->hasDynamicMethodCallOnFetchThis($node)) {
return null;
}

View File

@ -70,8 +70,9 @@ CODE_SAMPLE
/**
* @param If_ $node
* @return Node[]|null
*/
public function refactor(Node $node): ?Node
public function refactor(Node $node): ?array
{
if ($this->doesLastStatementBreakFlow($node)) {
return null;
@ -82,7 +83,6 @@ CODE_SAMPLE
$if = new If_($node->cond);
$if->stmts = $node->stmts;
$this->nodesToAddCollector->addNodeBeforeNode($if, $node);
$this->mirrorComments($if, $node);
/** @var ElseIf_ $firstElseIf */
@ -91,24 +91,20 @@ CODE_SAMPLE
$node->stmts = $firstElseIf->stmts;
$this->mirrorComments($node, $firstElseIf);
$statements = $this->getStatementsElseIfs($node);
if ($statements !== []) {
$this->nodesToAddCollector->addNodesAfterNode($statements, $node);
}
$nodesToReturnAfterNode = $this->getStatementsElseIfs($node);
if ($originalNode->else instanceof Else_) {
$node->else = null;
$this->nodesToAddCollector->addNodeAfterNode($originalNode->else, $node);
$nodesToReturnAfterNode = array_merge($nodesToReturnAfterNode, [$originalNode->else]);
}
return $node;
return [$if, $node, ...$nodesToReturnAfterNode];
}
if ($node->else !== null) {
$this->nodesToAddCollector->addNodesAfterNode($node->else->stmts, $node);
$stmts = $node->else->stmts;
$node->else = null;
return $node;
return [$node, ...$stmts];
}
return null;

View File

@ -9,6 +9,7 @@ final class DoNotRemoveUsedClassMethod
if (true === false) {
return -1;
} else {
echo 'a statement';
return $this->notUnused();
}
}
@ -31,6 +32,7 @@ final class DoNotRemoveUsedClassMethod
if (true === false) {
return -1;
}
echo 'a statement';
return $this->notUnused();
}