mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 03:40:50 +00:00
1f63ed3e55
* try reworking trait scope run * load configs from extension installer * add trait type * load configs from extension installer in PHPStan internally in Rector too * [ci-review] Rector Rectify Co-authored-by: GitHub Action <action@github.com>
112 lines
2.7 KiB
PHP
112 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Rector\Privatization\Rector\ClassMethod;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
use PHPStan\Analyser\Scope;
|
|
use PHPStan\Reflection\ClassReflection;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Rector\Privatization\VisibilityGuard\ClassMethodVisibilityGuard;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
/**
|
|
* @see \Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\PrivatizeFinalClassMethodRectorTest
|
|
*/
|
|
final class PrivatizeFinalClassMethodRector extends AbstractRector
|
|
{
|
|
public function __construct(
|
|
private ClassMethodVisibilityGuard $classMethodVisibilityGuard
|
|
) {
|
|
}
|
|
|
|
public function getRuleDefinition(): RuleDefinition
|
|
{
|
|
return new RuleDefinition(
|
|
'Change protected class method to private if possible',
|
|
[
|
|
new CodeSample(
|
|
<<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
protected function someMethod()
|
|
{
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
,
|
|
<<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
private function someMethod()
|
|
{
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
),
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes(): array
|
|
{
|
|
return [ClassMethod::class];
|
|
}
|
|
|
|
/**
|
|
* @param ClassMethod $node
|
|
*/
|
|
public function refactor(Node $node): ?Node
|
|
{
|
|
$scope = $node->getAttribute(AttributeKey::SCOPE);
|
|
if (! $scope instanceof Scope) {
|
|
return null;
|
|
}
|
|
|
|
if ($scope->isInTrait()) {
|
|
return null;
|
|
}
|
|
|
|
$classReflection = $scope->getClassReflection();
|
|
if (! $classReflection instanceof ClassReflection) {
|
|
return null;
|
|
}
|
|
|
|
if (! $classReflection->isFinal()) {
|
|
return null;
|
|
}
|
|
|
|
if ($this->shouldSkipClassMethod($node)) {
|
|
return null;
|
|
}
|
|
|
|
if ($this->classMethodVisibilityGuard->isClassMethodVisibilityGuardedByParent($node, $classReflection)) {
|
|
return null;
|
|
}
|
|
|
|
if ($this->classMethodVisibilityGuard->isClassMethodVisibilityGuardedByTrait($node, $classReflection)) {
|
|
return null;
|
|
}
|
|
|
|
$this->visibilityManipulator->makePrivate($node);
|
|
|
|
return $node;
|
|
}
|
|
|
|
private function shouldSkipClassMethod(ClassMethod $classMethod): bool
|
|
{
|
|
if ($this->isName($classMethod, 'createComponent*')) {
|
|
return true;
|
|
}
|
|
|
|
return ! $classMethod->isProtected();
|
|
}
|
|
}
|