-
-
Notifications
You must be signed in to change notification settings - Fork 441
Expand file tree
/
Copy pathConsoleOutputFormatter.php
More file actions
186 lines (150 loc) · 6.02 KB
/
Copy pathConsoleOutputFormatter.php
File metadata and controls
186 lines (150 loc) · 6.02 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
<?php
declare(strict_types=1);
namespace Rector\ChangesReporting\Output;
use Nette\Utils\Strings;
use Rector\ChangesReporting\Annotation\RectorsChangelogResolver;
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
use Rector\Core\Contract\Console\OutputStyleInterface;
use Rector\Core\ValueObject\Application\RectorError;
use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\ProcessResult;
use Rector\Core\ValueObject\Reporting\FileDiff;
final class ConsoleOutputFormatter implements OutputFormatterInterface
{
/**
* @var string
*/
final public const NAME = 'console';
/**
* @var string
* @see https://regex101.com/r/q8I66g/1
*/
private const ON_LINE_REGEX = '# on line #';
public function __construct(
private readonly OutputStyleInterface $outputStyle,
private readonly RectorsChangelogResolver $rectorsChangelogResolver
) {
}
public function report(ProcessResult $processResult, Configuration $configuration): void
{
if ($configuration->shouldShowDiffs()) {
$this->reportFileDiffs($processResult->getFileDiffs());
}
$this->reportErrors($processResult->getErrors());
$this->reportRemovedFilesAndNodes($processResult);
if ($processResult->getErrors() !== []) {
return;
}
$message = $this->createSuccessMessage($processResult, $configuration);
$this->outputStyle->success($message);
}
public function getName(): string
{
return self::NAME;
}
/**
* @param FileDiff[] $fileDiffs
*/
private function reportFileDiffs(array $fileDiffs): void
{
if (count($fileDiffs) <= 0) {
return;
}
// normalize
ksort($fileDiffs);
$message = sprintf('%d file%s with changes', count($fileDiffs), count($fileDiffs) === 1 ? '' : 's');
$this->outputStyle->title($message);
$i = 0;
foreach ($fileDiffs as $fileDiff) {
$relativeFilePath = $fileDiff->getRelativeFilePath();
// append line number for faster file jump in diff
$firstLineNumber = $fileDiff->getFirstLineNumber();
if ($firstLineNumber !== null) {
$relativeFilePath .= ':' . $firstLineNumber;
}
$message = sprintf('<options=bold>%d) %s</>', ++$i, $relativeFilePath);
$this->outputStyle->writeln($message);
$this->outputStyle->newline();
$this->outputStyle->writeln($fileDiff->getDiffConsoleFormatted());
$rectorsChangelogsLines = $this->createRectorChangelogLines($fileDiff);
if ($fileDiff->getRectorChanges() !== []) {
$this->outputStyle->writeln('<options=underscore>Applied rules:</>');
$this->outputStyle->listing($rectorsChangelogsLines);
$this->outputStyle->newline();
}
}
}
/**
* @param RectorError[] $errors
*/
private function reportErrors(array $errors): void
{
foreach ($errors as $error) {
$errorMessage = $error->getMessage();
$errorMessage = $this->normalizePathsToRelativeWithLine($errorMessage);
$message = sprintf(
'Could not process "%s" file%s, due to: %s"%s".',
$error->getRelativeFilePath(),
$error->getRectorClass() !== null ? ' by "' . $error->getRectorClass() . '"' : '',
PHP_EOL,
$errorMessage
);
if ($error->getLine() !== null) {
$message .= ' On line: ' . $error->getLine();
}
$this->outputStyle->error($message);
}
}
private function reportRemovedFilesAndNodes(ProcessResult $processResult): void
{
if ($processResult->getAddedFilesCount() !== 0) {
$message = sprintf('%d files were added', $processResult->getAddedFilesCount());
$this->outputStyle->note($message);
}
if ($processResult->getRemovedFilesCount() !== 0) {
$message = sprintf('%d files were removed', $processResult->getRemovedFilesCount());
$this->outputStyle->note($message);
}
$this->reportRemovedNodes($processResult);
}
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 reportRemovedNodes(ProcessResult $processResult): void
{
if ($processResult->getRemovedNodeCount() === 0) {
return;
}
$message = sprintf('%d nodes were removed', $processResult->getRemovedNodeCount());
$this->outputStyle->warning($message);
}
private function createSuccessMessage(ProcessResult $processResult, Configuration $configuration): string
{
$changeCount = count($processResult->getFileDiffs()) + $processResult->getRemovedAndAddedFilesCount();
if ($changeCount === 0) {
return 'Rector is done!';
}
return sprintf(
'%d file%s %s by Rector',
$changeCount,
$changeCount > 1 ? 's' : '',
$configuration->isDryRun() ? 'would have changed (dry-run)' : ($changeCount === 1 ? 'has' : 'have') . ' been changed'
);
}
/**
* @return string[]
*/
private function createRectorChangelogLines(FileDiff $fileDiff): array
{
$rectorsChangelogs = $this->rectorsChangelogResolver->resolveIncludingMissing($fileDiff->getRectorClasses());
$rectorsChangelogsLines = [];
foreach ($rectorsChangelogs as $rectorClass => $changelog) {
$rectorShortClass = (string) Strings::after($rectorClass, '\\', -1);
$rectorsChangelogsLines[] = $changelog === null ? $rectorShortClass : $rectorShortClass . ' (' . $changelog . ')';
}
return $rectorsChangelogsLines;
}
}