From 350042dba417e1493fa3e5bae178b9d02140541f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 2 Jan 2024 20:43:50 +0000 Subject: [PATCH] Updated Rector to commit 9144efc05cf4d084d4af69f4df0be9d7a66e5869 https://github.com/rectorphp/rector-src/commit/9144efc05cf4d084d4af69f4df0be9d7a66e5869 Keep called method in RemoveEmptyClassMethodRector (#5425) --- .../RemoveEmptyClassMethodRector.php | 36 ++++++++++++++++++- src/Application/VersionResolver.php | 4 +-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php index ddf3e883b63..ddc535be930 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveEmptyClassMethodRector.php @@ -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); + }); + } } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 3ad142ced5f..b9593882d94 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -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 */