Repository navigation
Expand file tree
/
Copy pathUnusedSkipResolver.php
More file actions
154 lines (131 loc) · 5.4 KB
/
Copy pathUnusedSkipResolver.php
File metadata and controls
154 lines (131 loc) · 5.4 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
namespace Rector\Reporting;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\FileSystem\FilePathHelper;
use Rector\Skipper\SkipCriteriaResolver\SkippedPathsResolver;
use Rector\Skipper\Skipper\Skipper;
use Rector\ValueObject\ProcessResult;
/**
* @see \Rector\Tests\Reporting\UnusedSkipResolverTest
*/
final readonly class UnusedSkipResolver
{
public function __construct(
private Skipper $skipper,
private SkippedPathsResolver $skippedPathsResolver,
private FilePathHelper $filePathHelper,
) {
}
/**
* Resolves skips configured via "->withSkip()" that never matched any element during the run.
* Rule-scoped skips are grouped under their rule ("rule:" on its own line, each path nested
* below it) so the user knows exactly what to remove; global skips are returned as a plain
* path. Returns an empty array unless "->reportUnusedSkips()" is enabled.
*
* @return string[]
*/
public function resolve(ProcessResult $processResult): array
{
if ($this->shouldSkipReporting()) {
return [];
}
$usedSkips = $processResult->getUsedSkips();
return [
...$this->resolveUnusedRuleScopedSkips($usedSkips),
...$this->resolveUnusedGlobalSkips($usedSkips),
];
}
private function shouldSkipReporting(): bool
{
if (! SimpleParameterProvider::provideBoolParameter(Option::REPORT_UNUSED_SKIPS, false)) {
return true;
}
// a narrowed run (cli paths, "--only" or "--only-suffix") only touches part of the codebase,
// so skips outside that scope look falsely unused - reporting them would be noise
if (SimpleParameterProvider::provideBoolParameter(Option::IS_RUN_NARROWED, false)) {
return true;
}
// a cached run only re-processes changed files, so skips on cached files never get a chance
// to match and would all look falsely unused - reporting them would be noise
return SimpleParameterProvider::provideBoolParameter(Option::IS_CACHED_RUN, false);
}
/**
* Map of rule => (trackable skip path => relative display path); skips are tracked at
* runtime by their path, but rule-scoped ones are printed grouped under their rule so the
* user knows exactly what to remove. Skip-everywhere rule skips (null path) are forgotten
* from the container at boot, so they never reach the skipper and cannot be tracked.
*
* @return array<string, array<string, string>>
*/
private function resolveRelativePathsByClass(): array
{
$relativePathsByClass = [];
foreach ($this->skipper->resolveSkippedClasses() as $rectorClass => $paths) {
if ($paths === null) {
continue;
}
// rule-scoped paths are intentional, so they are reported even as mask paths
foreach ($paths as $path) {
$relativePathsByClass[$rectorClass][$path] = $this->filePathHelper->relativePath($path);
}
}
return $relativePathsByClass;
}
/**
* @return array<string, string>
*/
private function resolveGlobalRelativePaths(): array
{
$globalRelativePaths = [];
foreach ($this->skippedPathsResolver->resolve() as $globalPath) {
// global mask paths like "*/some/*" are hard to spot and report false positives, skip them
if (str_contains($globalPath, '*')) {
continue;
}
$globalRelativePaths[$globalPath] = $this->filePathHelper->relativePath($globalPath);
}
return $globalRelativePaths;
}
/**
* @param array<string, string[]> $usedSkips
* @return string[]
*/
private function resolveUnusedRuleScopedSkips(array $usedSkips): array
{
$unusedSkips = [];
// group unused rule-scoped paths under their rule, matching the "->withSkip()" config shape
foreach ($this->resolveRelativePathsByClass() as $rectorClass => $relativePaths) {
$unusedRelativePaths = [];
foreach ($relativePaths as $path => $relativePath) {
// used skips are tracked scoped to their rule (class => [path]), so the same path
// skipped under another rule does not mark this one used (see Skipper::matchSkip())
if (! in_array($path, $usedSkips[$rectorClass] ?? [], true)) {
$unusedRelativePaths[] = $relativePath;
}
}
if ($unusedRelativePaths === []) {
continue;
}
// rule on its own line, with each unused path nested below it as a "->listing()" sub-item
$unusedSkips[] = $rectorClass . ':' . "\n * " . implode("\n * ", $unusedRelativePaths);
}
return $unusedSkips;
}
/**
* @param array<string, string[]> $usedSkips
* @return string[]
*/
private function resolveUnusedGlobalSkips(array $usedSkips): array
{
$unusedSkips = [];
foreach ($this->resolveGlobalRelativePaths() as $path => $relativePath) {
// global path skips are tracked by their own path key, with no nested paths
if (! array_key_exists($path, $usedSkips)) {
$unusedSkips[] = $relativePath;
}
}
return $unusedSkips;
}
}