Repository navigation
Expand file tree
/
Copy pathMissConfigurationReporter.php
More file actions
164 lines (134 loc) · 6.01 KB
/
Copy pathMissConfigurationReporter.php
File metadata and controls
164 lines (134 loc) · 6.01 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
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace Rector\Reporting;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Configuration\VendorMissAnalyseGuard;
use Rector\Contract\Rector\RectorInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Rector\ValueObject\ProcessResult;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* @see \Rector\Tests\Reporting\MissConfigurationReporterTest
*/
final readonly class MissConfigurationReporter
{
public function __construct(
private SymfonyStyle $symfonyStyle,
private VendorMissAnalyseGuard $vendorMissAnalyseGuard,
private UnusedSkipResolver $unusedSkipResolver,
private NeededSkipVerifier $neededSkipVerifier,
) {
}
/**
* Reports skips configured via "->withSkip()" that never matched any element during the run.
* Enabled with "->reportUnusedSkips()".
*/
public function reportUnusedSkips(ProcessResult $processResult): void
{
$unusedSkips = $this->unusedSkipResolver->resolve($processResult);
if ($unusedSkips === []) {
return;
}
$this->symfonyStyle->warning(sprintf(
'%s never matched any element. You can remove %s from "->withSkip()"',
count($unusedSkips) > 1 ? 'These skips are unused, they' : 'This skip is unused, it',
count($unusedSkips) > 1 ? 'them' : 'it'
));
// add a blank line between items, so grouped rule skips stay visually separated
$spacedUnusedSkips = array_map(static fn (string $unusedSkip): string => $unusedSkip . "\n", $unusedSkips);
$this->symfonyStyle->listing($spacedUnusedSkips);
}
/**
* Reports rule-scoped skips that matched a file but whose rule would not have changed it, so the
* skip is unnecessary. Enabled with "->reportUnusedSkips()". The re-run is forced to a dry run,
* so it is safe on an apply run too.
*/
public function reportUnnecessarySkips(): void
{
$unnecessarySkips = $this->neededSkipVerifier->verify();
if ($unnecessarySkips === []) {
return;
}
$this->symfonyStyle->warning(sprintf(
'%s matched a file, but the rule would not have changed it. You can remove %s from "->withSkip()"',
count($unnecessarySkips) > 1 ? 'These skips are unnecessary, they' : 'This skip is unnecessary, it',
count($unnecessarySkips) > 1 ? 'them' : 'it'
));
// add a blank line between items, so grouped rule skips stay visually separated
$spacedUnnecessarySkips = array_map(
static fn (string $unnecessarySkip): string => $unnecessarySkip . "\n",
$unnecessarySkips
);
$this->symfonyStyle->listing($spacedUnnecessarySkips);
}
public function reportSkippedNeverRegisteredRules(): int
{
$registeredRules = SimpleParameterProvider::provideArrayParameter(Option::REGISTERED_RECTOR_RULES);
$skippedRules = SimpleParameterProvider::provideArrayParameter(Option::SKIPPED_RECTOR_RULES);
$neverRegisteredSkippedRules = array_unique(array_diff($skippedRules, $registeredRules));
// remove special PostRectorInterface rules, they are registered in a different way
$neverRegisteredSkippedRules = array_filter(
$neverRegisteredSkippedRules,
fn ($skippedRule): bool => ! is_a($skippedRule, PostRectorInterface::class, true)
);
if ($neverRegisteredSkippedRules === []) {
return 0;
}
$this->symfonyStyle->warning(sprintf(
'%s never registered. You can remove %s from "->withSkip()"',
count($neverRegisteredSkippedRules) > 1 ? 'These skipped rules are' : 'This skipped rule is',
count($neverRegisteredSkippedRules) > 1 ? 'them' : 'it'
));
$this->symfonyStyle->listing($neverRegisteredSkippedRules);
return count($neverRegisteredSkippedRules);
}
public function reportSkippedNonRectorClasses(): int
{
$skippedNonRectorClasses = SimpleParameterProvider::provideArrayParameter(
Option::SKIPPED_NON_RECTOR_CLASSES
);
if ($skippedNonRectorClasses === []) {
return 0;
}
$this->symfonyStyle->warning(sprintf(
'%s "%s" %s not a Rector rule, so %s never be skipped. Only classes that implement "%s" can be used in "->withSkip()"',
count($skippedNonRectorClasses) > 1 ? 'These skipped classes' : 'This skipped class',
implode('", "', $skippedNonRectorClasses),
count($skippedNonRectorClasses) > 1 ? 'are' : 'is',
count($skippedNonRectorClasses) > 1 ? 'they can' : 'it can',
RectorInterface::class
));
return count($skippedNonRectorClasses);
}
/**
* @param string[] $filePaths
*/
public function reportVendorInPaths(array $filePaths): void
{
if (! $this->vendorMissAnalyseGuard->isVendorAnalyzed($filePaths)) {
return;
}
$this->symfonyStyle->warning(sprintf(
'Rector has detected a "/vendor" directory in your configured paths. If this is Composer\'s vendor directory, this is not necessary as it will be autoloaded. Scanning the Composer /vendor directory will cause Rector to run much slower and possibly with errors.%sRemove "/vendor" from Rector paths and run again.',
"\n\n"
));
sleep(3);
}
public function reportStartWithShortOpenTag(): void
{
$files = SimpleParameterProvider::provideArrayParameter(Option::SKIPPED_START_WITH_SHORT_OPEN_TAG_FILES);
if ($files === []) {
return;
}
$suffix = count($files) > 1 ? 's were' : ' was';
$fileList = implode("\n", $files);
$this->symfonyStyle->warning(sprintf(
'The following file%s skipped as starting with short open tag. Migrate to long open PHP tag first: %s%s',
$suffix,
"\n\n",
$fileList
));
sleep(3);
}
}