Repository navigation
Expand file tree
/
Copy pathPostFileProcessor.php
More file actions
126 lines (104 loc) · 4.16 KB
/
Copy pathPostFileProcessor.php
File metadata and controls
126 lines (104 loc) · 4.16 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace Rector\PostRector\Application;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Configuration\RenamedClassesDataCollector;
use Rector\Contract\DependencyInjection\ResettableInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Rector\PostRector\Rector\ClassRenamingPostRector;
use Rector\PostRector\Rector\DocblockNameImportingPostRector;
use Rector\PostRector\Rector\NameImportingPostRector;
use Rector\PostRector\Rector\UnusedImportRemovingPostRector;
use Rector\PostRector\Rector\UseAddingPostRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Skipper\Skipper\Skipper;
use Rector\ValueObject\Application\File;
final class PostFileProcessor implements ResettableInterface
{
/**
* @var PostRectorInterface[]
*/
private array $postRectors = [];
public function __construct(
private readonly Skipper $skipper,
private readonly UseAddingPostRector $useAddingPostRector,
private readonly NameImportingPostRector $nameImportingPostRector,
private readonly ClassRenamingPostRector $classRenamingPostRector,
private readonly DocblockNameImportingPostRector $docblockNameImportingPostRector,
private readonly UnusedImportRemovingPostRector $unusedImportRemovingPostRector,
private readonly RenamedClassesDataCollector $renamedClassesDataCollector,
private readonly RenameClassRector $renameClassRector
) {
}
public function reset(): void
{
$this->postRectors = [];
}
/**
* @param Stmt[] $stmts
* @return Stmt[]
*/
public function traverse(array $stmts, File $file): array
{
foreach ($this->getPostRectors() as $postRector) {
// file must be set early into PostRector class to ensure its usage
// always match on skipping process
$postRector->setFile($file);
if ($this->shouldSkipPostRector($postRector, $file->getFilePath(), $stmts)) {
continue;
}
$nodeTraverser = new NodeTraverser($postRector);
$stmts = $nodeTraverser->traverse($stmts);
}
return $stmts;
}
/**
* @param Stmt[] $stmts
*/
private function shouldSkipPostRector(PostRectorInterface $postRector, string $filePath, array $stmts): bool
{
// skip renaming if rename class rector is skipped
if ($postRector instanceof ClassRenamingPostRector && $this->skipper->shouldSkipRectorAndFile(
$this->renameClassRector,
$filePath
)) {
return true;
}
return ! $postRector->shouldTraverse($stmts);
}
/**
* Lazy load, to enable test reset with different configuration
* @return PostRectorInterface[]
*/
private function getPostRectors(): array
{
if ($this->postRectors !== []) {
return $this->postRectors;
}
$isRenamedClassEnabled = $this->renamedClassesDataCollector->getOldToNewClasses() !== [];
$isNameImportingEnabled = SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES);
$isRemovingUnusedImportsEnabled = SimpleParameterProvider::provideBoolParameter(Option::REMOVE_UNUSED_IMPORTS);
$postRectors = [];
// sorted by priority, to keep removed imports in order
if ($isRenamedClassEnabled && $isNameImportingEnabled) {
$postRectors[] = $this->classRenamingPostRector;
}
// import names
if ($isNameImportingEnabled) {
$postRectors[] = $this->nameImportingPostRector;
// import docblocks
if (SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_DOC_BLOCK_NAMES)) {
$postRectors[] = $this->docblockNameImportingPostRector;
}
}
$postRectors[] = $this->useAddingPostRector;
if ($isRemovingUnusedImportsEnabled) {
$postRectors[] = $this->unusedImportRemovingPostRector;
}
$this->postRectors = $postRectors;
return $this->postRectors;
}
}