Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce use of PreviousConnectingVisitor:: ATTRIBUTE_PARENT to assist garbage collector #729

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading