Skip to content

Commit

Permalink
Reduce use of PreviousConnectingVisitor:: ATTRIBUTE_PARENT to assist …
Browse files Browse the repository at this point in the history
…garbage collector (#729)
  • Loading branch information
staabm authored Dec 30, 2024
1 parent 2d12762 commit 675388c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
10 changes: 0 additions & 10 deletions src/Ast/ExpressionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\NodeFinder;
use PHPStan\ShouldNotHappenException;

Expand Down Expand Up @@ -147,15 +146,6 @@ private function findFirstPreviousOfNode(Node $node, callable $filter): ?Node
return $this->findFirstPreviousOfNode($previousStatement, $filter);
}

$parent = $node->getAttribute(PreviousConnectingVisitor::ATTRIBUTE_PARENT);
if ($parent instanceof FunctionLike) {
return null;
}

if ($parent instanceof Node) {
return $this->findFirstPreviousOfNode($parent, $filter);
}

return null;
}

Expand Down
25 changes: 7 additions & 18 deletions src/Ast/PreviousConnectingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@

use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Node\VirtualNode;
use staabm\PHPStanDba\QueryReflection\DIContainerBridge;
use function array_pop;

final class PreviousConnectingVisitor extends NodeVisitorAbstract
{
public const ATTRIBUTE_PARENT = 'dba-parent';

public const ATTRIBUTE_PREVIOUS = 'dba-previous';

/**
* @var list<Node>
*/
private array $stack = [];

private ?Node $previous;

// a dummy property to force instantiation of DIContainerBridge
Expand All @@ -33,33 +26,29 @@ public function __construct(DIContainerBridge $dummyParameter)

public function beforeTraverse(array $nodes)
{
$this->stack = [];
$this->previous = null;

return null;
}

public function enterNode(Node $node)
{
if ([] !== $this->stack) {
$node->setAttribute(self::ATTRIBUTE_PARENT, $this->stack[\count($this->stack) - 1]);
}

if (null !== $this->previous && $this->previous->getAttribute(self::ATTRIBUTE_PARENT) === $node->getAttribute(self::ATTRIBUTE_PARENT)) {
if (
null !== $this->previous
&& ! $this->previous instanceof Node\FunctionLike
&& ! $this->previous instanceof Node\Stmt\ClassLike
&& ! $this->previous instanceof VirtualNode
) {
$node->setAttribute(self::ATTRIBUTE_PREVIOUS, $this->previous);
}

$this->stack[] = $node;

return null;
}

public function leaveNode(Node $node)
{
$this->previous = $node;

array_pop($this->stack);

return null;
}
}

0 comments on commit 675388c

Please sign in to comment.