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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

use PhpParser\PrettyPrinter\Standard;

final class KeepMagicParentCalledMethod extends Standard
{
// called from parent by $this->{'p'} method
protected function pFileNode($fileNode)
{
}
}
88 changes: 88 additions & 0 deletions rules/Privatization/Guard/ParentClassMagicCallGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Rector\Privatization\Guard;

use PhpParser\PrettyPrinterAbstract;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\PrettyPrinter\Standard;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpParser\AstResolver;
use Rector\PhpParser\Node\BetterNodeFinder;

final class ParentClassMagicCallGuard
{
/**
* To speed up analysis
* @var string[]
*/
private const KNOWN_DYNAMIC_CALL_CLASSES = [Standard::class, PrettyPrinterAbstract::class];

/**
* @var array<string, bool>
*/
private array $cachedContainsByClassName = [];

public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly AstResolver $astResolver,
private readonly BetterNodeFinder $betterNodeFinder
) {
foreach (self::KNOWN_DYNAMIC_CALL_CLASSES as $knownDynamicCallClass) {
$this->cachedContainsByClassName[$knownDynamicCallClass] = true;
}
}

/**
* E.g. parent class has $this->{$magicName} call that might call the protected method
* If we make it private, it will break the code
*/
public function containsParentClassMagicCall(Class_ $class): bool
{
if (! $class->extends instanceof Name) {
return false;
}

// cache as heavy AST parsing here
$className = $this->nodeNameResolver->getName($class);

if (isset($this->cachedContainsByClassName[$className])) {
return $this->cachedContainsByClassName[$className];
}

$parentClassName = $this->nodeNameResolver->getName($class->extends);
if (isset($this->cachedContainsByClassName[$parentClassName])) {
return $this->cachedContainsByClassName[$parentClassName];
}

$parentClass = $this->astResolver->resolveClassFromName($parentClassName);
if (! $parentClass instanceof Class_) {
$this->cachedContainsByClassName[$parentClassName] = false;
return false;
}

foreach ($parentClass->getMethods() as $classMethod) {
if ($classMethod->isAbstract()) {
continue;
}

/** @var MethodCall[] $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstancesOfScoped(
(array) $classMethod->stmts,
MethodCall::class
);
foreach ($methodCalls as $methodCall) {
if ($methodCall->name instanceof Expr) {
$this->cachedContainsByClassName[$parentClassName] = true;
return true;
}
}
}

return $this->containsParentClassMagicCall($parentClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\PHPStan\ScopeFetcher;
use Rector\Privatization\Guard\LaravelModelGuard;
use Rector\Privatization\Guard\OverrideByParentClassGuard;
use Rector\Privatization\Guard\ParentClassMagicCallGuard;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard;
use Rector\Rector\AbstractRector;
Expand All @@ -30,6 +31,7 @@ public function __construct(
private readonly OverrideByParentClassGuard $overrideByParentClassGuard,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly LaravelModelGuard $laravelModelGuard,
private readonly ParentClassMagicCallGuard $parentClassMagicCallGuard,
) {
}

Expand Down Expand Up @@ -113,6 +115,10 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->parentClassMagicCallGuard->containsParentClassMagicCall($node)) {
continue;
}

$this->visibilityManipulator->makePrivate($classMethod);
$hasChanged = true;
}
Expand Down