1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPMD\Renderer; |
4
|
|
|
|
5
|
|
|
use PHPMD\AbstractRenderer; |
6
|
|
|
use PHPMD\ProcessingError; |
7
|
|
|
use PHPMD\Report; |
8
|
|
|
use PHPMD\RuleViolation; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This renderer output a command line friendly log with all found violations |
12
|
|
|
* and suspect software artifacts. |
13
|
|
|
*/ |
14
|
|
|
class AnsiRenderer extends AbstractRenderer |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param \PHPMD\Report $report |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function renderReport(Report $report) |
22
|
|
|
{ |
23
|
|
|
$this->writeViolationsReport($report); |
24
|
|
|
$this->writeErrorsReport($report); |
25
|
|
|
$this->writeReportSummary($report); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \PHPMD\Report $report |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
private function writeViolationsReport(Report $report) |
33
|
|
|
{ |
34
|
|
|
if ($report->isEmpty()) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$padding = $this->getMaxLineNumberLength($report); |
39
|
|
|
$previousFile = null; |
40
|
|
|
foreach ($report->getRuleViolations() as $violation) { |
41
|
|
|
if ($violation->getFileName() !== $previousFile) { |
42
|
|
|
$this->writeViolationFileHeader($violation); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->writeViolationLine($violation, $padding); |
46
|
|
|
$previousFile = $violation->getFileName(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \PHPMD\Report $report |
52
|
|
|
* @return int|null |
53
|
|
|
*/ |
54
|
|
|
private function getMaxLineNumberLength(Report $report) |
55
|
|
|
{ |
56
|
|
|
$maxLength = null; |
57
|
|
|
foreach ($report->getRuleViolations() as $violation) { |
58
|
|
|
if ($maxLength === null || strlen($violation->getBeginLine()) > $maxLength) { |
59
|
|
|
$maxLength = strlen($violation->getBeginLine()); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
return $maxLength; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param \PHPMD\RuleViolation $violation |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
private function writeViolationFileHeader(RuleViolation $violation) |
70
|
|
|
{ |
71
|
|
|
$fileHeader = sprintf( |
72
|
|
|
'FILE: %s', |
73
|
|
|
$violation->getFileName() |
74
|
|
|
); |
75
|
|
|
$this->getWriter()->write( |
76
|
|
|
PHP_EOL . $fileHeader . PHP_EOL . |
77
|
|
|
str_repeat('-', strlen($fileHeader)) . PHP_EOL |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \PHPMD\RuleViolation $violation |
83
|
|
|
* @param int $padding |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
private function writeViolationLine(RuleViolation $violation, $padding) |
87
|
|
|
{ |
88
|
|
|
$this->getWriter()->write(sprintf( |
89
|
|
|
" %s | \e[31mVIOLATION\e[0m | %s" . PHP_EOL, |
90
|
|
|
str_pad($violation->getBeginLine(), $padding, ' '), |
91
|
|
|
$violation->getDescription() |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \PHPMD\Report $report |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
private function writeErrorsReport(Report $report) |
100
|
|
|
{ |
101
|
|
|
if (!$report->hasErrors()) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** @var ProcessingError $error */ |
106
|
|
|
foreach ($report->getErrors() as $error) { |
107
|
|
|
$errorHeader = sprintf( |
108
|
|
|
"\e[33mERROR\e[0m while parsing %s", |
109
|
|
|
$error->getFile() |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$this->getWriter()->write( |
113
|
|
|
PHP_EOL . $errorHeader . PHP_EOL . |
114
|
|
|
str_repeat('-', strlen($errorHeader) - 9) . PHP_EOL |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$this->getWriter()->write(sprintf( |
118
|
|
|
'%s' . PHP_EOL, |
119
|
|
|
$error->getMessage() |
120
|
|
|
)); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param \PHPMD\Report $report |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
private function writeReportSummary(Report $report) |
129
|
|
|
{ |
130
|
|
|
$this->getWriter()->write( |
131
|
|
|
sprintf( |
132
|
|
|
PHP_EOL . 'Found %s %s and %s %s in %sms' . PHP_EOL, |
133
|
|
|
count($report->getRuleViolations()), |
134
|
|
|
count($report->getRuleViolations()) !== 1 ? 'violations' : 'violation', |
135
|
|
|
iterator_count($report->getErrors()), |
136
|
|
|
iterator_count($report->getErrors()) !== 1 ? 'errors' : 'error', |
137
|
|
|
$report->getElapsedTimeInMillis() |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
if (count($report->getRuleViolations()) === 0 && iterator_count($report->getErrors()) === 0) { |
141
|
|
|
$this->getWriter()->write(PHP_EOL . "\e[32mNo mess detected\e[0m" . PHP_EOL); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|