2020-05-26 22:22:22 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Privatization\Rector\ClassMethod;
|
2020-05-26 22:22:22 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-11-23 16:52:05 +00:00
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2022-12-12 08:02:46 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2023-05-27 10:31:41 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
2023-08-06 09:53:01 +00:00
|
|
|
use Rector\Privatization\Guard\OverrideByParentClassGuard;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
|
|
|
use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-05-26 22:22:22 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\PrivatizeFinalClassMethodRectorTest
|
2020-05-26 22:22:22 +00:00
|
|
|
*/
|
2023-06-09 17:06:47 +00:00
|
|
|
final class PrivatizeFinalClassMethodRector extends AbstractScopeAwareRector
|
2020-05-26 22:22:22 +00:00
|
|
|
{
|
2020-08-24 22:26:14 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard
|
2020-08-24 22:26:14 +00:00
|
|
|
*/
|
|
|
|
private $classMethodVisibilityGuard;
|
2021-11-24 11:00:16 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-11-24 11:00:16 +00:00
|
|
|
* @var \Rector\Privatization\NodeManipulator\VisibilityManipulator
|
|
|
|
*/
|
|
|
|
private $visibilityManipulator;
|
2023-08-06 09:53:01 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Privatization\Guard\OverrideByParentClassGuard
|
|
|
|
*/
|
|
|
|
private $overrideByParentClassGuard;
|
2023-11-23 16:52:05 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
2023-11-23 16:52:05 +00:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
|
|
|
public function __construct(ClassMethodVisibilityGuard $classMethodVisibilityGuard, VisibilityManipulator $visibilityManipulator, OverrideByParentClassGuard $overrideByParentClassGuard, BetterNodeFinder $betterNodeFinder)
|
2020-08-24 22:26:14 +00:00
|
|
|
{
|
|
|
|
$this->classMethodVisibilityGuard = $classMethodVisibilityGuard;
|
2021-11-24 11:00:16 +00:00
|
|
|
$this->visibilityManipulator = $visibilityManipulator;
|
2023-08-06 09:53:01 +00:00
|
|
|
$this->overrideByParentClassGuard = $overrideByParentClassGuard;
|
2023-11-23 16:52:05 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2020-08-24 22:26:14 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-05-26 22:22:22 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change protected class method to private if possible', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-05-26 22:22:22 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
protected function someMethod()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-05-26 22:22:22 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
private function someMethod()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-05-26 22:22:22 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-05-26 22:22:22 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-05-26 22:22:22 +00:00
|
|
|
{
|
2023-03-27 18:32:49 +00:00
|
|
|
return [Class_::class];
|
2020-05-26 22:22:22 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-03-27 18:32:49 +00:00
|
|
|
* @param Class_ $node
|
2020-05-26 22:22:22 +00:00
|
|
|
*/
|
2023-06-09 17:06:47 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2020-05-26 22:22:22 +00:00
|
|
|
{
|
2023-03-27 18:32:49 +00:00
|
|
|
if (!$node->isFinal()) {
|
2021-10-04 21:24:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-08-06 09:53:01 +00:00
|
|
|
if (!$this->overrideByParentClassGuard->isLegal($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-09 17:06:47 +00:00
|
|
|
$classReflection = $scope->getClassReflection();
|
2023-05-27 10:31:41 +00:00
|
|
|
if (!$classReflection instanceof ClassReflection) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-03-27 18:32:49 +00:00
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($node->getMethods() as $classMethod) {
|
|
|
|
if ($this->shouldSkipClassMethod($classMethod)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->classMethodVisibilityGuard->isClassMethodVisibilityGuardedByParent($classMethod, $classReflection)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->classMethodVisibilityGuard->isClassMethodVisibilityGuardedByTrait($classMethod, $classReflection)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->visibilityManipulator->makePrivate($classMethod);
|
|
|
|
$hasChanged = \true;
|
2020-05-26 22:22:22 +00:00
|
|
|
}
|
2023-03-27 18:32:49 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2020-05-26 22:22:22 +00:00
|
|
|
}
|
2023-03-27 18:32:49 +00:00
|
|
|
return null;
|
2020-05-26 22:22:22 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkipClassMethod(ClassMethod $classMethod) : bool
|
2020-05-26 22:22:22 +00:00
|
|
|
{
|
2023-09-09 14:19:44 +00:00
|
|
|
// edge case in nette framework
|
2023-09-09 22:49:40 +00:00
|
|
|
/** @var string $methodName */
|
|
|
|
$methodName = $this->getName($classMethod->name);
|
|
|
|
if (\strncmp($methodName, 'createComponent', \strlen('createComponent')) === 0) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-05-26 22:22:22 +00:00
|
|
|
}
|
2023-11-23 16:52:05 +00:00
|
|
|
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;
|
2020-05-26 22:22:22 +00:00
|
|
|
}
|
|
|
|
}
|