Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions rules/Renaming/Rector/MethodCall/RenameMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeManipulator\ClassManipulator;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\Renaming\Contract\MethodCallRenameInterface;
Expand Down Expand Up @@ -87,8 +85,8 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
if ($node instanceof Class_ || $node instanceof Trait_ || $node instanceof Interface_) {
$scope = ScopeFetcher::fetch($node);
return $this->refactorClass($node, $scope);
$classReflection = $this->getClassReflection($node);
return $this->refactorClass($node, $classReflection);
}

return $this->refactorMethodCallAndStaticCall($node);
Expand Down Expand Up @@ -169,10 +167,8 @@ private function shouldKeepForParentInterface(
*/
private function refactorClass(
Class_|Trait_|Interface_ $classOrInterface,
Scope $scope
ClassReflection $classReflection
): Class_|Trait_|Interface_|null {
$classReflection = $scope->getClassReflection();

$hasChanged = false;

foreach ($classOrInterface->getMethods() as $classMethod) {
Expand Down
32 changes: 32 additions & 0 deletions src/PhpParser/NodeTraverser/RectorNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\NodeVisitor;
use Rector\Application\RectorRegistry;
use Rector\Rector\RectorRunner;
Expand Down Expand Up @@ -76,6 +77,10 @@ private function traverseNode(Node $node, File $file): void
}

$this->traverseNode($subNode, $file);
// a descendant removal (e.g. an emptied method body) can make a Class_ rule
// newly apply; re-offer the class once, now that its children are settled
$node->{$name} = $this->reRunClassRectors($subNode, $file);
$subNode = $node->{$name};

if ($this->stopTraversal) {
break;
Expand Down Expand Up @@ -126,6 +131,10 @@ private function traverseArray(array $nodes, File $file): array
}

$this->traverseNode($node, $file);

// re-offer a Class_ after descendant removals settle (see traverseNode)
$nodes[$i] = $node = $this->reRunClassRectors($node, $file);

if ($this->stopTraversal) {
break;
}
Expand All @@ -140,6 +149,29 @@ private function traverseArray(array $nodes, File $file): array
return $nodes;
}

/**
* Re-run Class_ rules once, after its children are traversed, so a rule whose
* precondition is only met by a descendant removal still fires in a single pass.
*/
private function reRunClassRectors(Node $node, File $file): Node
{
if (! $node instanceof Class_) {
return $node;
}

foreach ($this->rectorRegistry->forNodeAndFile($node, $file) as $currentNodeVisitor) {
$return = $this->rectorRunner->run($currentNodeVisitor, $node);
if (! $return instanceof Node) {
continue;
}

$this->ensureReplacementReasonable($node, $return);
$node = $return;
}

return $node;
}

private function ensureReplacementReasonable(Node $old, Node $new): void
{
if ($old instanceof Stmt) {
Expand Down
Loading