-
-
Notifications
You must be signed in to change notification settings - Fork 333
/
RectorNodeTraverser.php
65 lines (54 loc) · 1.59 KB
/
RectorNodeTraverser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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
{
private bool $areNodeVisitorsPrepared = false;
/**
* @param RectorInterface[] $rectors
*/
public function __construct(
private array $rectors,
private readonly 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;
}
}