Repository navigation
Expand file tree
/
Copy pathUnusedSkipResolver.php
More file actions
105 lines (87 loc) · 3.92 KB
/
Copy pathUnusedSkipResolver.php
File metadata and controls
105 lines (87 loc) · 3.92 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
<?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\SkippedClassResolver;
use Rector\Skipper\SkipCriteriaResolver\SkippedPathsResolver;
use Rector\ValueObject\ProcessResult;
/**
* @see \Rector\Tests\Reporting\UnusedSkipResolverTest
*/
final readonly class UnusedSkipResolver
{
public function __construct(
private SkippedClassResolver $skippedClassResolver,
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 => path", or "rule => [ path\n path ]"
* for multiple paths) 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 (! SimpleParameterProvider::provideBoolParameter(Option::REPORT_UNUSED_SKIPS, false)) {
return [];
}
// 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 [];
}
// 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.
$relativePathsByClass = [];
foreach ($this->skippedClassResolver->resolve() 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);
}
}
// global mask paths like "*/some/*" are hard to spot and report false positives, skip them
$globalRelativePaths = [];
foreach ($this->skippedPathsResolver->resolve() as $globalPath) {
if (str_contains($globalPath, '*')) {
continue;
}
$globalRelativePaths[$globalPath] = $this->filePathHelper->relativePath($globalPath);
}
$usedSkips = $processResult->getUsedSkips();
$unusedSkips = [];
// group unused rule-scoped paths under their rule, matching the "->withSkip()" config shape
foreach ($relativePathsByClass as $rectorClass => $relativePaths) {
$unusedRelativePaths = [];
foreach ($relativePaths as $path => $relativePath) {
if (! in_array($path, $usedSkips, true)) {
$unusedRelativePaths[] = $relativePath;
}
}
if ($unusedRelativePaths === []) {
continue;
}
if (count($unusedRelativePaths) === 1) {
$unusedSkips[] = $rectorClass . ' => ' . $unusedRelativePaths[0];
continue;
}
$unusedSkips[] = $rectorClass . ' => [ ' . implode("\n ", $unusedRelativePaths) . ' ]';
}
foreach ($globalRelativePaths as $path => $relativePath) {
if (! in_array($path, $usedSkips, true)) {
$unusedSkips[] = $relativePath;
}
}
return $unusedSkips;
}
}