Skip to content

Commit

Permalink
[Performance] Remove SimpleCallableNodeTraverser usage on ByRefReturn…
Browse files Browse the repository at this point in the history
…NodeVisitor
  • Loading branch information
samsonasik committed Dec 20, 2024
1 parent f67bc4e commit 4e08252
Showing 1 changed file with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@

use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;

final class ByRefReturnNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface
{
public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser
) {
}

public function enterNode(Node $node): ?Node
{
if (! $node instanceof FunctionLike) {
Expand All @@ -36,22 +29,29 @@ public function enterNode(Node $node): ?Node
return null;
}

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$stmts,
static function (Node $node): int|null|Node {
if ($node instanceof Class_ || $node instanceof FunctionLike) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
$this->setByRefAttribute($stmts);

return null;
}

if (! $node instanceof Return_) {
return null;
}
/**
* @param Stmt[] $stmts
*/
private function setByRefAttribute(array $stmts)
{
foreach ($stmts as $stmt) {
if ($stmt instanceof FunctionLike) {
continue;
}

$node->setAttribute(AttributeKey::IS_BYREF_RETURN, true);
return $node;
if ($stmt instanceof StmtsAwareInterface && $stmt->stmts !== null) {
$this->setByRefAttribute($stmt->stmts);
continue;
}
);

return null;
if ($stmt instanceof Return_) {
$stmt->setAttribute(AttributeKey::IS_BYREF_RETURN, true);
}
}
}
}

0 comments on commit 4e08252

Please sign in to comment.