Merge pull request #3778 from rectorphp/remove-callee

remove MethodCallNodeVisitor, use better parent traversal approach
This commit is contained in:
Tomas Votruba 2020-07-24 22:44:48 +02:00 committed by GitHub
commit c03bd46e1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 86 deletions

View File

@ -161,11 +161,6 @@ final class AttributeKey
*/
public const PHP_DOC_INFO = PhpDocInfo::class;
/**
* @var string
*/
public const METHOD_CALL_NODE_CALLER_NAME = 'methodCallVariableName';
/**
* @var string
*/

View File

@ -12,7 +12,6 @@ use Rector\Core\Configuration\Configuration;
use Rector\NodeCollector\NodeVisitor\NodeCollectorNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FileInfoNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FunctionMethodAndClassNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\MethodCallNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\NamespaceNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\ParentAndNextNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\PhpDocInfoNodeVisitor;
@ -72,11 +71,6 @@ final class NodeScopeAndMetadataDecorator
*/
private $phpDocInfoNodeVisitor;
/**
* @var MethodCallNodeVisitor
*/
private $methodCallNodeVisitor;
public function __construct(
PHPStanNodeScopeResolver $phpStanNodeScopeResolver,
ParentAndNextNodeVisitor $parentAndNextNodeVisitor,
@ -87,8 +81,7 @@ final class NodeScopeAndMetadataDecorator
FileInfoNodeVisitor $fileInfoNodeVisitor,
NodeCollectorNodeVisitor $nodeCollectorNodeVisitor,
PhpDocInfoNodeVisitor $phpDocInfoNodeVisitor,
Configuration $configuration,
MethodCallNodeVisitor $methodCallNodeVisitor
Configuration $configuration
) {
$this->phpStanNodeScopeResolver = $phpStanNodeScopeResolver;
$this->parentAndNextNodeVisitor = $parentAndNextNodeVisitor;
@ -100,7 +93,6 @@ final class NodeScopeAndMetadataDecorator
$this->nodeCollectorNodeVisitor = $nodeCollectorNodeVisitor;
$this->configuration = $configuration;
$this->phpDocInfoNodeVisitor = $phpDocInfoNodeVisitor;
$this->methodCallNodeVisitor = $methodCallNodeVisitor;
}
/**
@ -139,7 +131,6 @@ final class NodeScopeAndMetadataDecorator
$nodeTraverser->addVisitor($this->parentAndNextNodeVisitor);
$nodeTraverser->addVisitor($this->functionMethodAndClassNodeVisitor);
$nodeTraverser->addVisitor($this->namespaceNodeVisitor);
$nodeTraverser->addVisitor($this->methodCallNodeVisitor);
$nodeTraverser->addVisitor($this->phpDocInfoNodeVisitor);
$nodes = $nodeTraverser->traverse($nodes);

View File

@ -1,67 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\NodeTypeResolver\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class MethodCallNodeVisitor extends NodeVisitorAbstract
{
/**
* @var Expr|Name
*/
private $callerNode;
/**
* @var NodeNameResolver
*/
private $nodeNameResolver;
public function __construct(NodeNameResolver $nodeNameResolver)
{
$this->nodeNameResolver = $nodeNameResolver;
}
/**
* @return int|Node|void|null
*/
public function enterNode(Node $node)
{
$this->processMethodCall($node);
return $node;
}
private function processMethodCall(Node $node): void
{
if (! $node instanceof MethodCall) {
return;
}
$callerNode = $node->var;
if ($callerNode instanceof MethodCall) {
while ($callerNode instanceof MethodCall) {
$callerNode = $callerNode->var;
}
}
if ($callerNode instanceof StaticCall) {
while ($callerNode instanceof StaticCall) {
$callerNode = $callerNode->class;
}
}
$this->callerNode = $callerNode;
$currentCallerName = $this->nodeNameResolver->getName($this->callerNode);
$node->setAttribute(AttributeKey::METHOD_CALL_NODE_CALLER_NAME, $currentCallerName);
}
}

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\Core\PhpParser\Node\Manipulator;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
@ -113,15 +114,17 @@ final class MethodCallManipulator
return [];
}
return $this->betterNodeFinder->find($scopeNode, function (Node $node) use ($variable) {
$variableName = $this->nodeNameResolver->getName($variable);
return $this->betterNodeFinder->find($scopeNode, function (Node $node) use ($variableName) {
if (! $node instanceof MethodCall) {
return false;
}
/** @var string $methodCallVariableName */
$methodCallVariableName = $node->getAttribute(AttributeKey::METHOD_CALL_NODE_CALLER_NAME);
// cover fluent interfaces too
$callerNode = $this->resolveRootVariable($node);
return $this->nodeNameResolver->isName($variable, $methodCallVariableName);
return $this->nodeNameResolver->isName($callerNode, $variableName);
});
}
@ -179,4 +182,14 @@ final class MethodCallManipulator
return $parentNode;
}
private function resolveRootVariable(MethodCall $methodCall): Expr
{
$callerNode = $methodCall->var;
while ($callerNode instanceof MethodCall) {
$callerNode = $callerNode->var;
}
return $callerNode;
}
}