Skip to content

Commit

Permalink
[NodeRemover] Use return null after $this->removeNode() (#3558)
Browse files Browse the repository at this point in the history
* [NodeRemover] Use return null after $this->removeNode()

* return node if it try to change next node before remove

* update warning message
  • Loading branch information
samsonasik committed Apr 3, 2023
1 parent 3cbf710 commit 5e5681d
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function refactor(Node $node): ?Node
}

$this->removeNode($node);
return $node;
return null;
}

private function cleanCastedExpr(Expr $expr): Expr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function refactor(Node $node): ?Node

$this->removeNode($node);

return $node;
return null;
}

private function shouldSkipNonFinalNonPrivateClassMethod(Class_ $class, ClassMethod $classMethod): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function refactor(Node $node): ?Node

$this->removeNode($node);

return $node;
return null;
}

private function shouldSkip(ClassMethod $classMethod): bool
Expand Down
2 changes: 1 addition & 1 deletion rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ public function refactor(Node $node): ?Node
}

$this->removeNode($node);
return $node;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function refactor(Node $node): ?Node

$this->removeNode($node);

return $node;
return null;
}

private function resolveClassLike(MethodCall $methodCall): ?ClassLike
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function refactor(Node $node): array|null|TryCatch

if ($this->isEmpty($node->stmts)) {
$this->removeNode($node);
return $node;
return null;
}

if (count($node->catches) !== 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ public function refactor(Node $node): ?Node

$this->removeNode($node);

return $node;
return null;
}
}
1 change: 1 addition & 0 deletions rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public function refactor(Node $node): ?Node

$this->removeNode($node);

// change next node before remove, so it needs to return the Node
return $node;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
private const EMPTY_NODE_ARRAY_MESSAGE = <<<CODE_SAMPLE
Array of nodes cannot be empty. Ensure "%s->refactor()" returns non-empty array for Nodes.
A) Return null for no change:
A) Direct return null for no change:
return null;
B) Remove the Node:
\$this->removeNode(\$node);
return \$node;
return null;
CODE_SAMPLE;

protected NodeNameResolver $nodeNameResolver;
Expand Down Expand Up @@ -210,7 +210,7 @@ final public function enterNode(Node $node)

$refactoredNode = $this->refactor($node);

// nothing to change → continue
// nothing to change or just removed via removeNode() → continue
if ($refactoredNode === null) {
return null;
}
Expand Down

0 comments on commit 5e5681d

Please sign in to comment.