-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathConsoleOutputFormatter.php
More file actions
204 lines (166 loc) · 6.81 KB
/
ConsoleOutputFormatter.php
File metadata and controls
204 lines (166 loc) · 6.81 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
declare(strict_types=1);
namespace Rector\ChangesReporting\Output;
use Nette\Utils\Strings;
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\ValueObject\Configuration;
use Rector\ValueObject\Error\SystemError;
use Rector\ValueObject\ProcessResult;
use Rector\ValueObject\Reporting\FileDiff;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Style\SymfonyStyle;
final readonly class ConsoleOutputFormatter implements OutputFormatterInterface
{
public const string NAME = 'console';
/**
* @see https://regex101.com/r/q8I66g/1
*/
private const string ON_LINE_REGEX = '# on line #';
public function __construct(
private SymfonyStyle $symfonyStyle,
) {
}
public function report(ProcessResult $processResult, Configuration $configuration): void
{
if ($configuration->shouldShowDiffs()) {
$this->reportFileDiffs($processResult->getFileDiffs(), $configuration->isReportingWithRealPath());
}
$this->reportErrors($processResult->getSystemErrors(), $configuration->isReportingWithRealPath());
if ($processResult->getSystemErrors() !== []) {
return;
}
// to keep space between progress bar and success message
if ($configuration->shouldShowProgressBar() && $processResult->getFileDiffs() === []) {
$this->symfonyStyle->newLine();
}
if ($configuration->shouldShowRulesSummary()) {
$this->reportRulesSummary($processResult, $configuration);
}
$message = $this->createSuccessMessage($processResult, $configuration);
$this->symfonyStyle->success($message);
}
public function getName(): string
{
return self::NAME;
}
/**
* @param FileDiff[] $fileDiffs
*/
private function reportFileDiffs(array $fileDiffs, bool $absoluteFilePath): void
{
if (count($fileDiffs) <= 0) {
return;
}
// normalize
ksort($fileDiffs);
$message = sprintf('%d file%s with changes', count($fileDiffs), count($fileDiffs) === 1 ? '' : 's');
$this->symfonyStyle->title($message);
$i = 0;
foreach ($fileDiffs as $fileDiff) {
$filePath = $absoluteFilePath
? ($fileDiff->getAbsoluteFilePath() ?? '')
: $fileDiff->getRelativeFilePath();
// append line number for faster file jump in diff
$firstLineNumber = $fileDiff->getFirstLineNumber();
if ($firstLineNumber !== null) {
$filePath .= ':' . $firstLineNumber;
}
$filePathWithUrl = $this->addEditorUrl(
$filePath,
$fileDiff->getAbsoluteFilePath(),
$fileDiff->getRelativeFilePath(),
(string) $fileDiff->getFirstLineNumber()
);
$message = sprintf('<options=bold>%d) %s</>', ++$i, $filePathWithUrl);
$this->symfonyStyle->writeln($message);
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln($fileDiff->getDiffConsoleFormatted());
if ($fileDiff->getRectorChanges() !== []) {
$this->symfonyStyle->writeln('<options=underscore>Applied rules:</>');
$this->symfonyStyle->listing($fileDiff->getRectorShortClasses());
$this->symfonyStyle->newLine();
}
}
}
/**
* @param SystemError[] $errors
*/
private function reportErrors(array $errors, bool $absoluteFilePath): void
{
foreach ($errors as $error) {
$errorMessage = $error->getMessage();
$errorMessage = $this->normalizePathsToRelativeWithLine($errorMessage);
$errorMessage = str_replace("\r\n", "\n", $errorMessage);
$filePath = $absoluteFilePath ? $error->getAbsoluteFilePath() : $error->getRelativeFilePath();
$message = sprintf(
'Could not process %s%s, due to: %s"%s".',
$filePath !== null ? '"' . $filePath . '" file' : 'some files',
$error->getRectorClass() !== null ? ' by "' . $error->getRectorClass() . '"' : '',
"\n",
$errorMessage
);
if ($error->getLine() !== null) {
$message .= ' On line: ' . $error->getLine();
}
$this->symfonyStyle->error($message);
}
}
private function reportRulesSummary(ProcessResult $processResult, Configuration $configuration): void
{
$ruleApplicationCounts = $processResult->getRuleApplicationCounts();
if ($ruleApplicationCounts === []) {
return;
}
$verb = $configuration->isDryRun() ? 'would have been applied' : 'was applied';
$this->symfonyStyle->section('Rules Summary');
foreach ($ruleApplicationCounts as $ruleClass => $count) {
$ruleShortClass = (string) Strings::after($ruleClass, '\\', -1);
$this->symfonyStyle->writeln(sprintf(
' * <info>%s</info> %s <comment>%d</comment> time%s',
$ruleShortClass,
$verb,
$count,
$count > 1 ? 's' : ''
));
}
$this->symfonyStyle->newLine();
}
private function normalizePathsToRelativeWithLine(string $errorMessage): string
{
$regex = '#' . preg_quote(getcwd(), '#') . '/#';
$errorMessage = Strings::replace($errorMessage, $regex);
return Strings::replace($errorMessage, self::ON_LINE_REGEX);
}
private function createSuccessMessage(ProcessResult $processResult, Configuration $configuration): string
{
$changeCount = $processResult->getTotalChanged();
if ($changeCount === 0) {
return 'Rector is done!';
}
return sprintf(
'%d file%s %s by Rector',
$changeCount,
$changeCount > 1 ? 's' : '',
$configuration->isDryRun() ? 'would have been changed (dry-run)' : ($changeCount === 1 ? 'has' : 'have') . ' been changed'
);
}
private function addEditorUrl(
string $filePath,
?string $absoluteFilePath,
?string $relativeFilePath,
?string $lineNumber,
): string {
$editorUrl = SimpleParameterProvider::provideStringParameter(Option::EDITOR_URL, '');
if ($editorUrl !== '') {
$editorUrl = str_replace(
['%file%', '%relFile%', '%line%'],
[(string) $absoluteFilePath, (string) $relativeFilePath, (string) $lineNumber],
$editorUrl,
);
$filePath = '<href=' . OutputFormatter::escape($editorUrl) . '>' . $filePath . '</>';
}
return $filePath;
}
}