Updated Rector to commit 9144efc05cf4d084d4af69f4df0be9d7a66e5869

9144efc05c Keep called method in RemoveEmptyClassMethodRector (#5425)
This commit is contained in:
Tomas Votruba 2024-01-02 20:43:50 +00:00
parent 3a49fcd8d5
commit 350042dba4
2 changed files with 37 additions and 3 deletions

View File

@ -4,6 +4,8 @@ declare (strict_types=1);
namespace Rector\DeadCode\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
@ -13,6 +15,7 @@ use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\DeadCode\NodeManipulator\ControllerClassMethodManipulator;
use Rector\NodeAnalyzer\ParamAnalyzer;
use Rector\NodeManipulator\ClassMethodManipulator;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -42,12 +45,18 @@ final class RemoveEmptyClassMethodRector extends AbstractRector
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;
public function __construct(ClassMethodManipulator $classMethodManipulator, ControllerClassMethodManipulator $controllerClassMethodManipulator, ParamAnalyzer $paramAnalyzer, PhpDocInfoFactory $phpDocInfoFactory)
/**
* @readonly
* @var \Rector\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
public function __construct(ClassMethodManipulator $classMethodManipulator, ControllerClassMethodManipulator $controllerClassMethodManipulator, ParamAnalyzer $paramAnalyzer, PhpDocInfoFactory $phpDocInfoFactory, BetterNodeFinder $betterNodeFinder)
{
$this->classMethodManipulator = $classMethodManipulator;
$this->controllerClassMethodManipulator = $controllerClassMethodManipulator;
$this->paramAnalyzer = $paramAnalyzer;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->betterNodeFinder = $betterNodeFinder;
}
public function getRuleDefinition() : RuleDefinition
{
@ -121,6 +130,16 @@ CODE_SAMPLE
}
private function shouldSkipClassMethod(Class_ $class, ClassMethod $classMethod) : bool
{
$desiredClassMethodName = $this->getName($classMethod);
// is method called somewhere else in the class?
foreach ($class->getMethods() as $anotherClassMethod) {
if ($anotherClassMethod === $classMethod) {
continue;
}
if ($this->containsMethodCall($anotherClassMethod, $desiredClassMethodName)) {
return \true;
}
}
if ($this->classMethodManipulator->isNamedConstructor($classMethod)) {
return \true;
}
@ -150,4 +169,19 @@ CODE_SAMPLE
}
return $phpDocInfo->hasByType(DeprecatedTagValueNode::class);
}
private function containsMethodCall(ClassMethod $anotherClassMethod, string $desiredClassMethodName) : bool
{
return (bool) $this->betterNodeFinder->findFirst($anotherClassMethod, function (Node $node) use($desiredClassMethodName) : bool {
if (!$node instanceof MethodCall) {
return \false;
}
if (!$node->var instanceof Variable) {
return \false;
}
if (!$this->isName($node->var, 'this')) {
return \false;
}
return $this->isName($node->name, $desiredClassMethodName);
});
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '8724feefea91743e05006cb1329c5cd1675b5003';
public const PACKAGE_VERSION = '9144efc05cf4d084d4af69f4df0be9d7a66e5869';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-01-02 20:14:35';
public const RELEASE_DATE = '2024-01-02 20:41:33';
/**
* @var int
*/