Skip to content

Commit 675388c

Browse files
authored
Reduce use of PreviousConnectingVisitor:: ATTRIBUTE_PARENT to assist garbage collector (#729)
1 parent 2d12762 commit 675388c

File tree

2 files changed

+7
-28
lines changed

2 files changed

+7
-28
lines changed

src/Ast/ExpressionFinder.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpParser\Node\Expr\AssignOp;
1111
use PhpParser\Node\Expr\MethodCall;
1212
use PhpParser\Node\Expr\Variable;
13-
use PhpParser\Node\FunctionLike;
1413
use PhpParser\NodeFinder;
1514
use PHPStan\ShouldNotHappenException;
1615

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

150-
$parent = $node->getAttribute(PreviousConnectingVisitor::ATTRIBUTE_PARENT);
151-
if ($parent instanceof FunctionLike) {
152-
return null;
153-
}
154-
155-
if ($parent instanceof Node) {
156-
return $this->findFirstPreviousOfNode($parent, $filter);
157-
}
158-
159149
return null;
160150
}
161151

src/Ast/PreviousConnectingVisitor.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@
66

77
use PhpParser\Node;
88
use PhpParser\NodeVisitorAbstract;
9+
use PHPStan\Node\VirtualNode;
910
use staabm\PHPStanDba\QueryReflection\DIContainerBridge;
10-
use function array_pop;
1111

1212
final class PreviousConnectingVisitor extends NodeVisitorAbstract
1313
{
14-
public const ATTRIBUTE_PARENT = 'dba-parent';
15-
1614
public const ATTRIBUTE_PREVIOUS = 'dba-previous';
1715

18-
/**
19-
* @var list<Node>
20-
*/
21-
private array $stack = [];
22-
2316
private ?Node $previous;
2417

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

3427
public function beforeTraverse(array $nodes)
3528
{
36-
$this->stack = [];
3729
$this->previous = null;
3830

3931
return null;
4032
}
4133

4234
public function enterNode(Node $node)
4335
{
44-
if ([] !== $this->stack) {
45-
$node->setAttribute(self::ATTRIBUTE_PARENT, $this->stack[\count($this->stack) - 1]);
46-
}
47-
48-
if (null !== $this->previous && $this->previous->getAttribute(self::ATTRIBUTE_PARENT) === $node->getAttribute(self::ATTRIBUTE_PARENT)) {
36+
if (
37+
null !== $this->previous
38+
&& ! $this->previous instanceof Node\FunctionLike
39+
&& ! $this->previous instanceof Node\Stmt\ClassLike
40+
&& ! $this->previous instanceof VirtualNode
41+
) {
4942
$node->setAttribute(self::ATTRIBUTE_PREVIOUS, $this->previous);
5043
}
5144

52-
$this->stack[] = $node;
53-
5445
return null;
5546
}
5647

5748
public function leaveNode(Node $node)
5849
{
5950
$this->previous = $node;
6051

61-
array_pop($this->stack);
62-
6352
return null;
6453
}
6554
}

0 commit comments

Comments
 (0)