Skip to content

Commit

Permalink
[Privatization] Skip method with parent call (#5283)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 23, 2023
1 parent 72c0216 commit d864d7b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\Fixture;

use Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\Source\SomeParentClass;

final class SkipWithParentCall extends SomeParentClass
{
protected function someMethod()
{
parent::someMethod();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\Source;

class SomeParentClass
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Rector\Privatization\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Privatization\Guard\OverrideByParentClassGuard;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
Expand All @@ -24,7 +26,8 @@ final class PrivatizeFinalClassMethodRector extends AbstractScopeAwareRector
public function __construct(
private readonly ClassMethodVisibilityGuard $classMethodVisibilityGuard,
private readonly VisibilityManipulator $visibilityManipulator,
private readonly OverrideByParentClassGuard $overrideByParentClassGuard
private readonly OverrideByParentClassGuard $overrideByParentClassGuard,
private readonly BetterNodeFinder $betterNodeFinder,
) {
}

Expand Down Expand Up @@ -123,6 +126,22 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
return true;
}

return ! $classMethod->isProtected();
if (! $classMethod->isProtected()) {
return true;
}

// if has parent call, its probably overriding parent one → skip it
$hasParentCall = (bool) $this->betterNodeFinder->findFirst(
(array) $classMethod->stmts,
function (Node $node): bool {
if (! $node instanceof StaticCall) {
return false;
}

return $this->isName($node->class, 'parent');
}
);

return $hasParentCall;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Set\SymfonySetList;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Symfony\Set\SymfonySetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
]);
$rectorConfig->sets([SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES, DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES]);
};

0 comments on commit d864d7b

Please sign in to comment.