2019-10-13 05:59:52 +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\CodingStyle\Rector\ClassMethod;
|
2019-09-25 17:15:35 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PHPStan\Reflection\ClassReflection;
|
|
|
|
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\Reflection\ReflectionResolver;
|
2019-09-25 17:15:35 +00:00
|
|
|
use ReflectionMethod;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-25 17:15:35 +00:00
|
|
|
/**
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/RFYmn
|
2019-09-30 09:52:33 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector\MakeInheritedMethodVisibilitySameAsParentRectorTest
|
2019-09-25 17:15:35 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class MakeInheritedMethodVisibilitySameAsParentRector extends AbstractRector
|
2019-09-25 17:15:35 +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-05-11 14:46:44 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\Reflection\ReflectionResolver
|
2022-05-11 14:46:44 +00:00
|
|
|
*/
|
|
|
|
private $reflectionResolver;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(VisibilityManipulator $visibilityManipulator, ReflectionResolver $reflectionResolver)
|
2021-11-24 11:00:16 +00:00
|
|
|
{
|
|
|
|
$this->visibilityManipulator = $visibilityManipulator;
|
2022-05-11 14:46:44 +00:00
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
2021-11-24 11:00:16 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-09-25 17:15:35 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Make method visibility same as parent one', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-09-25 17:15:35 +00:00
|
|
|
class ChildClass extends ParentClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ParentClass
|
|
|
|
{
|
|
|
|
protected function run()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-09-25 17:15:35 +00:00
|
|
|
class ChildClass extends ParentClass
|
|
|
|
{
|
|
|
|
protected function run()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ParentClass
|
|
|
|
{
|
|
|
|
protected function run()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-09-25 17:15:35 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-09-25 17:15:35 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-09-25 17:15:35 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2019-09-25 17:15:35 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-05-05 17:42:23 +00:00
|
|
|
* @param Class_ $node
|
2019-09-25 17:15:35 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-09-25 17:15:35 +00:00
|
|
|
{
|
2022-05-11 14:46:44 +00:00
|
|
|
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$classReflection instanceof ClassReflection) {
|
2019-09-25 17:15:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-03-04 21:33:44 +00:00
|
|
|
if ($classReflection->isAnonymous()) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-05-07 00:18:08 +00:00
|
|
|
$parentClassReflections = $classReflection->getParents();
|
|
|
|
if ($parentClassReflections === []) {
|
2022-05-05 17:42:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-05-11 14:46:44 +00:00
|
|
|
$hasChanged = \false;
|
2024-03-10 23:50:43 +00:00
|
|
|
$interfaces = $classReflection->getInterfaces();
|
2022-05-05 17:42:23 +00:00
|
|
|
foreach ($node->getMethods() as $classMethod) {
|
|
|
|
if ($classMethod->isMagic()) {
|
2019-09-25 17:15:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-05-05 17:42:23 +00:00
|
|
|
/** @var string $methodName */
|
|
|
|
$methodName = $this->getName($classMethod->name);
|
2024-03-11 00:12:12 +00:00
|
|
|
if ($classMethod->isPublic()) {
|
|
|
|
foreach ($interfaces as $interface) {
|
|
|
|
if ($interface->hasNativeMethod($methodName)) {
|
|
|
|
continue 2;
|
|
|
|
}
|
2024-03-10 23:50:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-05 17:42:23 +00:00
|
|
|
foreach ($parentClassReflections as $parentClassReflection) {
|
|
|
|
$nativeClassReflection = $parentClassReflection->getNativeReflection();
|
|
|
|
// the class reflection above takes also @method annotations into an account
|
|
|
|
if (!$nativeClassReflection->hasMethod($methodName)) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-05-24 08:37:28 +00:00
|
|
|
/** @var ReflectionMethod $parentReflectionMethod */
|
2022-05-05 17:42:23 +00:00
|
|
|
$parentReflectionMethod = $nativeClassReflection->getMethod($methodName);
|
|
|
|
if ($this->isClassMethodCompatibleWithParentReflectionMethod($classMethod, $parentReflectionMethod)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->changeClassMethodVisibilityBasedOnReflectionMethod($classMethod, $parentReflectionMethod);
|
|
|
|
$hasChanged = \true;
|
2019-09-30 09:52:33 +00:00
|
|
|
}
|
2022-05-05 17:42:23 +00:00
|
|
|
}
|
|
|
|
if ($hasChanged) {
|
2019-09-25 17:15:35 +00:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isClassMethodCompatibleWithParentReflectionMethod(ClassMethod $classMethod, ReflectionMethod $reflectionMethod) : bool
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2019-09-25 17:15:35 +00:00
|
|
|
if ($reflectionMethod->isPublic() && $classMethod->isPublic()) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2019-09-25 17:15:35 +00:00
|
|
|
}
|
|
|
|
if ($reflectionMethod->isProtected() && $classMethod->isProtected()) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2019-09-25 17:15:35 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$reflectionMethod->isPrivate()) {
|
|
|
|
return \false;
|
2020-12-19 15:24:53 +00:00
|
|
|
}
|
|
|
|
return $classMethod->isPrivate();
|
2019-09-25 17:15:35 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function changeClassMethodVisibilityBasedOnReflectionMethod(ClassMethod $classMethod, ReflectionMethod $reflectionMethod) : void
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2019-10-30 09:49:07 +00:00
|
|
|
if ($reflectionMethod->isPublic()) {
|
2021-01-30 21:41:25 +00:00
|
|
|
$this->visibilityManipulator->makePublic($classMethod);
|
2019-10-30 09:49:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($reflectionMethod->isProtected()) {
|
2021-01-30 21:41:25 +00:00
|
|
|
$this->visibilityManipulator->makeProtected($classMethod);
|
2019-10-30 09:49:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($reflectionMethod->isPrivate()) {
|
2021-01-30 21:41:25 +00:00
|
|
|
$this->visibilityManipulator->makePrivate($classMethod);
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-25 17:15:35 +00:00
|
|
|
}
|