Skip to content

Use more efficient AttributeParentConnectingVisitor #1074

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

Merged
merged 1 commit into from
Apr 28, 2025
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
63 changes: 63 additions & 0 deletions src/StaticAnalysis/AttributeParentConnectingVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);

/*
* This file is part of phpunit/php-code-coverage.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;

use function array_pop;
use function count;
use PhpParser\Node;
use PhpParser\NodeVisitor;

/**
* Visitor that connects a child node to its parent node optimzed for Attribute nodes.
*
* On the child node, the parent node can be accessed through
* <code>$node->getAttribute('parent')</code>.
*/
final class AttributeParentConnectingVisitor implements NodeVisitor
{
/**
* @var Node[]
*/
private array $stack = [];

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

return null;
}

public function enterNode(Node $node): null
{
if (
$this->stack !== [] &&
($node instanceof Node\Attribute || $node instanceof Node\AttributeGroup)
) {
$node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
}

$this->stack[] = $node;

return null;
}

public function leaveNode(Node $node): null
{
array_pop($this->stack);

return null;
}

public function afterTraverse(array $nodes): null
{
return null;
}
}
3 changes: 1 addition & 2 deletions src/StaticAnalysis/ParsingFileAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use PhpParser\Error;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use PhpParser\ParserFactory;
use SebastianBergmann\CodeCoverage\ParserException;
use SebastianBergmann\LinesOfCode\LineCountingVisitor;
Expand Down Expand Up @@ -180,7 +179,7 @@ private function analyse(string $filename): void
$executableLinesFindingVisitor = new ExecutableLinesFindingVisitor($source);

$traverser->addVisitor(new NameResolver);
$traverser->addVisitor(new ParentConnectingVisitor);
$traverser->addVisitor(new AttributeParentConnectingVisitor);
$traverser->addVisitor($codeUnitFindingVisitor);
$traverser->addVisitor($lineCountingVisitor);
$traverser->addVisitor($ignoredLinesFindingVisitor);
Expand Down