Skip to content

Commit

Permalink
Rollback SimpleCallableNodeTraverser usage on ByRefReturnNodeVisitor (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik authored Dec 20, 2024
1 parent df255c0 commit 69c2201
Showing 1 changed file with 20 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@

use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
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 @@ -31,37 +36,21 @@ public function enterNode(Node $node): ?Node
return null;
}

$this->setByRefAttribute($stmts);

return null;
}

/**
* @param Stmt[] $stmts
*/
private function setByRefAttribute(array $stmts): void
{
foreach ($stmts as $stmt) {
if ($stmt instanceof FunctionLike) {
continue;
}

if ($stmt instanceof StmtsAwareInterface && $stmt->stmts !== null) {
$this->setByRefAttribute($stmt->stmts);
continue;
}
$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;
}

if ($stmt instanceof Switch_) {
foreach ($stmt->cases as $case) {
$this->setByRefAttribute($case->stmts);
if (! $node instanceof Return_) {
return null;
}

continue;
}
$node->setAttribute(AttributeKey::IS_BYREF_RETURN, true);
return $node;
});

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

0 comments on commit 69c2201

Please sign in to comment.