Skip to content

Commit

Permalink
cleanup MethodCallManipulator (#2135)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Apr 23, 2022
1 parent 7bc7426 commit ccd3ba6
Showing 1 changed file with 0 additions and 108 deletions.
108 changes: 0 additions & 108 deletions src/NodeManipulator/MethodCallManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
namespace Rector\Core\NodeManipulator;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Defluent\NodeAnalyzer\FluentChainMethodCallNodeAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class MethodCallManipulator
{
Expand Down Expand Up @@ -45,55 +41,6 @@ public function findMethodCallNamesOnVariable(Variable $variable): array
return array_unique($methodCallNamesOnVariable);
}

/**
* @return MethodCall[]
*/
public function findMethodCallsIncludingChain(MethodCall $methodCall): array
{
$chainMethodCalls = [];

// 1. collect method chain call
$currentMethodCallee = $methodCall->var;
while ($currentMethodCallee instanceof MethodCall) {
$chainMethodCalls[] = $currentMethodCallee;
$currentMethodCallee = $currentMethodCallee->var;
}

// 2. collect on-same-variable calls
$onVariableMethodCalls = [];
if ($currentMethodCallee instanceof Variable) {
$onVariableMethodCalls = $this->findMethodCallsOnVariable($currentMethodCallee);
}

$methodCalls = array_merge($chainMethodCalls, $onVariableMethodCalls);

return $this->uniquateObjects($methodCalls);
}

public function findAssignToVariable(Variable $variable): ?Assign
{
$parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Node) {
return null;
}

$variableName = $this->nodeNameResolver->getName($variable);
if ($variableName === null) {
return null;
}

do {
$assign = $this->findAssignToVariableName($parentNode, $variableName);
if ($assign instanceof Assign) {
return $assign;
}

$parentNode = $this->resolvePreviousNodeInSameScope($parentNode);
} while ($parentNode instanceof Node && ! $parentNode instanceof FunctionLike);

return null;
}

/**
* @return MethodCall[]
*/
Expand Down Expand Up @@ -126,59 +73,4 @@ public function findMethodCallsOnVariable(Variable $variable): array
return $this->nodeNameResolver->isName($callerNode, $variableName);
});
}

/**
* @see https://stackoverflow.com/a/4507991/1348344
* @param object[] $objects
* @return object[]
*
* @template T
* @phpstan-param array<T>|T[] $objects
* @phpstan-return array<T>|T[]
*/
private function uniquateObjects(array $objects): array
{
$uniqueObjects = [];
foreach ($objects as $object) {
if (in_array($object, $uniqueObjects, true)) {
continue;
}

$uniqueObjects[] = $object;
}

// re-index
return array_values($uniqueObjects);
}

private function findAssignToVariableName(Node $node, string $variableName): ?Node
{
return $this->betterNodeFinder->findFirst($node, function (Node $node) use ($variableName): bool {
if (! $node instanceof Assign) {
return false;
}

if (! $node->var instanceof Variable) {
return false;
}

return $this->nodeNameResolver->isName($node->var, $variableName);
});
}

private function resolvePreviousNodeInSameScope(Node $parentNode): ?Node
{
$previousParentNode = $parentNode;
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);

if (! $parentNode instanceof FunctionLike) {
// is about to leave → try previous expression
$previousStatement = $previousParentNode->getAttribute(AttributeKey::PREVIOUS_STATEMENT);
if ($previousStatement instanceof Expression) {
return $previousStatement->expr;
}
}

return $parentNode;
}
}

0 comments on commit ccd3ba6

Please sign in to comment.