rector/src/PhpParser/NodeTraverser/RectorNodeTraverser.php
Tomas Votruba 0d611d897a Updated Rector to commit 07df7dc1a344c52711441149d61251a4fe10366d
07df7dc1a3 Remove collector interface and fix SpatieEnumClassToEnumRector already has underscore to double underscore (#5473)
2024-01-16 10:21:57 +00:00

69 lines
1.8 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\PhpParser\NodeTraverser;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use Rector\Contract\Rector\RectorInterface;
use Rector\VersionBonding\PhpVersionedFilter;
final class RectorNodeTraverser extends NodeTraverser
{
/**
* @var RectorInterface[]
*/
private $rectors;
/**
* @readonly
* @var \Rector\VersionBonding\PhpVersionedFilter
*/
private $phpVersionedFilter;
/**
* @var bool
*/
private $areNodeVisitorsPrepared = \false;
/**
* @param RectorInterface[] $rectors
*/
public function __construct(array $rectors, PhpVersionedFilter $phpVersionedFilter)
{
$this->rectors = $rectors;
$this->phpVersionedFilter = $phpVersionedFilter;
parent::__construct();
}
/**
* @param Node[] $nodes
* @return Node[]
*/
public function traverse(array $nodes) : array
{
$this->prepareNodeVisitors();
return parent::traverse($nodes);
}
/**
* @param RectorInterface[] $rectors
* @api used in tests to update the active rules
*/
public function refreshPhpRectors(array $rectors) : void
{
$this->rectors = $rectors;
$this->visitors = [];
$this->areNodeVisitorsPrepared = \false;
}
/**
* This must happen after $this->configuration is set after ProcessCommand::execute() is run,
* otherwise we get default false positives.
*
* This hack should be removed after https://github.com/rectorphp/rector/issues/5584 is resolved
*/
private function prepareNodeVisitors() : void
{
if ($this->areNodeVisitorsPrepared) {
return;
}
// filer out by version
$this->visitors = $this->phpVersionedFilter->filter($this->rectors);
$this->areNodeVisitorsPrepared = \true;
}
}