Skip to content

Commit c985b85

Browse files
committed
Fix bug with nested methods in anonymous classes in methods
1 parent 22d340b commit c985b85

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/Parser/VariadicMethodsVisitor.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ final class VariadicMethodsVisitor extends NodeVisitorAbstract
3030
/** @var array<string> */
3131
private array $classStack = [];
3232

33-
private ?string $inMethod = null;
33+
/** @var array<string> */
34+
private array $inMethodStack = [];
3435

3536
/** @var array<string, array<string, bool>> */
3637
public static array $cache = [];
@@ -45,7 +46,7 @@ public function beforeTraverse(array $nodes): ?array
4546
$this->variadicMethods = [];
4647
$this->inNamespace = null;
4748
$this->classStack = [];
48-
$this->inMethod = null;
49+
$this->inMethodStack = [];
4950

5051
return null;
5152
}
@@ -70,11 +71,13 @@ public function enterNode(Node $node): ?Node
7071
}
7172

7273
if ($node instanceof ClassMethod) {
73-
$this->inMethod = $node->name->name;
74+
$this->inMethodStack[] = $node->name->name;
7475
}
7576

77+
$lastMethod = $this->inMethodStack[count($this->inMethodStack) - 1] ?? null;
78+
7679
if (
77-
$this->inMethod !== null
80+
$lastMethod !== null
7881
&& $node instanceof Node\Expr\FuncCall
7982
&& $node->name instanceof Name
8083
&& in_array((string) $node->name, ParametersAcceptor::VARIADIC_FUNCTIONS, true)
@@ -83,9 +86,9 @@ public function enterNode(Node $node): ?Node
8386
if ($lastClass !== null) {
8487
if (
8588
!array_key_exists($lastClass, $this->variadicMethods)
86-
|| !array_key_exists($this->inMethod, $this->variadicMethods[$lastClass])
89+
|| !array_key_exists($lastMethod, $this->variadicMethods[$lastClass])
8790
) {
88-
$this->variadicMethods[$lastClass][$this->inMethod] = true;
91+
$this->variadicMethods[$lastClass][$lastMethod] = true;
8992
}
9093
}
9194

@@ -99,10 +102,11 @@ public function leaveNode(Node $node): ?Node
99102
{
100103
if ($node instanceof ClassMethod) {
101104
$lastClass = $this->classStack[count($this->classStack) - 1] ?? null;
102-
if ($lastClass !== null) {
103-
$this->variadicMethods[$lastClass][$this->inMethod] ??= false;
105+
$lastMethod = $this->inMethodStack[count($this->inMethodStack) - 1] ?? null;
106+
if ($lastClass !== null && $lastMethod !== null) {
107+
$this->variadicMethods[$lastClass][$lastMethod] ??= false;
104108
}
105-
$this->inMethod = null;
109+
array_pop($this->inMethodStack);
106110
}
107111

108112
if ($node instanceof Node\Stmt\ClassLike) {

0 commit comments

Comments
 (0)