-
Notifications
You must be signed in to change notification settings - Fork 14
/
STPluginReport.php
201 lines (166 loc) · 6.82 KB
/
STPluginReport.php
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
<?php
/**
* Sublime Text Plugin report for PHP_CodeSniffer.
*
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer_SublimePlugin;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Reports\Report;
class STPluginReport implements Report
{
/**
* Generate a partial report for a single processed file.
*
* Function should return TRUE if it printed or stored data about the file
* and FALSE if it ignored the file. Returning TRUE indicates that the file and
* its data should be counted in the grand totals.
*
* @param array $report Prepared report data.
* @param \PHP_CodeSniffer\File $phpcsFile The file being reported on.
* @param bool $showSources Show sources?
* @param int $width Maximum allowed line width.
*
* @return bool
*/
public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
{
if ($report['errors'] === 0 && $report['warnings'] === 0) {
// Nothing to print.
return false;
}
echo PHP_EOL;
echo ' PHP_CodeSniffer found '.$report['errors'].' error';
if ($report['errors'] !== 1) {
echo 's';
}
if ($report['warnings'] > 0) {
echo ' and '.$report['warnings'].' warning';
if ($report['warnings'] !== 1) {
echo 's';
}
}
echo ' affecting '.count($report['messages']).' line';
if (count($report['messages']) !== 1) {
echo 's';
}
echo PHP_EOL;
// Print the fixable message.
if ($report['fixable'] > 0) {
echo PHP_EOL;
$message = 'Good news! ';
if ($report['fixable'] > 1) {
$message .= 'The '.$report['fixable'].' marked sniff violations';
} else {
$message .= 'The marked sniff violation';
}
$message .= ' below can be fixed automatically';
$length = (strlen($message) + 4);
$fixButton = '[ Click here to fix this file ]';
$leftPadding = floor(($length - 4 - strlen($fixButton)) / 2);
$rightPadding = ($length - $leftPadding - strlen($fixButton) - 4);
$fixMessage = str_repeat(' ', $leftPadding).$fixButton.str_repeat(' ', $rightPadding);
echo ' '.str_repeat('-', $length).PHP_EOL;
echo ' | '.$message.' |'.PHP_EOL;
echo ' | '.$fixMessage.' |'.PHP_EOL;
echo ' '.str_repeat('-', $length).PHP_EOL;
}//end if
echo PHP_EOL.' Note: click a sniff violation below to jump directly to the relevant line'.PHP_EOL;
// Work out the max line number for formatting.
$maxLine = 0;
foreach ($report['messages'] as $line => $lineErrors) {
if ($line > $maxLine) {
$maxLine = $line;
}
}
$maxLineLength = strlen($maxLine);
if ($report['errors'] > 0) {
echo PHP_EOL.' Errors:'.PHP_EOL;
foreach ($report['messages'] as $line => $lineErrors) {
foreach ($lineErrors as $column => $colErrors) {
foreach ($colErrors as $error) {
if ($error['type'] !== 'ERROR') {
continue;
}
$message = $error['message'];
if ($showSources === true) {
$message .= ' ('.$error['source'].')';
}
echo ' ';
if ($report['fixable'] > 0) {
echo '[';
if ($error['fixable'] === true) {
echo 'x';
} else {
echo ' ';
}
echo '] ';
}
$padding = ($maxLineLength - strlen($line));
echo 'Line '.$line.str_repeat(' ', $padding).': '.$message.PHP_EOL;
}//end foreach
}//end foreach
}//end foreach
}//end if
if ($report['warnings'] > 0) {
echo PHP_EOL.' Warnings:'.PHP_EOL;
foreach ($report['messages'] as $line => $lineErrors) {
foreach ($lineErrors as $column => $colErrors) {
foreach ($colErrors as $error) {
if ($error['type'] !== 'WARNING') {
continue;
}
$message = $error['message'];
if ($showSources === true) {
$message .= ' ('.$error['source'].')';
}
echo ' ';
if ($report['fixable'] > 0) {
echo '[';
if ($error['fixable'] === true) {
echo 'x';
} else {
echo ' ';
}
echo '] ';
}
$padding = ($maxLineLength - strlen($line));
echo 'Line '.$line.str_repeat(' ', $padding).': '.$message.PHP_EOL;
}//end foreach
}//end foreach
}//end foreach
}//end if
return true;
}//end generateFileReport()
/**
* Prints all errors and warnings for each file processed.
*
* @param string $cachedData Any partial report data that was returned from
* generateFileReport during the run.
* @param int $totalFiles Total number of files processed during the run.
* @param int $totalErrors Total number of errors found during the run.
* @param int $totalWarnings Total number of warnings found during the run.
* @param int $totalFixable Total number of problems that can be fixed.
* @param bool $showSources Show sources?
* @param int $width Maximum allowed line width.
* @param bool $interactive Are we running in interactive mode?
* @param bool $toScreen Is the report being printed to screen?
*
* @return void
*/
public function generate(
$cachedData,
$totalFiles,
$totalErrors,
$totalWarnings,
$totalFixable,
$showSources=false,
$width=80,
$interactive=false,
$toScreen=true
) {
echo $cachedData;
}//end generate()
}//end class