2020-12-05 21:33:30 +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\Php80\Rector\ClassMethod;
|
2020-12-05 21:33:30 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\MethodName;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-12-05 21:33:30 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php80\Rector\ClassMethod\FinalPrivateToPrivateVisibilityRector\FinalPrivateToPrivateVisibilityRectorTest
|
2020-12-05 21:33:30 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class FinalPrivateToPrivateVisibilityRector extends AbstractRector implements MinPhpVersionInterface
|
2020-12-05 21:33:30 +00:00
|
|
|
{
|
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;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(VisibilityManipulator $visibilityManipulator)
|
2021-11-24 11:00:16 +00:00
|
|
|
{
|
|
|
|
$this->visibilityManipulator = $visibilityManipulator;
|
|
|
|
}
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::NO_FINAL_PRIVATE;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-12-05 21:33:30 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes method visibility from final private to only private', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-12-05 21:33:30 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
final private function getter() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-12-05 21:33:30 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
private function getter() {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-12-05 21:33:30 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-12-05 21:33:30 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-12-05 21:33:30 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassMethod::class];
|
2020-12-05 21:33:30 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClassMethod $node
|
2020-12-05 21:33:30 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-12-05 21:33:30 +00:00
|
|
|
{
|
|
|
|
if ($this->shouldSkip($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-01-30 21:41:25 +00:00
|
|
|
$this->visibilityManipulator->makeNonFinal($node);
|
2020-12-05 21:33:30 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkip(ClassMethod $classMethod) : bool
|
2020-12-05 21:33:30 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$classMethod->isFinal()) {
|
|
|
|
return \true;
|
2020-12-05 21:33:30 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($classMethod->name->toString() === MethodName::CONSTRUCT) {
|
2022-01-12 10:12:35 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return !$classMethod->isPrivate();
|
2020-12-05 21:33:30 +00:00
|
|
|
}
|
|
|
|
}
|