Skip to content
Merged

Tidy up #7829

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
18 changes: 14 additions & 4 deletions src/PhpParser/NodeTraverser/RectorNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
use Webmozart\Assert\Assert;

/**
* Based on native NodeTraverser class, but heavily customized for Rector needs.
*
* The main differences are:
* - no leaveNode(), as we do all in enterNode() that calls refactor() method
* - cached visitors per node class for performance, e.g. when we find rules for Class_ node, they're cached for next time
* - immutability features, register Rector rules once, then use; no changes on the fly
*
* @see \Rector\Tests\PhpParser\NodeTraverser\RectorNodeTraverserTest
* @internal No BC promise on this class, it might change any time.
*/
final class RectorNodeTraverser implements NodeTraverserInterface
{
/**
* @var list<NodeVisitor>
* @var RectorInterface[]
*/
private array $visitors = [];

Expand All @@ -33,17 +41,17 @@ final class RectorNodeTraverser implements NodeTraverserInterface
private bool $areNodeVisitorsPrepared = false;

/**
* @var array<class-string<Node>, NodeVisitor[]>
* @var array<class-string<Node>, RectorInterface[]>
*/
private array $visitorsPerNodeClass = [];

/**
* @param RectorInterface[] $rectors
*/
public function __construct(
private array $rectors,
private readonly PhpVersionedFilter $phpVersionedFilter,
private readonly ConfigurationRuleFilter $configurationRuleFilter,
private array $rectors
) {
}

Expand Down Expand Up @@ -103,7 +111,9 @@ public function refreshPhpRectors(array $rectors): void
}

/**
* @return NodeVisitor[]
* @return RectorInterface[]
*
* @api used in tests
*/
public function getVisitorsForNode(Node $node): array
{
Expand Down