2020-10-17 15:29:10 +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\MethodCall;
|
2020-10-17 15:29:10 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\ConstFetch;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassLike;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Enum_;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Interface_;
|
|
|
|
use PhpParser\Node\Stmt\Trait_;
|
|
|
|
use PHPStan\Analyser\Scope;
|
2022-10-13 11:06:06 +00:00
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Type\ThisType;
|
|
|
|
use PHPStan\Type\TypeWithClassName;
|
|
|
|
use Rector\Core\NodeAnalyzer\CallAnalyzer;
|
|
|
|
use Rector\Core\PhpParser\AstResolver;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2022-10-13 11:06:06 +00:00
|
|
|
use Rector\Core\Reflection\ReflectionResolver;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-10-17 15:29:10 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector\RemoveEmptyMethodCallRectorTest
|
2020-10-17 15:29:10 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveEmptyMethodCallRector extends AbstractRector
|
2020-10-17 15:29:10 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-06-26 11:50:38 +00:00
|
|
|
* @var \Rector\Core\PhpParser\AstResolver
|
2020-10-17 15:29:10 +00:00
|
|
|
*/
|
2021-06-26 11:50:38 +00:00
|
|
|
private $reflectionAstResolver;
|
2021-07-28 11:11:32 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-07-28 11:11:32 +00:00
|
|
|
* @var \Rector\Core\NodeAnalyzer\CallAnalyzer
|
|
|
|
*/
|
|
|
|
private $callAnalyzer;
|
2022-10-13 11:06:06 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Core\Reflection\ReflectionResolver
|
|
|
|
*/
|
|
|
|
private $reflectionResolver;
|
|
|
|
public function __construct(AstResolver $reflectionAstResolver, CallAnalyzer $callAnalyzer, ReflectionResolver $reflectionResolver)
|
2020-10-17 15:29:10 +00:00
|
|
|
{
|
2021-06-26 11:50:38 +00:00
|
|
|
$this->reflectionAstResolver = $reflectionAstResolver;
|
2021-07-28 11:11:32 +00:00
|
|
|
$this->callAnalyzer = $callAnalyzer;
|
2022-10-13 11:06:06 +00:00
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
2020-10-17 15:29:10 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-10-17 15:29:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove empty method call', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-10-17 15:29:10 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function callThis()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$some = new SomeClass();
|
|
|
|
$some->callThis();
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-10-17 15:29:10 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function callThis()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$some = new SomeClass();
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-10-17 15:29:10 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-10-17 15:29:10 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-10-17 15:29:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [MethodCall::class];
|
2020-10-17 15:29:10 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param MethodCall $node
|
2020-10-17 15:29:10 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-10-17 15:29:10 +00:00
|
|
|
{
|
2021-07-27 11:16:44 +00:00
|
|
|
$scope = $this->getScope($node);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$scope instanceof Scope) {
|
2020-10-17 15:29:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$type = $scope->getType($node->var);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$type instanceof TypeWithClassName) {
|
2021-07-05 09:49:56 +00:00
|
|
|
return null;
|
2020-11-13 11:14:25 +00:00
|
|
|
}
|
2022-10-13 11:06:06 +00:00
|
|
|
$classLike = $this->resolveClassLike($node);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$classLike instanceof ClassLike) {
|
2020-10-17 15:29:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-10-13 11:06:06 +00:00
|
|
|
/** @var Class_|Trait_|Interface_|Enum_ $classLike */
|
2021-08-11 12:48:37 +00:00
|
|
|
if ($this->shouldSkipClassMethod($classLike, $node, $type)) {
|
2020-10-17 15:29:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-10-19 14:38:02 +00:00
|
|
|
// if->cond cannot removed, it has to be replaced with false, see https://3v4l.org/U9S9i
|
2022-08-09 13:39:17 +00:00
|
|
|
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
if ($parentNode instanceof If_ && $parentNode->cond === $node) {
|
2021-01-30 21:41:25 +00:00
|
|
|
return $this->nodeFactory->createFalse();
|
2020-10-19 14:38:02 +00:00
|
|
|
}
|
2022-08-09 13:39:17 +00:00
|
|
|
if ($parentNode instanceof Assign) {
|
2021-01-30 21:41:25 +00:00
|
|
|
return $this->nodeFactory->createFalse();
|
2020-12-02 11:44:56 +00:00
|
|
|
}
|
2022-08-09 13:39:17 +00:00
|
|
|
if ($parentNode instanceof ArrowFunction && $this->nodeComparator->areNodesEqual($parentNode->expr, $node)) {
|
|
|
|
return $this->processArrowFunction($parentNode, $node);
|
2021-01-29 07:42:34 +00:00
|
|
|
}
|
2022-08-09 13:39:17 +00:00
|
|
|
if (!$parentNode instanceof Expression) {
|
2022-04-09 23:52:05 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-10-19 13:35:37 +00:00
|
|
|
$this->removeNode($node);
|
2020-10-17 15:29:10 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2022-10-13 11:06:06 +00:00
|
|
|
private function resolveClassLike(MethodCall $methodCall) : ?ClassLike
|
|
|
|
{
|
|
|
|
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($methodCall);
|
|
|
|
if (!$classReflection instanceof ClassReflection) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->reflectionAstResolver->resolveClassFromName($classReflection->getName());
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function getScope(MethodCall $methodCall) : ?Scope
|
2021-07-27 11:16:44 +00:00
|
|
|
{
|
2021-07-28 11:11:32 +00:00
|
|
|
if ($this->callAnalyzer->isObjectCall($methodCall->var)) {
|
2021-07-27 11:16:44 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$parentArg = $this->betterNodeFinder->findParentType($methodCall, Arg::class);
|
|
|
|
if ($parentArg instanceof Arg) {
|
2021-08-07 11:36:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-08-09 13:39:17 +00:00
|
|
|
return $methodCall->var->getAttribute(AttributeKey::SCOPE);
|
2021-07-27 11:16:44 +00:00
|
|
|
}
|
2021-07-05 07:47:59 +00:00
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Trait_|\PhpParser\Node\Stmt\Interface_|\PhpParser\Node\Stmt\Enum_ $classLike
|
2021-07-05 07:47:59 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkipClassMethod($classLike, MethodCall $methodCall, TypeWithClassName $typeWithClassName) : bool
|
2020-10-17 15:29:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$classLike instanceof Class_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-01-29 07:42:34 +00:00
|
|
|
}
|
2020-10-19 13:35:37 +00:00
|
|
|
$methodName = $this->getName($methodCall->name);
|
|
|
|
if ($methodName === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-10-19 13:35:37 +00:00
|
|
|
}
|
2021-07-05 09:49:56 +00:00
|
|
|
$classMethod = $classLike->getMethod($methodName);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$classMethod instanceof ClassMethod) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-10-17 15:29:10 +00:00
|
|
|
}
|
2020-10-19 13:35:37 +00:00
|
|
|
if ($classMethod->isAbstract()) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-10-19 13:35:37 +00:00
|
|
|
}
|
2021-08-11 12:48:37 +00:00
|
|
|
if ((array) $classMethod->stmts !== []) {
|
|
|
|
return \true;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$class = $this->betterNodeFinder->findParentType($methodCall, Class_::class);
|
|
|
|
if (!$class instanceof Class_) {
|
2021-08-11 12:48:37 +00:00
|
|
|
return \false;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$typeWithClassName instanceof ThisType) {
|
2021-08-13 20:43:09 +00:00
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if ($class->isFinal()) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return !$classMethod->isPrivate();
|
2020-10-17 15:29:10 +00:00
|
|
|
}
|
2021-06-18 17:31:16 +00:00
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\ConstFetch
|
2021-06-18 17:31:16 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processArrowFunction(ArrowFunction $arrowFunction, MethodCall $methodCall)
|
2021-01-29 07:42:34 +00:00
|
|
|
{
|
2022-08-09 13:39:17 +00:00
|
|
|
$parentParentNode = $arrowFunction->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
if ($parentParentNode instanceof Expression) {
|
2021-01-29 07:42:34 +00:00
|
|
|
$this->removeNode($arrowFunction);
|
|
|
|
return $methodCall;
|
|
|
|
}
|
2021-01-30 21:41:25 +00:00
|
|
|
return $this->nodeFactory->createFalse();
|
2021-01-29 07:42:34 +00:00
|
|
|
}
|
2020-10-17 15:29:10 +00:00
|
|
|
}
|