mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 07:00:04 +00:00
695c190be3
94b5561ca8
chore: bump min version of github actions (fix deprecations) (#5675)
116 lines
3.4 KiB
PHP
116 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Visibility\Rector\ClassMethod;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
use PHPStan\Analyser\Scope;
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
|
|
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
|
use Rector\ValueObject\Visibility;
|
|
use Rector\Visibility\ValueObject\ChangeMethodVisibility;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
use RectorPrefix202403\Webmozart\Assert\Assert;
|
|
/**
|
|
* @see \Rector\Tests\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector\ChangeMethodVisibilityRectorTest
|
|
*/
|
|
final class ChangeMethodVisibilityRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver
|
|
*/
|
|
private $parentClassScopeResolver;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\Privatization\NodeManipulator\VisibilityManipulator
|
|
*/
|
|
private $visibilityManipulator;
|
|
/**
|
|
* @var ChangeMethodVisibility[]
|
|
*/
|
|
private $methodVisibilities = [];
|
|
public function __construct(ParentClassScopeResolver $parentClassScopeResolver, VisibilityManipulator $visibilityManipulator)
|
|
{
|
|
$this->parentClassScopeResolver = $parentClassScopeResolver;
|
|
$this->visibilityManipulator = $visibilityManipulator;
|
|
}
|
|
public function getRuleDefinition() : RuleDefinition
|
|
{
|
|
return new RuleDefinition('Change visibility of method from parent class.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
|
class FrameworkClass
|
|
{
|
|
protected function someMethod()
|
|
{
|
|
}
|
|
}
|
|
|
|
class MyClass extends FrameworkClass
|
|
{
|
|
public function someMethod()
|
|
{
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class FrameworkClass
|
|
{
|
|
protected function someMethod()
|
|
{
|
|
}
|
|
}
|
|
|
|
class MyClass extends FrameworkClass
|
|
{
|
|
protected function someMethod()
|
|
{
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, [new ChangeMethodVisibility('FrameworkClass', 'someMethod', Visibility::PROTECTED)])]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [ClassMethod::class];
|
|
}
|
|
/**
|
|
* @param ClassMethod $node
|
|
*/
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
|
{
|
|
if ($this->methodVisibilities === []) {
|
|
return null;
|
|
}
|
|
$parentClassName = $this->parentClassScopeResolver->resolveParentClassName($scope);
|
|
if ($parentClassName === null) {
|
|
return null;
|
|
}
|
|
foreach ($this->methodVisibilities as $methodVisibility) {
|
|
if ($methodVisibility->getClass() !== $parentClassName) {
|
|
continue;
|
|
}
|
|
if (!$this->isName($node, $methodVisibility->getMethod())) {
|
|
continue;
|
|
}
|
|
$this->visibilityManipulator->changeNodeVisibility($node, $methodVisibility->getVisibility());
|
|
return $node;
|
|
}
|
|
return null;
|
|
}
|
|
/**
|
|
* @param mixed[] $configuration
|
|
*/
|
|
public function configure(array $configuration) : void
|
|
{
|
|
Assert::allIsAOf($configuration, ChangeMethodVisibility::class);
|
|
$this->methodVisibilities = $configuration;
|
|
}
|
|
}
|