forked from easy-coding-standard/ecs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleOutputFormatter.php
More file actions
190 lines (154 loc) · 6.45 KB
/
ConsoleOutputFormatter.php
File metadata and controls
190 lines (154 loc) · 6.45 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
<?php
declare(strict_types=1);
namespace Symplify\EasyCodingStandard\Console\Output;
use Symplify\EasyCodingStandard\Console\ExitCode;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;
use Symplify\EasyCodingStandard\Contract\Console\Output\OutputFormatterInterface;
use Symplify\EasyCodingStandard\ValueObject\Configuration;
use Symplify\EasyCodingStandard\ValueObject\Error\ErrorAndDiffResult;
use Symplify\EasyCodingStandard\ValueObject\Error\FileDiff;
use Symplify\EasyCodingStandard\ValueObject\Error\SystemError;
final readonly class ConsoleOutputFormatter implements OutputFormatterInterface
{
/**
* @var string
*/
public const NAME = 'console';
public function __construct(
private EasyCodingStandardStyle $easyCodingStandardStyle,
private ExitCodeResolver $exitCodeResolver
) {
}
/**
* @return ExitCode::*
*/
public function report(ErrorAndDiffResult $errorAndDiffResult, Configuration $configuration): int
{
if ($configuration->shouldShowDiffs()) {
$this->reportFileDiffs($errorAndDiffResult->getFileDiffs(), $configuration->isReportingWithRealPath());
}
$this->easyCodingStandardStyle->newLine(1);
if ($errorAndDiffResult->getErrorCount() === 0 && $errorAndDiffResult->getFileDiffsCount() === 0) {
$this->easyCodingStandardStyle->success('No errors found. Great job - your code is shiny in style!');
return $this->exitCodeResolver->resolve($errorAndDiffResult, $configuration);
}
$this->easyCodingStandardStyle->newLine();
if ($configuration->isFixer()) {
$this->printAfterFixerStatus($errorAndDiffResult, $configuration);
} else {
$this->printNoFixerStatus($errorAndDiffResult, $configuration);
}
return $this->exitCodeResolver->resolve($errorAndDiffResult, $configuration);
}
public static function getName(): string
{
return self::NAME;
}
public static function hasSupportForProgressBars(): bool
{
return true;
}
/**
* @param FileDiff[] $fileDiffs
*/
private function reportFileDiffs(array $fileDiffs, bool $absoluteFilePath = false): void
{
if ($fileDiffs === []) {
return;
}
$this->easyCodingStandardStyle->newLine(1);
$i = 1;
foreach ($fileDiffs as $fileDiff) {
$this->easyCodingStandardStyle->newLine(2);
$filePath = $absoluteFilePath ? $fileDiff->getAbsoluteFilePath() : $fileDiff->getRelativeFilePath();
$boldNumberedMessage = sprintf('<options=bold>%d) %s</>', $i, $filePath);
$this->easyCodingStandardStyle->writeln($boldNumberedMessage);
++$i;
$this->easyCodingStandardStyle->newLine();
$this->easyCodingStandardStyle->writeln($fileDiff->getDiffConsoleFormatted());
$this->easyCodingStandardStyle->newLine();
$this->easyCodingStandardStyle->writeln('<options=underscore>Applied checkers:</>');
$this->easyCodingStandardStyle->newLine();
$this->easyCodingStandardStyle->listing($fileDiff->getAppliedCheckers());
}
}
private function printAfterFixerStatus(ErrorAndDiffResult $errorAndDiffResult, Configuration $configuration): void
{
if ($configuration->shouldShowErrorTable()) {
$this->easyCodingStandardStyle->printErrors($errorAndDiffResult->getErrors());
}
if ($errorAndDiffResult->getErrorCount() === 0) {
$successMessage = sprintf(
'%d error%s successfully fixed and no other errors found!',
$errorAndDiffResult->getFileDiffsCount(),
$errorAndDiffResult->getFileDiffsCount() === 1 ? '' : 's'
);
$this->easyCodingStandardStyle->success($successMessage);
return;
}
$this->printSystemErrors($errorAndDiffResult);
$this->printErrorMessageFromErrorCounts(
$errorAndDiffResult->getCodingStandardErrorCount(),
$errorAndDiffResult->getFileDiffsCount(),
$configuration
);
}
private function printNoFixerStatus(ErrorAndDiffResult $errorAndDiffResult, Configuration $configuration): void
{
if ($configuration->shouldShowErrorTable()) {
$errors = $errorAndDiffResult->getErrors();
if ($errors !== []) {
$this->easyCodingStandardStyle->newLine();
$this->easyCodingStandardStyle->printErrors($errors);
}
}
$this->printSystemErrors($errorAndDiffResult);
$this->printErrorMessageFromErrorCounts(
$errorAndDiffResult->getCodingStandardErrorCount(),
$errorAndDiffResult->getFileDiffsCount(),
$configuration
);
}
private function printSystemErrors(ErrorAndDiffResult $errorAndDiffResult): void
{
$systemErrors = $errorAndDiffResult->getSystemErrors();
foreach ($systemErrors as $systemError) {
$this->easyCodingStandardStyle->newLine();
if ($systemError instanceof SystemError) {
$this->easyCodingStandardStyle->error(
$systemError->getMessage() . ' in ' . $systemError->getFileWithLine()
);
} else {
$this->easyCodingStandardStyle->error($systemError);
}
}
}
private function printErrorMessageFromErrorCounts(
int $codingStandardErrorCount,
int $fileDiffsCount,
Configuration $configuration
): void {
if ($codingStandardErrorCount !== 0) {
$errorMessage = sprintf(
'Found %d error%s that need%s to be fixed manually.',
$codingStandardErrorCount,
$codingStandardErrorCount === 1 ? '' : 's',
$codingStandardErrorCount === 1 ? 's' : ''
);
$this->easyCodingStandardStyle->error($errorMessage);
}
if ($fileDiffsCount === 0) {
return;
}
if ($configuration->isFixer()) {
return;
}
$fixableMessage = sprintf(
'%s%d %s fixable! Just add "--fix" to console command and rerun to apply.',
$codingStandardErrorCount !== 0 ? 'Good news is that ' : '',
$fileDiffsCount,
$fileDiffsCount === 1 ? 'error is' : 'errors are'
);
$this->easyCodingStandardStyle->warning($fixableMessage);
}
}