Skip to content

Commit

Permalink
Drop AttributeKey::SCOPE in CurrentAndParentClassMethodComparator (#3803
Browse files Browse the repository at this point in the history
)
  • Loading branch information
staabm committed May 11, 2023
1 parent 257395a commit f4ecf4a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
) {
}

public function isParentCallMatching(ClassMethod $classMethod, StaticCall $staticCall): bool
public function isParentCallMatching(ClassMethod $classMethod, StaticCall $staticCall, Scope $scope): bool
{
if (! $this->isSameMethodParentCall($classMethod, $staticCall)) {
return false;
Expand All @@ -48,7 +48,7 @@ public function isParentCallMatching(ClassMethod $classMethod, StaticCall $stati
return false;
}

return ! $this->isParentClassMethodVisibilityOrDefaultOverride($classMethod, $staticCall);
return ! $this->isParentClassMethodVisibilityOrDefaultOverride($classMethod, $staticCall, $scope);
}

private function isSameMethodParentCall(ClassMethod $classMethod, StaticCall $staticCall): bool
Expand Down Expand Up @@ -95,7 +95,8 @@ private function areArgsAndParamsEqual(array $parentStaticCallArgs, array $curre

private function isParentClassMethodVisibilityOrDefaultOverride(
ClassMethod $classMethod,
StaticCall $staticCall
StaticCall $staticCall,
Scope $scope
): bool {
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
if (! $classReflection instanceof ClassReflection) {
Expand All @@ -116,11 +117,11 @@ private function isParentClassMethodVisibilityOrDefaultOverride(
$nativeParentClassMethodReflection = $nativeParentClassReflection->getMethod($methodName);

if (! $nativeParentClassMethodReflection->isProtected()) {
return $this->isOverridingParentParameters($classMethod, $parentClassReflection, $methodName);
return $this->isOverridingParentParameters($classMethod, $parentClassReflection, $methodName, $scope);
}

if (! $nativeParentClassMethodReflection->isPublic()) {
return $this->isOverridingParentParameters($classMethod, $parentClassReflection, $methodName);
return $this->isOverridingParentParameters($classMethod, $parentClassReflection, $methodName, $scope);
}

return true;
Expand All @@ -132,13 +133,9 @@ private function isParentClassMethodVisibilityOrDefaultOverride(
private function isOverridingParentParameters(
ClassMethod $classMethod,
ClassReflection $classReflection,
string $methodName
string $methodName,
Scope $scope
): bool {
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
throw new ShouldNotHappenException();
}

$extendedMethodReflection = $classReflection->getMethod($methodName, $scope);

// 3rd party code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Rector\AbstractRector;
use PHPStan\Analyser\Scope;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\DeadCode\Comparator\CurrentAndParentClassMethodComparator;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -23,7 +24,7 @@
/**
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector\RemoveDelegatingParentCallRectorTest
*/
final class RemoveDelegatingParentCallRector extends AbstractRector
final class RemoveDelegatingParentCallRector extends AbstractScopeAwareRector
{
/**
* @var string[]
Expand Down Expand Up @@ -81,7 +82,7 @@ public function getNodeTypes(): array
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
$classLike = $this->betterNodeFinder->findParentType($node, Class_::class);
if ($this->shouldSkipClass($classLike)) {
Expand All @@ -103,7 +104,7 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->currentAndParentClassMethodComparator->isParentCallMatching($node, $staticCall)) {
if (! $this->currentAndParentClassMethodComparator->isParentCallMatching($node, $staticCall, $scope)) {
return null;
}

Expand Down

0 comments on commit f4ecf4a

Please sign in to comment.