Skip to content

Commit

Permalink
[Performance] Reduce parent lookup on PropertyFetchAnalyzer (#4034)
Browse files Browse the repository at this point in the history
* [Performance] Reduce parent lookup on PropertyFetchAnalyzer

* lookup on methods

* Fix return

* fix phpstan
  • Loading branch information
samsonasik committed May 31, 2023
1 parent b84fe95 commit 227186b
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/NodeAnalyzer/PropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeTraverser;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use Rector\Core\Enum\ObjectReference;
Expand Down Expand Up @@ -89,26 +91,20 @@ public function countLocalPropertyFetchName(Class_ $class, string $propertyName)
{
$total = 0;

$this->simpleCallableNodeTraverser->traverseNodesWithCallable($class->stmts, function (Node $subNode) use (
$class,
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($class->getMethods(), function (Node $subNode) use (
$propertyName,
&$total
): ?Node {
if (! $this->isLocalPropertyFetchName($subNode, $propertyName)) {
return null;
): int|null|Node {
// skip anonymous classes and inner function
if ($subNode instanceof Class_ || $subNode instanceof Function_) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

$parentClassLike = $this->betterNodeFinder->findParentType($subNode, ClassLike::class);

// property fetch in Trait cannot get parent ClassLike
if (! $parentClassLike instanceof ClassLike) {
++$total;
}

if ($parentClassLike === $class) {
++$total;
if (! $this->isLocalPropertyFetchName($subNode, $propertyName)) {
return null;
}

++$total;
return $subNode;
});

Expand Down

0 comments on commit 227186b

Please sign in to comment.