Skip to content

Commit

Permalink
Fixed optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 8, 2024
1 parent 2f2d6a3 commit 19cd999
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Parser/VariadicMethodsVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ final class VariadicMethodsVisitor extends NodeVisitorAbstract

private ?string $inMethod = null;

/** @var array<string, array<string, true>> */
/** @var array<string, array<string, bool>> */
public static array $cache = [];

/** @var array<string, array<string, true>> */
/** @var array<string, array<string, bool>> */
private array $variadicMethods = [];

public function beforeTraverse(array $nodes): ?array
Expand Down Expand Up @@ -94,6 +94,10 @@ public function enterNode(Node $node): ?Node
public function leaveNode(Node $node): ?Node
{
if ($node instanceof ClassMethod) {
$lastClass = $this->classStack[count($this->classStack) - 1] ?? null;
if ($lastClass !== null) {
$this->variadicMethods[$lastClass][$this->inMethod] ??= false;
}
$this->inMethod = null;
}

Expand All @@ -111,12 +115,18 @@ public function leaveNode(Node $node): ?Node
public function afterTraverse(array $nodes): ?array
{
if ($this->topNode !== null && $this->variadicMethods !== []) {
$filteredMethods = [];
foreach ($this->variadicMethods as $class => $methods) {
foreach ($methods as $name => $variadic) {
self::$cache[$class][$name] = $variadic;
if (!$variadic) {
continue;
}

$filteredMethods[$class][$name] = true;
}
}
$this->topNode->setAttribute(self::ATTRIBUTE_NAME, $this->variadicMethods);
$this->topNode->setAttribute(self::ATTRIBUTE_NAME, $filteredMethods);
}

return null;
Expand Down

0 comments on commit 19cd999

Please # to comment.