Skip to content
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 <sebastian@phpunit.de>
*
* 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