Skip to content

Commit

Permalink
Performance: reduce parent attribute usage on BetterNodeFinder (#3504)
Browse files Browse the repository at this point in the history
* Performance: reduce parent type call usage on BetterNodeFinder

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Mar 22, 2023
1 parent 5e29a35 commit 5c985c8
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeFinder;
use PhpParser\NodeTraverser;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\Util\MultiInstanceofChecker;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Webmozart\Assert\Assert;

/**
Expand All @@ -39,7 +41,8 @@ public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly NodeComparator $nodeComparator,
private readonly ClassAnalyzer $classAnalyzer,
private readonly MultiInstanceofChecker $multiInstanceofChecker
private readonly MultiInstanceofChecker $multiInstanceofChecker,
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser
) {
}

Expand Down Expand Up @@ -234,28 +237,37 @@ public function findFirst(Node | array $nodes, callable $filter): ?Node
*/
public function findClassMethodAssignsToLocalProperty(ClassMethod $classMethod, string $propertyName): array
{
return $this->find((array) $classMethod->stmts, function (Node $node) use ($classMethod, $propertyName): bool {
/** @var Assign[] $assigns */
$assigns = [];

$this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) use ($propertyName, &$assigns): int|null|Assign {
// skip anonymous classes and inner function
if ($node instanceof Class_ || $node instanceof Function_) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

if (! $node instanceof Assign) {
return false;
return null;
}

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

$propertyFetch = $node->var;
if (! $this->nodeNameResolver->isName($propertyFetch->var, 'this')) {
return false;
return null;
}

$parentFunctionLike = $this->findParentType($node, ClassMethod::class);

if ($parentFunctionLike !== $classMethod) {
return false;
if (! $this->nodeNameResolver->isName($propertyFetch->name, $propertyName)) {
return null;
}

return $this->nodeNameResolver->isName($propertyFetch->name, $propertyName);
$assigns[] = $node;
return $node;
});

return $assigns;
}

/**
Expand Down

0 comments on commit 5c985c8

Please sign in to comment.