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\DeadCode\Rector\ClassMethod;
|
2019-02-18 17:53:29 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2023-05-15 08:51:25 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Reflection\ClassReflection;
|
|
|
|
use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
|
|
|
use Rector\Reflection\ReflectionResolver;
|
|
|
|
use Rector\ValueObject\MethodName;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\RemoveUnusedPrivateMethodRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2023-05-15 08:51:25 +00:00
|
|
|
final class RemoveUnusedPrivateMethodRector extends AbstractScopeAwareRector
|
2019-02-18 17:53:29 +00:00
|
|
|
{
|
2021-06-24 10:09:03 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-08-06 06:45:08 +00:00
|
|
|
* @var \Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer
|
2021-06-24 10:09:03 +00:00
|
|
|
*/
|
2021-08-06 06:45:08 +00:00
|
|
|
private $isClassMethodUsedAnalyzer;
|
2022-05-04 10:46:11 +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-04 10:46:11 +00:00
|
|
|
*/
|
|
|
|
private $reflectionResolver;
|
2023-09-20 12:55:35 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
2023-09-20 12:55:35 +00:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
|
|
|
public function __construct(IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer, ReflectionResolver $reflectionResolver, BetterNodeFinder $betterNodeFinder)
|
2021-06-24 10:09:03 +00:00
|
|
|
{
|
2021-08-06 06:45:08 +00:00
|
|
|
$this->isClassMethodUsedAnalyzer = $isClassMethodUsedAnalyzer;
|
2022-05-04 10:46:11 +00:00
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
2023-09-20 12:55:35 +00:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2021-06-24 10:09:03 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-02-18 17:53:29 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove unused private method', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-02-18 17:53:29 +00:00
|
|
|
final class SomeController
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function skip()
|
|
|
|
{
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-02-18 17:53:29 +00:00
|
|
|
final class SomeController
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-02-18 17:53:29 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-02-18 17:53:29 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-02-18 17:53:29 +00:00
|
|
|
{
|
2023-05-28 21:02:39 +00:00
|
|
|
return [Class_::class];
|
2019-02-18 17:53:29 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-28 21:02:39 +00:00
|
|
|
* @param Class_ $node
|
2019-02-18 17:53:29 +00:00
|
|
|
*/
|
2023-05-15 08:51:25 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2019-02-18 17:53:29 +00:00
|
|
|
{
|
2024-03-04 17:14:34 +00:00
|
|
|
$classMethods = $node->getMethods();
|
|
|
|
if ($classMethods === []) {
|
2019-05-07 12:46:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-03-04 17:14:34 +00:00
|
|
|
$filter = static function (ClassMethod $classMethod) : bool {
|
|
|
|
return $classMethod->isPrivate();
|
|
|
|
};
|
|
|
|
if (\array_filter($classMethods, $filter) === []) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($this->hasDynamicMethodCallOnFetchThis($classMethods)) {
|
2023-06-04 18:10:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-28 21:02:39 +00:00
|
|
|
$hasChanged = \false;
|
2023-06-04 18:10:51 +00:00
|
|
|
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
|
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof ClassMethod) {
|
|
|
|
continue;
|
2023-05-28 21:08:58 +00:00
|
|
|
}
|
2023-06-04 18:10:51 +00:00
|
|
|
if ($this->shouldSkip($stmt, $classReflection)) {
|
2023-05-28 21:02:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-04 18:10:51 +00:00
|
|
|
if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($node, $stmt, $scope)) {
|
2023-05-28 21:02:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-04 18:10:51 +00:00
|
|
|
unset($node->stmts[$key]);
|
2023-05-28 21:02:39 +00:00
|
|
|
$hasChanged = \true;
|
2019-04-26 08:33:40 +00:00
|
|
|
}
|
2023-05-28 21:02:39 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2021-11-02 08:30:09 +00:00
|
|
|
}
|
2023-04-03 17:20:08 +00:00
|
|
|
return null;
|
2019-07-09 17:36:42 +00:00
|
|
|
}
|
2023-05-28 21:08:58 +00:00
|
|
|
private function shouldSkip(ClassMethod $classMethod, ?ClassReflection $classReflection) : bool
|
2019-07-09 17:36:42 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$classReflection instanceof ClassReflection) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2019-02-18 17:53:29 +00:00
|
|
|
}
|
2024-03-04 17:14:34 +00:00
|
|
|
// unreliable to detect trait, interface, anonymous class: doesn't make sense
|
2021-02-28 07:47:48 +00:00
|
|
|
if ($classReflection->isTrait()) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-02-28 07:47:48 +00:00
|
|
|
}
|
|
|
|
if ($classReflection->isInterface()) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2019-03-15 15:08:43 +00:00
|
|
|
}
|
2021-02-28 07:47:48 +00:00
|
|
|
if ($classReflection->isAnonymous()) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2019-02-18 17:53:29 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$classMethod->isPrivate()) {
|
|
|
|
return \true;
|
2019-07-09 17:36:42 +00:00
|
|
|
}
|
|
|
|
// skip magic methods - @see https://www.php.net/manual/en/language.oop5.magic.php
|
2021-03-21 09:52:15 +00:00
|
|
|
if ($classMethod->isMagic()) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-03-21 09:52:15 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return $classReflection->hasMethod(MethodName::CALL);
|
2021-06-24 10:09:03 +00:00
|
|
|
}
|
2024-03-04 17:14:34 +00:00
|
|
|
/**
|
|
|
|
* @param ClassMethod[] $classMethods
|
|
|
|
*/
|
|
|
|
private function hasDynamicMethodCallOnFetchThis(array $classMethods) : bool
|
2021-11-02 08:30:09 +00:00
|
|
|
{
|
2024-03-04 17:14:34 +00:00
|
|
|
foreach ($classMethods as $classMethod) {
|
2023-05-28 21:02:39 +00:00
|
|
|
$isFound = (bool) $this->betterNodeFinder->findFirst((array) $classMethod->getStmts(), function (Node $subNode) : bool {
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$subNode instanceof MethodCall) {
|
2021-11-02 08:30:09 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$subNode->var instanceof Variable) {
|
2021-11-02 08:30:09 +00:00
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if (!$this->nodeNameResolver->isName($subNode->var, 'this')) {
|
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return $subNode->name instanceof Variable;
|
2021-11-02 08:30:09 +00:00
|
|
|
});
|
|
|
|
if ($isFound) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \false;
|
|
|
|
}
|
2019-02-18 17:53:29 +00:00
|
|
|
}
|