From 675388c2a557ad6199dc7dbbb4257087a5c0ef60 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Mon, 30 Dec 2024 13:13:12 +0100 Subject: [PATCH] Reduce use of PreviousConnectingVisitor:: ATTRIBUTE_PARENT to assist garbage collector (#729) --- src/Ast/ExpressionFinder.php | 10 ---------- src/Ast/PreviousConnectingVisitor.php | 25 +++++++------------------ 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/src/Ast/ExpressionFinder.php b/src/Ast/ExpressionFinder.php index d24ec7187..492b569ed 100644 --- a/src/Ast/ExpressionFinder.php +++ b/src/Ast/ExpressionFinder.php @@ -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; @@ -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; } diff --git a/src/Ast/PreviousConnectingVisitor.php b/src/Ast/PreviousConnectingVisitor.php index a5657bd0a..db9111761 100644 --- a/src/Ast/PreviousConnectingVisitor.php +++ b/src/Ast/PreviousConnectingVisitor.php @@ -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 - */ - private array $stack = []; - private ?Node $previous; // a dummy property to force instantiation of DIContainerBridge @@ -33,7 +26,6 @@ public function __construct(DIContainerBridge $dummyParameter) public function beforeTraverse(array $nodes) { - $this->stack = []; $this->previous = null; return null; @@ -41,16 +33,15 @@ public function beforeTraverse(array $nodes) 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; } @@ -58,8 +49,6 @@ public function leaveNode(Node $node) { $this->previous = $node; - array_pop($this->stack); - return null; } }