Skip to content

Commit 69c2201

Browse files
authored
Rollback SimpleCallableNodeTraverser usage on ByRefReturnNodeVisitor (#6624)
1 parent df255c0 commit 69c2201

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefReturnNodeVisitor.php

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\FunctionLike;
9-
use PhpParser\Node\Stmt;
9+
use PhpParser\Node\Stmt\Class_;
1010
use PhpParser\Node\Stmt\Return_;
11-
use PhpParser\Node\Stmt\Switch_;
11+
use PhpParser\NodeVisitor;
1212
use PhpParser\NodeVisitorAbstract;
13-
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
1413
use Rector\NodeTypeResolver\Node\AttributeKey;
1514
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
15+
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
1616

1717
final class ByRefReturnNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface
1818
{
19+
public function __construct(
20+
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser
21+
) {
22+
}
23+
1924
public function enterNode(Node $node): ?Node
2025
{
2126
if (! $node instanceof FunctionLike) {
@@ -31,37 +36,21 @@ public function enterNode(Node $node): ?Node
3136
return null;
3237
}
3338

34-
$this->setByRefAttribute($stmts);
35-
36-
return null;
37-
}
38-
39-
/**
40-
* @param Stmt[] $stmts
41-
*/
42-
private function setByRefAttribute(array $stmts): void
43-
{
44-
foreach ($stmts as $stmt) {
45-
if ($stmt instanceof FunctionLike) {
46-
continue;
47-
}
48-
49-
if ($stmt instanceof StmtsAwareInterface && $stmt->stmts !== null) {
50-
$this->setByRefAttribute($stmt->stmts);
51-
continue;
52-
}
39+
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
40+
$stmts,
41+
static function (Node $node): int|null|Node {
42+
if ($node instanceof Class_ || $node instanceof FunctionLike) {
43+
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
44+
}
5345

54-
if ($stmt instanceof Switch_) {
55-
foreach ($stmt->cases as $case) {
56-
$this->setByRefAttribute($case->stmts);
46+
if (! $node instanceof Return_) {
47+
return null;
5748
}
5849

59-
continue;
60-
}
50+
$node->setAttribute(AttributeKey::IS_BYREF_RETURN, true);
51+
return $node;
52+
});
6153

62-
if ($stmt instanceof Return_) {
63-
$stmt->setAttribute(AttributeKey::IS_BYREF_RETURN, true);
64-
}
65-
}
54+
return null;
6655
}
6756
}

0 commit comments

Comments
 (0)