Skip to content
Merged
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
29 changes: 14 additions & 15 deletions rules/Php70/Rector/ClassMethod/Php4ConstructorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Enum\ObjectReference;
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
use Rector\Php70\NodeAnalyzer\MethodCallNameAnalyzer;
use Rector\Php70\NodeAnalyzer\Php4ConstructorClassMethodAnalyzer;
use Rector\PHPStan\ScopeFetcher;
Expand All @@ -34,7 +32,6 @@ final class Php4ConstructorRector extends AbstractRector implements MinPhpVersio
{
public function __construct(
private readonly Php4ConstructorClassMethodAnalyzer $php4ConstructorClassMethodAnalyzer,
private readonly ParentClassScopeResolver $parentClassScopeResolver,
private readonly MethodCallNameAnalyzer $methodCallNameAnalyzer,
) {
}
Expand Down Expand Up @@ -102,6 +99,8 @@ public function refactor(Node $node): Class_|null
return null;
}

$parentClassReflection = $classReflection->getParentClass();

foreach ($node->stmts as $classStmtKey => $classStmt) {
if (! $classStmt instanceof ClassMethod) {
continue;
Expand All @@ -114,7 +113,7 @@ public function refactor(Node $node): Class_|null
$psr4ConstructorMethod = $classStmt;

// process parent call references first
$this->processClassMethodStatementsForParentConstructorCalls($psr4ConstructorMethod, $scope);
$this->processClassMethodStatementsForParentConstructorCalls($psr4ConstructorMethod, $parentClassReflection);

// does it already have a __construct method?
if (! $node->getMethod(MethodName::CONSTRUCT) instanceof ClassMethod) {
Expand Down Expand Up @@ -153,8 +152,15 @@ public function refactor(Node $node): Class_|null
return null;
}

private function processClassMethodStatementsForParentConstructorCalls(ClassMethod $classMethod, Scope $scope): void
{
private function processClassMethodStatementsForParentConstructorCalls(
ClassMethod $classMethod,
?ClassReflection $parentClassReflection
): void {
// no parent class
if (! $parentClassReflection instanceof ClassReflection) {
return;
}

foreach ((array) $classMethod->stmts as $methodStmt) {
if (! $methodStmt instanceof Expression) {
continue;
Expand All @@ -165,19 +171,12 @@ private function processClassMethodStatementsForParentConstructorCalls(ClassMeth
continue;
}

$this->processParentPhp4ConstructCall($methodStmt, $scope);
$this->processParentPhp4ConstructCall($methodStmt, $parentClassReflection);
}
}

private function processParentPhp4ConstructCall(StaticCall $staticCall, Scope $scope): void
private function processParentPhp4ConstructCall(StaticCall $staticCall, ClassReflection $parentClassReflection): void
{
$parentClassReflection = $this->parentClassScopeResolver->resolveParentClassReflection($scope);

// no parent class
if (! $parentClassReflection instanceof ClassReflection) {
return;
}

if (! $staticCall->class instanceof Name) {
return;
}
Expand Down
21 changes: 0 additions & 21 deletions src/NodeCollector/ScopeResolver/ParentClassScopeResolver.php

This file was deleted.

Loading