rector/rules/CodingStyle/Rector/ClassMethod/MakeInheritedMethodVisibilitySameAsParentRector.php
Tomas Votruba 24318b9a19 Updated Rector to commit 26cad67b1c
26cad67b1c [CodingStyle] Clean up getParents() call on MakeInheritedMethodVisibilitySameAsParentRector (#2246)
2022-05-07 00:18:08 +00:00

140 lines
4.6 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\CodingStyle\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use ReflectionMethod;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://3v4l.org/RFYmn
*
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector\MakeInheritedMethodVisibilitySameAsParentRectorTest
*/
final class MakeInheritedMethodVisibilitySameAsParentRector extends \Rector\Core\Rector\AbstractScopeAwareRector
{
/**
* @readonly
* @var \Rector\Privatization\NodeManipulator\VisibilityManipulator
*/
private $visibilityManipulator;
public function __construct(\Rector\Privatization\NodeManipulator\VisibilityManipulator $visibilityManipulator)
{
$this->visibilityManipulator = $visibilityManipulator;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Make method visibility same as parent one', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
class ChildClass extends ParentClass
{
public function run()
{
}
}
class ParentClass
{
protected function run()
{
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class ChildClass extends ParentClass
{
protected function run()
{
}
}
class ParentClass
{
protected function run()
{
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\Class_::class];
}
/**
* @param Class_ $node
*/
public function refactorWithScope(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope) : ?\PhpParser\Node
{
$hasChanged = \false;
$classReflection = $scope->getClassReflection();
if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) {
return null;
}
$parentClassReflections = $classReflection->getParents();
if ($parentClassReflections === []) {
return null;
}
foreach ($node->getMethods() as $classMethod) {
if ($classMethod->isMagic()) {
continue;
}
/** @var string $methodName */
$methodName = $this->getName($classMethod->name);
foreach ($parentClassReflections as $parentClassReflection) {
$nativeClassReflection = $parentClassReflection->getNativeReflection();
// the class reflection above takes also @method annotations into an account
if (!$nativeClassReflection->hasMethod($methodName)) {
continue;
}
$parentReflectionMethod = $nativeClassReflection->getMethod($methodName);
if ($this->isClassMethodCompatibleWithParentReflectionMethod($classMethod, $parentReflectionMethod)) {
continue;
}
$this->changeClassMethodVisibilityBasedOnReflectionMethod($classMethod, $parentReflectionMethod);
$hasChanged = \true;
}
}
if ($hasChanged) {
return $node;
}
return null;
}
private function isClassMethodCompatibleWithParentReflectionMethod(\PhpParser\Node\Stmt\ClassMethod $classMethod, \ReflectionMethod $reflectionMethod) : bool
{
if ($reflectionMethod->isPublic() && $classMethod->isPublic()) {
return \true;
}
if ($reflectionMethod->isProtected() && $classMethod->isProtected()) {
return \true;
}
if (!$reflectionMethod->isPrivate()) {
return \false;
}
return $classMethod->isPrivate();
}
private function changeClassMethodVisibilityBasedOnReflectionMethod(\PhpParser\Node\Stmt\ClassMethod $classMethod, \ReflectionMethod $reflectionMethod) : void
{
if ($reflectionMethod->isPublic()) {
$this->visibilityManipulator->makePublic($classMethod);
return;
}
if ($reflectionMethod->isProtected()) {
$this->visibilityManipulator->makeProtected($classMethod);
return;
}
if ($reflectionMethod->isPrivate()) {
$this->visibilityManipulator->makePrivate($classMethod);
}
}
}