@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | if ($report['errors'] === 0 && $report['warnings'] === 0) { |
36 | 36 | // Nothing to print. |
@@ -86,10 +86,10 @@ discard block |
||
86 | 86 | $totalErrors, |
87 | 87 | $totalWarnings, |
88 | 88 | $totalFixable, |
89 | - $showSources=false, |
|
90 | - $width=80, |
|
91 | - $interactive=false, |
|
92 | - $toScreen=true |
|
89 | + $showSources = false, |
|
90 | + $width = 80, |
|
91 | + $interactive = false, |
|
92 | + $toScreen = true |
|
93 | 93 | ) { |
94 | 94 | $lines = explode(PHP_EOL, $cachedData); |
95 | 95 | array_pop($lines); |
@@ -16,321 +16,321 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
36 | - // Nothing to print. |
|
37 | - return false; |
|
38 | - } |
|
39 | - |
|
40 | - $sources = []; |
|
41 | - |
|
42 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
43 | - foreach ($lineErrors as $column => $colErrors) { |
|
44 | - foreach ($colErrors as $error) { |
|
45 | - $src = $error['source']; |
|
46 | - if (isset($sources[$src]) === false) { |
|
47 | - $sources[$src] = [ |
|
48 | - 'fixable' => (int) $error['fixable'], |
|
49 | - 'count' => 1, |
|
50 | - ]; |
|
51 | - } else { |
|
52 | - $sources[$src]['count']++; |
|
53 | - } |
|
54 | - } |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - foreach ($sources as $source => $data) { |
|
59 | - echo $source.'>>'.$data['fixable'].'>>'.$data['count'].PHP_EOL; |
|
60 | - } |
|
61 | - |
|
62 | - return true; |
|
63 | - |
|
64 | - }//end generateFileReport() |
|
65 | - |
|
66 | - |
|
67 | - /** |
|
68 | - * Prints the source of all errors and warnings. |
|
69 | - * |
|
70 | - * @param string $cachedData Any partial report data that was returned from |
|
71 | - * generateFileReport during the run. |
|
72 | - * @param int $totalFiles Total number of files processed during the run. |
|
73 | - * @param int $totalErrors Total number of errors found during the run. |
|
74 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
75 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
76 | - * @param bool $showSources Show sources? |
|
77 | - * @param int $width Maximum allowed line width. |
|
78 | - * @param bool $interactive Are we running in interactive mode? |
|
79 | - * @param bool $toScreen Is the report being printed to screen? |
|
80 | - * |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - public function generate( |
|
84 | - $cachedData, |
|
85 | - $totalFiles, |
|
86 | - $totalErrors, |
|
87 | - $totalWarnings, |
|
88 | - $totalFixable, |
|
89 | - $showSources=false, |
|
90 | - $width=80, |
|
91 | - $interactive=false, |
|
92 | - $toScreen=true |
|
93 | - ) { |
|
94 | - $lines = explode(PHP_EOL, $cachedData); |
|
95 | - array_pop($lines); |
|
96 | - |
|
97 | - if (empty($lines) === true) { |
|
98 | - return; |
|
99 | - } |
|
100 | - |
|
101 | - $sources = []; |
|
102 | - $maxLength = 0; |
|
103 | - |
|
104 | - foreach ($lines as $line) { |
|
105 | - $parts = explode('>>', $line); |
|
106 | - $source = $parts[0]; |
|
107 | - $fixable = (bool) $parts[1]; |
|
108 | - $count = $parts[2]; |
|
109 | - |
|
110 | - if (isset($sources[$source]) === false) { |
|
111 | - if ($showSources === true) { |
|
112 | - $parts = null; |
|
113 | - $sniff = $source; |
|
114 | - } else { |
|
115 | - $parts = explode('.', $source); |
|
116 | - if ($parts[0] === 'Internal') { |
|
117 | - $parts[2] = $parts[1]; |
|
118 | - $parts[1] = ''; |
|
119 | - } |
|
120 | - |
|
121 | - $parts[1] = $this->makeFriendlyName($parts[1]); |
|
122 | - |
|
123 | - $sniff = $this->makeFriendlyName($parts[2]); |
|
124 | - if (isset($parts[3]) === true) { |
|
125 | - $name = $this->makeFriendlyName($parts[3]); |
|
126 | - $name[0] = strtolower($name[0]); |
|
127 | - $sniff .= ' '.$name; |
|
128 | - unset($parts[3]); |
|
129 | - } |
|
130 | - |
|
131 | - $parts[2] = $sniff; |
|
132 | - }//end if |
|
133 | - |
|
134 | - $maxLength = max($maxLength, strlen($sniff)); |
|
135 | - |
|
136 | - $sources[$source] = [ |
|
137 | - 'count' => $count, |
|
138 | - 'fixable' => $fixable, |
|
139 | - 'parts' => $parts, |
|
140 | - ]; |
|
141 | - } else { |
|
142 | - $sources[$source]['count'] += $count; |
|
143 | - }//end if |
|
144 | - }//end foreach |
|
145 | - |
|
146 | - if ($showSources === true) { |
|
147 | - $width = min($width, ($maxLength + 11)); |
|
148 | - } else { |
|
149 | - $width = min($width, ($maxLength + 41)); |
|
150 | - } |
|
151 | - |
|
152 | - $width = max($width, 70); |
|
153 | - |
|
154 | - // Sort the data based on counts and source code. |
|
155 | - $sourceCodes = array_keys($sources); |
|
156 | - $counts = []; |
|
157 | - foreach ($sources as $source => $data) { |
|
158 | - $counts[$source] = $data['count']; |
|
159 | - } |
|
160 | - |
|
161 | - array_multisort($counts, SORT_DESC, $sourceCodes, SORT_ASC, SORT_NATURAL, $sources); |
|
162 | - |
|
163 | - echo PHP_EOL."\033[1mPHP CODE SNIFFER VIOLATION SOURCE SUMMARY\033[0m".PHP_EOL; |
|
164 | - echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
|
165 | - if ($showSources === true) { |
|
166 | - if ($totalFixable > 0) { |
|
167 | - echo ' SOURCE'.str_repeat(' ', ($width - 15)).'COUNT'.PHP_EOL; |
|
168 | - } else { |
|
169 | - echo 'SOURCE'.str_repeat(' ', ($width - 11)).'COUNT'.PHP_EOL; |
|
170 | - } |
|
171 | - } else { |
|
172 | - if ($totalFixable > 0) { |
|
173 | - echo ' STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 44)).'COUNT'.PHP_EOL; |
|
174 | - } else { |
|
175 | - echo 'STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 40)).'COUNT'.PHP_EOL; |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - echo "\033[0m".str_repeat('-', $width).PHP_EOL; |
|
180 | - |
|
181 | - $fixableSources = 0; |
|
182 | - |
|
183 | - if ($showSources === true) { |
|
184 | - $maxSniffWidth = ($width - 7); |
|
185 | - } else { |
|
186 | - $maxSniffWidth = ($width - 37); |
|
187 | - } |
|
188 | - |
|
189 | - if ($totalFixable > 0) { |
|
190 | - $maxSniffWidth -= 4; |
|
191 | - } |
|
192 | - |
|
193 | - foreach ($sources as $source => $sourceData) { |
|
194 | - if ($totalFixable > 0) { |
|
195 | - echo '['; |
|
196 | - if ($sourceData['fixable'] === true) { |
|
197 | - echo 'x'; |
|
198 | - $fixableSources++; |
|
199 | - } else { |
|
200 | - echo ' '; |
|
201 | - } |
|
202 | - |
|
203 | - echo '] '; |
|
204 | - } |
|
205 | - |
|
206 | - if ($showSources === true) { |
|
207 | - if (strlen($source) > $maxSniffWidth) { |
|
208 | - $source = substr($source, 0, $maxSniffWidth); |
|
209 | - } |
|
210 | - |
|
211 | - echo $source; |
|
212 | - if ($totalFixable > 0) { |
|
213 | - echo str_repeat(' ', ($width - 9 - strlen($source))); |
|
214 | - } else { |
|
215 | - echo str_repeat(' ', ($width - 5 - strlen($source))); |
|
216 | - } |
|
217 | - } else { |
|
218 | - $parts = $sourceData['parts']; |
|
219 | - |
|
220 | - if (strlen($parts[0]) > 8) { |
|
221 | - $parts[0] = substr($parts[0], 0, ((strlen($parts[0]) - 8) * -1)); |
|
222 | - } |
|
223 | - |
|
224 | - echo $parts[0].str_repeat(' ', (10 - strlen($parts[0]))); |
|
225 | - |
|
226 | - $category = $parts[1]; |
|
227 | - if (strlen($category) > 18) { |
|
228 | - $category = substr($category, 0, ((strlen($category) - 18) * -1)); |
|
229 | - } |
|
230 | - |
|
231 | - echo $category.str_repeat(' ', (20 - strlen($category))); |
|
232 | - |
|
233 | - $sniff = $parts[2]; |
|
234 | - if (strlen($sniff) > $maxSniffWidth) { |
|
235 | - $sniff = substr($sniff, 0, $maxSniffWidth); |
|
236 | - } |
|
237 | - |
|
238 | - if ($totalFixable > 0) { |
|
239 | - echo $sniff.str_repeat(' ', ($width - 39 - strlen($sniff))); |
|
240 | - } else { |
|
241 | - echo $sniff.str_repeat(' ', ($width - 35 - strlen($sniff))); |
|
242 | - } |
|
243 | - }//end if |
|
244 | - |
|
245 | - echo $sourceData['count'].PHP_EOL; |
|
246 | - }//end foreach |
|
247 | - |
|
248 | - echo str_repeat('-', $width).PHP_EOL; |
|
249 | - echo "\033[1m".'A TOTAL OF '.($totalErrors + $totalWarnings).' SNIFF VIOLATION'; |
|
250 | - if (($totalErrors + $totalWarnings) > 1) { |
|
251 | - echo 'S'; |
|
252 | - } |
|
253 | - |
|
254 | - echo ' WERE FOUND IN '.count($sources).' SOURCE'; |
|
255 | - if (count($sources) !== 1) { |
|
256 | - echo 'S'; |
|
257 | - } |
|
258 | - |
|
259 | - echo "\033[0m"; |
|
260 | - |
|
261 | - if ($totalFixable > 0) { |
|
262 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
263 | - echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
|
264 | - } |
|
265 | - |
|
266 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
267 | - |
|
268 | - if ($toScreen === true && $interactive === false) { |
|
269 | - Timing::printRunTime(); |
|
270 | - } |
|
271 | - |
|
272 | - }//end generate() |
|
273 | - |
|
274 | - |
|
275 | - /** |
|
276 | - * Converts a camel caps name into a readable string. |
|
277 | - * |
|
278 | - * @param string $name The camel caps name to convert. |
|
279 | - * |
|
280 | - * @return string |
|
281 | - */ |
|
282 | - public function makeFriendlyName($name) |
|
283 | - { |
|
284 | - if (trim($name) === '') { |
|
285 | - return ''; |
|
286 | - } |
|
287 | - |
|
288 | - $friendlyName = ''; |
|
289 | - $length = strlen($name); |
|
290 | - |
|
291 | - $lastWasUpper = false; |
|
292 | - $lastWasNumeric = false; |
|
293 | - for ($i = 0; $i < $length; $i++) { |
|
294 | - if (is_numeric($name[$i]) === true) { |
|
295 | - if ($lastWasNumeric === false) { |
|
296 | - $friendlyName .= ' '; |
|
297 | - } |
|
298 | - |
|
299 | - $lastWasUpper = false; |
|
300 | - $lastWasNumeric = true; |
|
301 | - } else { |
|
302 | - $lastWasNumeric = false; |
|
303 | - |
|
304 | - $char = strtolower($name[$i]); |
|
305 | - if ($char === $name[$i]) { |
|
306 | - // Lowercase. |
|
307 | - $lastWasUpper = false; |
|
308 | - } else { |
|
309 | - // Uppercase. |
|
310 | - if ($lastWasUpper === false) { |
|
311 | - $friendlyName .= ' '; |
|
312 | - if ($i < ($length - 1)) { |
|
313 | - $next = $name[($i + 1)]; |
|
314 | - if (strtolower($next) === $next) { |
|
315 | - // Next char is lowercase so it is a word boundary. |
|
316 | - $name[$i] = strtolower($name[$i]); |
|
317 | - } |
|
318 | - } |
|
319 | - } |
|
320 | - |
|
321 | - $lastWasUpper = true; |
|
322 | - } |
|
323 | - }//end if |
|
324 | - |
|
325 | - $friendlyName .= $name[$i]; |
|
326 | - }//end for |
|
327 | - |
|
328 | - $friendlyName = trim($friendlyName); |
|
329 | - $friendlyName[0] = strtoupper($friendlyName[0]); |
|
330 | - |
|
331 | - return $friendlyName; |
|
332 | - |
|
333 | - }//end makeFriendlyName() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
36 | + // Nothing to print. |
|
37 | + return false; |
|
38 | + } |
|
39 | + |
|
40 | + $sources = []; |
|
41 | + |
|
42 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
43 | + foreach ($lineErrors as $column => $colErrors) { |
|
44 | + foreach ($colErrors as $error) { |
|
45 | + $src = $error['source']; |
|
46 | + if (isset($sources[$src]) === false) { |
|
47 | + $sources[$src] = [ |
|
48 | + 'fixable' => (int) $error['fixable'], |
|
49 | + 'count' => 1, |
|
50 | + ]; |
|
51 | + } else { |
|
52 | + $sources[$src]['count']++; |
|
53 | + } |
|
54 | + } |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + foreach ($sources as $source => $data) { |
|
59 | + echo $source.'>>'.$data['fixable'].'>>'.$data['count'].PHP_EOL; |
|
60 | + } |
|
61 | + |
|
62 | + return true; |
|
63 | + |
|
64 | + }//end generateFileReport() |
|
65 | + |
|
66 | + |
|
67 | + /** |
|
68 | + * Prints the source of all errors and warnings. |
|
69 | + * |
|
70 | + * @param string $cachedData Any partial report data that was returned from |
|
71 | + * generateFileReport during the run. |
|
72 | + * @param int $totalFiles Total number of files processed during the run. |
|
73 | + * @param int $totalErrors Total number of errors found during the run. |
|
74 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
75 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
76 | + * @param bool $showSources Show sources? |
|
77 | + * @param int $width Maximum allowed line width. |
|
78 | + * @param bool $interactive Are we running in interactive mode? |
|
79 | + * @param bool $toScreen Is the report being printed to screen? |
|
80 | + * |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function generate( |
|
84 | + $cachedData, |
|
85 | + $totalFiles, |
|
86 | + $totalErrors, |
|
87 | + $totalWarnings, |
|
88 | + $totalFixable, |
|
89 | + $showSources=false, |
|
90 | + $width=80, |
|
91 | + $interactive=false, |
|
92 | + $toScreen=true |
|
93 | + ) { |
|
94 | + $lines = explode(PHP_EOL, $cachedData); |
|
95 | + array_pop($lines); |
|
96 | + |
|
97 | + if (empty($lines) === true) { |
|
98 | + return; |
|
99 | + } |
|
100 | + |
|
101 | + $sources = []; |
|
102 | + $maxLength = 0; |
|
103 | + |
|
104 | + foreach ($lines as $line) { |
|
105 | + $parts = explode('>>', $line); |
|
106 | + $source = $parts[0]; |
|
107 | + $fixable = (bool) $parts[1]; |
|
108 | + $count = $parts[2]; |
|
109 | + |
|
110 | + if (isset($sources[$source]) === false) { |
|
111 | + if ($showSources === true) { |
|
112 | + $parts = null; |
|
113 | + $sniff = $source; |
|
114 | + } else { |
|
115 | + $parts = explode('.', $source); |
|
116 | + if ($parts[0] === 'Internal') { |
|
117 | + $parts[2] = $parts[1]; |
|
118 | + $parts[1] = ''; |
|
119 | + } |
|
120 | + |
|
121 | + $parts[1] = $this->makeFriendlyName($parts[1]); |
|
122 | + |
|
123 | + $sniff = $this->makeFriendlyName($parts[2]); |
|
124 | + if (isset($parts[3]) === true) { |
|
125 | + $name = $this->makeFriendlyName($parts[3]); |
|
126 | + $name[0] = strtolower($name[0]); |
|
127 | + $sniff .= ' '.$name; |
|
128 | + unset($parts[3]); |
|
129 | + } |
|
130 | + |
|
131 | + $parts[2] = $sniff; |
|
132 | + }//end if |
|
133 | + |
|
134 | + $maxLength = max($maxLength, strlen($sniff)); |
|
135 | + |
|
136 | + $sources[$source] = [ |
|
137 | + 'count' => $count, |
|
138 | + 'fixable' => $fixable, |
|
139 | + 'parts' => $parts, |
|
140 | + ]; |
|
141 | + } else { |
|
142 | + $sources[$source]['count'] += $count; |
|
143 | + }//end if |
|
144 | + }//end foreach |
|
145 | + |
|
146 | + if ($showSources === true) { |
|
147 | + $width = min($width, ($maxLength + 11)); |
|
148 | + } else { |
|
149 | + $width = min($width, ($maxLength + 41)); |
|
150 | + } |
|
151 | + |
|
152 | + $width = max($width, 70); |
|
153 | + |
|
154 | + // Sort the data based on counts and source code. |
|
155 | + $sourceCodes = array_keys($sources); |
|
156 | + $counts = []; |
|
157 | + foreach ($sources as $source => $data) { |
|
158 | + $counts[$source] = $data['count']; |
|
159 | + } |
|
160 | + |
|
161 | + array_multisort($counts, SORT_DESC, $sourceCodes, SORT_ASC, SORT_NATURAL, $sources); |
|
162 | + |
|
163 | + echo PHP_EOL."\033[1mPHP CODE SNIFFER VIOLATION SOURCE SUMMARY\033[0m".PHP_EOL; |
|
164 | + echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
|
165 | + if ($showSources === true) { |
|
166 | + if ($totalFixable > 0) { |
|
167 | + echo ' SOURCE'.str_repeat(' ', ($width - 15)).'COUNT'.PHP_EOL; |
|
168 | + } else { |
|
169 | + echo 'SOURCE'.str_repeat(' ', ($width - 11)).'COUNT'.PHP_EOL; |
|
170 | + } |
|
171 | + } else { |
|
172 | + if ($totalFixable > 0) { |
|
173 | + echo ' STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 44)).'COUNT'.PHP_EOL; |
|
174 | + } else { |
|
175 | + echo 'STANDARD CATEGORY SNIFF'.str_repeat(' ', ($width - 40)).'COUNT'.PHP_EOL; |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + echo "\033[0m".str_repeat('-', $width).PHP_EOL; |
|
180 | + |
|
181 | + $fixableSources = 0; |
|
182 | + |
|
183 | + if ($showSources === true) { |
|
184 | + $maxSniffWidth = ($width - 7); |
|
185 | + } else { |
|
186 | + $maxSniffWidth = ($width - 37); |
|
187 | + } |
|
188 | + |
|
189 | + if ($totalFixable > 0) { |
|
190 | + $maxSniffWidth -= 4; |
|
191 | + } |
|
192 | + |
|
193 | + foreach ($sources as $source => $sourceData) { |
|
194 | + if ($totalFixable > 0) { |
|
195 | + echo '['; |
|
196 | + if ($sourceData['fixable'] === true) { |
|
197 | + echo 'x'; |
|
198 | + $fixableSources++; |
|
199 | + } else { |
|
200 | + echo ' '; |
|
201 | + } |
|
202 | + |
|
203 | + echo '] '; |
|
204 | + } |
|
205 | + |
|
206 | + if ($showSources === true) { |
|
207 | + if (strlen($source) > $maxSniffWidth) { |
|
208 | + $source = substr($source, 0, $maxSniffWidth); |
|
209 | + } |
|
210 | + |
|
211 | + echo $source; |
|
212 | + if ($totalFixable > 0) { |
|
213 | + echo str_repeat(' ', ($width - 9 - strlen($source))); |
|
214 | + } else { |
|
215 | + echo str_repeat(' ', ($width - 5 - strlen($source))); |
|
216 | + } |
|
217 | + } else { |
|
218 | + $parts = $sourceData['parts']; |
|
219 | + |
|
220 | + if (strlen($parts[0]) > 8) { |
|
221 | + $parts[0] = substr($parts[0], 0, ((strlen($parts[0]) - 8) * -1)); |
|
222 | + } |
|
223 | + |
|
224 | + echo $parts[0].str_repeat(' ', (10 - strlen($parts[0]))); |
|
225 | + |
|
226 | + $category = $parts[1]; |
|
227 | + if (strlen($category) > 18) { |
|
228 | + $category = substr($category, 0, ((strlen($category) - 18) * -1)); |
|
229 | + } |
|
230 | + |
|
231 | + echo $category.str_repeat(' ', (20 - strlen($category))); |
|
232 | + |
|
233 | + $sniff = $parts[2]; |
|
234 | + if (strlen($sniff) > $maxSniffWidth) { |
|
235 | + $sniff = substr($sniff, 0, $maxSniffWidth); |
|
236 | + } |
|
237 | + |
|
238 | + if ($totalFixable > 0) { |
|
239 | + echo $sniff.str_repeat(' ', ($width - 39 - strlen($sniff))); |
|
240 | + } else { |
|
241 | + echo $sniff.str_repeat(' ', ($width - 35 - strlen($sniff))); |
|
242 | + } |
|
243 | + }//end if |
|
244 | + |
|
245 | + echo $sourceData['count'].PHP_EOL; |
|
246 | + }//end foreach |
|
247 | + |
|
248 | + echo str_repeat('-', $width).PHP_EOL; |
|
249 | + echo "\033[1m".'A TOTAL OF '.($totalErrors + $totalWarnings).' SNIFF VIOLATION'; |
|
250 | + if (($totalErrors + $totalWarnings) > 1) { |
|
251 | + echo 'S'; |
|
252 | + } |
|
253 | + |
|
254 | + echo ' WERE FOUND IN '.count($sources).' SOURCE'; |
|
255 | + if (count($sources) !== 1) { |
|
256 | + echo 'S'; |
|
257 | + } |
|
258 | + |
|
259 | + echo "\033[0m"; |
|
260 | + |
|
261 | + if ($totalFixable > 0) { |
|
262 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
263 | + echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
|
264 | + } |
|
265 | + |
|
266 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
267 | + |
|
268 | + if ($toScreen === true && $interactive === false) { |
|
269 | + Timing::printRunTime(); |
|
270 | + } |
|
271 | + |
|
272 | + }//end generate() |
|
273 | + |
|
274 | + |
|
275 | + /** |
|
276 | + * Converts a camel caps name into a readable string. |
|
277 | + * |
|
278 | + * @param string $name The camel caps name to convert. |
|
279 | + * |
|
280 | + * @return string |
|
281 | + */ |
|
282 | + public function makeFriendlyName($name) |
|
283 | + { |
|
284 | + if (trim($name) === '') { |
|
285 | + return ''; |
|
286 | + } |
|
287 | + |
|
288 | + $friendlyName = ''; |
|
289 | + $length = strlen($name); |
|
290 | + |
|
291 | + $lastWasUpper = false; |
|
292 | + $lastWasNumeric = false; |
|
293 | + for ($i = 0; $i < $length; $i++) { |
|
294 | + if (is_numeric($name[$i]) === true) { |
|
295 | + if ($lastWasNumeric === false) { |
|
296 | + $friendlyName .= ' '; |
|
297 | + } |
|
298 | + |
|
299 | + $lastWasUpper = false; |
|
300 | + $lastWasNumeric = true; |
|
301 | + } else { |
|
302 | + $lastWasNumeric = false; |
|
303 | + |
|
304 | + $char = strtolower($name[$i]); |
|
305 | + if ($char === $name[$i]) { |
|
306 | + // Lowercase. |
|
307 | + $lastWasUpper = false; |
|
308 | + } else { |
|
309 | + // Uppercase. |
|
310 | + if ($lastWasUpper === false) { |
|
311 | + $friendlyName .= ' '; |
|
312 | + if ($i < ($length - 1)) { |
|
313 | + $next = $name[($i + 1)]; |
|
314 | + if (strtolower($next) === $next) { |
|
315 | + // Next char is lowercase so it is a word boundary. |
|
316 | + $name[$i] = strtolower($name[$i]); |
|
317 | + } |
|
318 | + } |
|
319 | + } |
|
320 | + |
|
321 | + $lastWasUpper = true; |
|
322 | + } |
|
323 | + }//end if |
|
324 | + |
|
325 | + $friendlyName .= $name[$i]; |
|
326 | + }//end for |
|
327 | + |
|
328 | + $friendlyName = trim($friendlyName); |
|
329 | + $friendlyName[0] = strtoupper($friendlyName[0]); |
|
330 | + |
|
331 | + return $friendlyName; |
|
332 | + |
|
333 | + }//end makeFriendlyName() |
|
334 | 334 | |
335 | 335 | |
336 | 336 | }//end class |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | * |
95 | 95 | * @return bool |
96 | 96 | */ |
97 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
97 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
98 | 98 | { |
99 | 99 | echo $report['filename'].PHP_EOL; |
100 | 100 | |
@@ -127,10 +127,10 @@ discard block |
||
127 | 127 | $totalErrors, |
128 | 128 | $totalWarnings, |
129 | 129 | $totalFixable, |
130 | - $showSources=false, |
|
131 | - $width=80, |
|
132 | - $interactive=false, |
|
133 | - $toScreen=true |
|
130 | + $showSources = false, |
|
131 | + $width = 80, |
|
132 | + $interactive = false, |
|
133 | + $toScreen = true |
|
134 | 134 | ) { |
135 | 135 | $checkedFiles = explode(PHP_EOL, trim($cachedData)); |
136 | 136 |
@@ -23,220 +23,220 @@ |
||
23 | 23 | class Notifysend implements Report |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * Notification timeout in milliseconds. |
|
28 | - * |
|
29 | - * @var integer |
|
30 | - */ |
|
31 | - protected $timeout = 3000; |
|
32 | - |
|
33 | - /** |
|
34 | - * Path to notify-send command. |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $path = 'notify-send'; |
|
39 | - |
|
40 | - /** |
|
41 | - * Show "ok, all fine" messages. |
|
42 | - * |
|
43 | - * @var boolean |
|
44 | - */ |
|
45 | - protected $showOk = true; |
|
46 | - |
|
47 | - /** |
|
48 | - * Version of installed notify-send executable. |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - protected $version = null; |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * Load configuration data. |
|
57 | - */ |
|
58 | - public function __construct() |
|
59 | - { |
|
60 | - $path = Config::getExecutablePath('notifysend'); |
|
61 | - if ($path !== null) { |
|
62 | - $this->path = Common::escapeshellcmd($path); |
|
63 | - } |
|
64 | - |
|
65 | - $timeout = Config::getConfigData('notifysend_timeout'); |
|
66 | - if ($timeout !== null) { |
|
67 | - $this->timeout = (int) $timeout; |
|
68 | - } |
|
69 | - |
|
70 | - $showOk = Config::getConfigData('notifysend_showok'); |
|
71 | - if ($showOk !== null) { |
|
72 | - $this->showOk = (bool) $showOk; |
|
73 | - } |
|
74 | - |
|
75 | - $this->version = str_replace( |
|
76 | - 'notify-send ', |
|
77 | - '', |
|
78 | - exec($this->path.' --version') |
|
79 | - ); |
|
80 | - |
|
81 | - }//end __construct() |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Generate a partial report for a single processed file. |
|
86 | - * |
|
87 | - * Function should return TRUE if it printed or stored data about the file |
|
88 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
89 | - * its data should be counted in the grand totals. |
|
90 | - * |
|
91 | - * @param array $report Prepared report data. |
|
92 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
93 | - * @param bool $showSources Show sources? |
|
94 | - * @param int $width Maximum allowed line width. |
|
95 | - * |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
99 | - { |
|
100 | - echo $report['filename'].PHP_EOL; |
|
101 | - |
|
102 | - // We want this file counted in the total number |
|
103 | - // of checked files even if it has no errors. |
|
104 | - return true; |
|
105 | - |
|
106 | - }//end generateFileReport() |
|
107 | - |
|
108 | - |
|
109 | - /** |
|
110 | - * Generates a summary of errors and warnings for each file processed. |
|
111 | - * |
|
112 | - * @param string $cachedData Any partial report data that was returned from |
|
113 | - * generateFileReport during the run. |
|
114 | - * @param int $totalFiles Total number of files processed during the run. |
|
115 | - * @param int $totalErrors Total number of errors found during the run. |
|
116 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
117 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
118 | - * @param bool $showSources Show sources? |
|
119 | - * @param int $width Maximum allowed line width. |
|
120 | - * @param bool $interactive Are we running in interactive mode? |
|
121 | - * @param bool $toScreen Is the report being printed to screen? |
|
122 | - * |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - public function generate( |
|
126 | - $cachedData, |
|
127 | - $totalFiles, |
|
128 | - $totalErrors, |
|
129 | - $totalWarnings, |
|
130 | - $totalFixable, |
|
131 | - $showSources=false, |
|
132 | - $width=80, |
|
133 | - $interactive=false, |
|
134 | - $toScreen=true |
|
135 | - ) { |
|
136 | - $checkedFiles = explode(PHP_EOL, trim($cachedData)); |
|
137 | - |
|
138 | - $msg = $this->generateMessage($checkedFiles, $totalErrors, $totalWarnings); |
|
139 | - if ($msg === null) { |
|
140 | - if ($this->showOk === true) { |
|
141 | - $this->notifyAllFine(); |
|
142 | - } |
|
143 | - } else { |
|
144 | - $this->notifyErrors($msg); |
|
145 | - } |
|
146 | - |
|
147 | - }//end generate() |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * Generate the error message to show to the user. |
|
152 | - * |
|
153 | - * @param string[] $checkedFiles The files checked during the run. |
|
154 | - * @param int $totalErrors Total number of errors found during the run. |
|
155 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
156 | - * |
|
157 | - * @return string Error message or NULL if no error/warning found. |
|
158 | - */ |
|
159 | - protected function generateMessage($checkedFiles, $totalErrors, $totalWarnings) |
|
160 | - { |
|
161 | - if ($totalErrors === 0 && $totalWarnings === 0) { |
|
162 | - // Nothing to print. |
|
163 | - return null; |
|
164 | - } |
|
165 | - |
|
166 | - $totalFiles = count($checkedFiles); |
|
167 | - |
|
168 | - $msg = ''; |
|
169 | - if ($totalFiles > 1) { |
|
170 | - $msg .= 'Checked '.$totalFiles.' files'.PHP_EOL; |
|
171 | - } else { |
|
172 | - $msg .= $checkedFiles[0].PHP_EOL; |
|
173 | - } |
|
174 | - |
|
175 | - if ($totalWarnings > 0) { |
|
176 | - $msg .= $totalWarnings.' warnings'.PHP_EOL; |
|
177 | - } |
|
178 | - |
|
179 | - if ($totalErrors > 0) { |
|
180 | - $msg .= $totalErrors.' errors'.PHP_EOL; |
|
181 | - } |
|
182 | - |
|
183 | - return $msg; |
|
184 | - |
|
185 | - }//end generateMessage() |
|
186 | - |
|
187 | - |
|
188 | - /** |
|
189 | - * Tell the user that all is fine and no error/warning has been found. |
|
190 | - * |
|
191 | - * @return void |
|
192 | - */ |
|
193 | - protected function notifyAllFine() |
|
194 | - { |
|
195 | - $cmd = $this->getBasicCommand(); |
|
196 | - $cmd .= ' -i info'; |
|
197 | - $cmd .= ' "PHP CodeSniffer: Ok"'; |
|
198 | - $cmd .= ' "All fine"'; |
|
199 | - exec($cmd); |
|
200 | - |
|
201 | - }//end notifyAllFine() |
|
202 | - |
|
203 | - |
|
204 | - /** |
|
205 | - * Tell the user that errors/warnings have been found. |
|
206 | - * |
|
207 | - * @param string $msg Message to display. |
|
208 | - * |
|
209 | - * @return void |
|
210 | - */ |
|
211 | - protected function notifyErrors($msg) |
|
212 | - { |
|
213 | - $cmd = $this->getBasicCommand(); |
|
214 | - $cmd .= ' -i error'; |
|
215 | - $cmd .= ' "PHP CodeSniffer: Error"'; |
|
216 | - $cmd .= ' '.escapeshellarg(trim($msg)); |
|
217 | - exec($cmd); |
|
218 | - |
|
219 | - }//end notifyErrors() |
|
220 | - |
|
221 | - |
|
222 | - /** |
|
223 | - * Generate and return the basic notify-send command string to execute. |
|
224 | - * |
|
225 | - * @return string Shell command with common parameters. |
|
226 | - */ |
|
227 | - protected function getBasicCommand() |
|
228 | - { |
|
229 | - $cmd = $this->path; |
|
230 | - $cmd .= ' --category dev.validate'; |
|
231 | - $cmd .= ' -h int:transient:1'; |
|
232 | - $cmd .= ' -t '.(int) $this->timeout; |
|
233 | - if (version_compare($this->version, '0.7.3', '>=') === true) { |
|
234 | - $cmd .= ' -a phpcs'; |
|
235 | - } |
|
236 | - |
|
237 | - return $cmd; |
|
238 | - |
|
239 | - }//end getBasicCommand() |
|
26 | + /** |
|
27 | + * Notification timeout in milliseconds. |
|
28 | + * |
|
29 | + * @var integer |
|
30 | + */ |
|
31 | + protected $timeout = 3000; |
|
32 | + |
|
33 | + /** |
|
34 | + * Path to notify-send command. |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $path = 'notify-send'; |
|
39 | + |
|
40 | + /** |
|
41 | + * Show "ok, all fine" messages. |
|
42 | + * |
|
43 | + * @var boolean |
|
44 | + */ |
|
45 | + protected $showOk = true; |
|
46 | + |
|
47 | + /** |
|
48 | + * Version of installed notify-send executable. |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + protected $version = null; |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * Load configuration data. |
|
57 | + */ |
|
58 | + public function __construct() |
|
59 | + { |
|
60 | + $path = Config::getExecutablePath('notifysend'); |
|
61 | + if ($path !== null) { |
|
62 | + $this->path = Common::escapeshellcmd($path); |
|
63 | + } |
|
64 | + |
|
65 | + $timeout = Config::getConfigData('notifysend_timeout'); |
|
66 | + if ($timeout !== null) { |
|
67 | + $this->timeout = (int) $timeout; |
|
68 | + } |
|
69 | + |
|
70 | + $showOk = Config::getConfigData('notifysend_showok'); |
|
71 | + if ($showOk !== null) { |
|
72 | + $this->showOk = (bool) $showOk; |
|
73 | + } |
|
74 | + |
|
75 | + $this->version = str_replace( |
|
76 | + 'notify-send ', |
|
77 | + '', |
|
78 | + exec($this->path.' --version') |
|
79 | + ); |
|
80 | + |
|
81 | + }//end __construct() |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Generate a partial report for a single processed file. |
|
86 | + * |
|
87 | + * Function should return TRUE if it printed or stored data about the file |
|
88 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
89 | + * its data should be counted in the grand totals. |
|
90 | + * |
|
91 | + * @param array $report Prepared report data. |
|
92 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
93 | + * @param bool $showSources Show sources? |
|
94 | + * @param int $width Maximum allowed line width. |
|
95 | + * |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
99 | + { |
|
100 | + echo $report['filename'].PHP_EOL; |
|
101 | + |
|
102 | + // We want this file counted in the total number |
|
103 | + // of checked files even if it has no errors. |
|
104 | + return true; |
|
105 | + |
|
106 | + }//end generateFileReport() |
|
107 | + |
|
108 | + |
|
109 | + /** |
|
110 | + * Generates a summary of errors and warnings for each file processed. |
|
111 | + * |
|
112 | + * @param string $cachedData Any partial report data that was returned from |
|
113 | + * generateFileReport during the run. |
|
114 | + * @param int $totalFiles Total number of files processed during the run. |
|
115 | + * @param int $totalErrors Total number of errors found during the run. |
|
116 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
117 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
118 | + * @param bool $showSources Show sources? |
|
119 | + * @param int $width Maximum allowed line width. |
|
120 | + * @param bool $interactive Are we running in interactive mode? |
|
121 | + * @param bool $toScreen Is the report being printed to screen? |
|
122 | + * |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + public function generate( |
|
126 | + $cachedData, |
|
127 | + $totalFiles, |
|
128 | + $totalErrors, |
|
129 | + $totalWarnings, |
|
130 | + $totalFixable, |
|
131 | + $showSources=false, |
|
132 | + $width=80, |
|
133 | + $interactive=false, |
|
134 | + $toScreen=true |
|
135 | + ) { |
|
136 | + $checkedFiles = explode(PHP_EOL, trim($cachedData)); |
|
137 | + |
|
138 | + $msg = $this->generateMessage($checkedFiles, $totalErrors, $totalWarnings); |
|
139 | + if ($msg === null) { |
|
140 | + if ($this->showOk === true) { |
|
141 | + $this->notifyAllFine(); |
|
142 | + } |
|
143 | + } else { |
|
144 | + $this->notifyErrors($msg); |
|
145 | + } |
|
146 | + |
|
147 | + }//end generate() |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * Generate the error message to show to the user. |
|
152 | + * |
|
153 | + * @param string[] $checkedFiles The files checked during the run. |
|
154 | + * @param int $totalErrors Total number of errors found during the run. |
|
155 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
156 | + * |
|
157 | + * @return string Error message or NULL if no error/warning found. |
|
158 | + */ |
|
159 | + protected function generateMessage($checkedFiles, $totalErrors, $totalWarnings) |
|
160 | + { |
|
161 | + if ($totalErrors === 0 && $totalWarnings === 0) { |
|
162 | + // Nothing to print. |
|
163 | + return null; |
|
164 | + } |
|
165 | + |
|
166 | + $totalFiles = count($checkedFiles); |
|
167 | + |
|
168 | + $msg = ''; |
|
169 | + if ($totalFiles > 1) { |
|
170 | + $msg .= 'Checked '.$totalFiles.' files'.PHP_EOL; |
|
171 | + } else { |
|
172 | + $msg .= $checkedFiles[0].PHP_EOL; |
|
173 | + } |
|
174 | + |
|
175 | + if ($totalWarnings > 0) { |
|
176 | + $msg .= $totalWarnings.' warnings'.PHP_EOL; |
|
177 | + } |
|
178 | + |
|
179 | + if ($totalErrors > 0) { |
|
180 | + $msg .= $totalErrors.' errors'.PHP_EOL; |
|
181 | + } |
|
182 | + |
|
183 | + return $msg; |
|
184 | + |
|
185 | + }//end generateMessage() |
|
186 | + |
|
187 | + |
|
188 | + /** |
|
189 | + * Tell the user that all is fine and no error/warning has been found. |
|
190 | + * |
|
191 | + * @return void |
|
192 | + */ |
|
193 | + protected function notifyAllFine() |
|
194 | + { |
|
195 | + $cmd = $this->getBasicCommand(); |
|
196 | + $cmd .= ' -i info'; |
|
197 | + $cmd .= ' "PHP CodeSniffer: Ok"'; |
|
198 | + $cmd .= ' "All fine"'; |
|
199 | + exec($cmd); |
|
200 | + |
|
201 | + }//end notifyAllFine() |
|
202 | + |
|
203 | + |
|
204 | + /** |
|
205 | + * Tell the user that errors/warnings have been found. |
|
206 | + * |
|
207 | + * @param string $msg Message to display. |
|
208 | + * |
|
209 | + * @return void |
|
210 | + */ |
|
211 | + protected function notifyErrors($msg) |
|
212 | + { |
|
213 | + $cmd = $this->getBasicCommand(); |
|
214 | + $cmd .= ' -i error'; |
|
215 | + $cmd .= ' "PHP CodeSniffer: Error"'; |
|
216 | + $cmd .= ' '.escapeshellarg(trim($msg)); |
|
217 | + exec($cmd); |
|
218 | + |
|
219 | + }//end notifyErrors() |
|
220 | + |
|
221 | + |
|
222 | + /** |
|
223 | + * Generate and return the basic notify-send command string to execute. |
|
224 | + * |
|
225 | + * @return string Shell command with common parameters. |
|
226 | + */ |
|
227 | + protected function getBasicCommand() |
|
228 | + { |
|
229 | + $cmd = $this->path; |
|
230 | + $cmd .= ' --category dev.validate'; |
|
231 | + $cmd .= ' -h int:transient:1'; |
|
232 | + $cmd .= ' -t '.(int) $this->timeout; |
|
233 | + if (version_compare($this->version, '0.7.3', '>=') === true) { |
|
234 | + $cmd .= ' -a phpcs'; |
|
235 | + } |
|
236 | + |
|
237 | + return $cmd; |
|
238 | + |
|
239 | + }//end getBasicCommand() |
|
240 | 240 | |
241 | 241 | |
242 | 242 | }//end class |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | $filename = str_replace('\\', '\\\\', $report['filename']); |
36 | 36 | $filename = str_replace('"', '\"', $filename); |
@@ -96,10 +96,10 @@ discard block |
||
96 | 96 | $totalErrors, |
97 | 97 | $totalWarnings, |
98 | 98 | $totalFixable, |
99 | - $showSources=false, |
|
100 | - $width=80, |
|
101 | - $interactive=false, |
|
102 | - $toScreen=true |
|
99 | + $showSources = false, |
|
100 | + $width = 80, |
|
101 | + $interactive = false, |
|
102 | + $toScreen = true |
|
103 | 103 | ) { |
104 | 104 | echo '{"totals":{"errors":'.$totalErrors.',"warnings":'.$totalWarnings.',"fixable":'.$totalFixable.'},"files":{'; |
105 | 105 | echo rtrim($cachedData, ','); |
@@ -16,91 +16,91 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - $filename = str_replace('\\', '\\\\', $report['filename']); |
|
36 | - $filename = str_replace('"', '\"', $filename); |
|
37 | - $filename = str_replace('/', '\/', $filename); |
|
38 | - echo '"'.$filename.'":{'; |
|
39 | - echo '"errors":'.$report['errors'].',"warnings":'.$report['warnings'].',"messages":['; |
|
40 | - |
|
41 | - $messages = ''; |
|
42 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
43 | - foreach ($lineErrors as $column => $colErrors) { |
|
44 | - foreach ($colErrors as $error) { |
|
45 | - $error['message'] = str_replace("\n", '\n', $error['message']); |
|
46 | - $error['message'] = str_replace("\r", '\r', $error['message']); |
|
47 | - $error['message'] = str_replace("\t", '\t', $error['message']); |
|
48 | - |
|
49 | - $fixable = false; |
|
50 | - if ($error['fixable'] === true) { |
|
51 | - $fixable = true; |
|
52 | - } |
|
53 | - |
|
54 | - $messagesObject = (object) $error; |
|
55 | - $messagesObject->line = $line; |
|
56 | - $messagesObject->column = $column; |
|
57 | - $messagesObject->fixable = $fixable; |
|
58 | - |
|
59 | - $messages .= json_encode($messagesObject).","; |
|
60 | - } |
|
61 | - } |
|
62 | - }//end foreach |
|
63 | - |
|
64 | - echo rtrim($messages, ','); |
|
65 | - echo ']},'; |
|
66 | - |
|
67 | - return true; |
|
68 | - |
|
69 | - }//end generateFileReport() |
|
70 | - |
|
71 | - |
|
72 | - /** |
|
73 | - * Generates a JSON report. |
|
74 | - * |
|
75 | - * @param string $cachedData Any partial report data that was returned from |
|
76 | - * generateFileReport during the run. |
|
77 | - * @param int $totalFiles Total number of files processed during the run. |
|
78 | - * @param int $totalErrors Total number of errors found during the run. |
|
79 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
80 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
81 | - * @param bool $showSources Show sources? |
|
82 | - * @param int $width Maximum allowed line width. |
|
83 | - * @param bool $interactive Are we running in interactive mode? |
|
84 | - * @param bool $toScreen Is the report being printed to screen? |
|
85 | - * |
|
86 | - * @return void |
|
87 | - */ |
|
88 | - public function generate( |
|
89 | - $cachedData, |
|
90 | - $totalFiles, |
|
91 | - $totalErrors, |
|
92 | - $totalWarnings, |
|
93 | - $totalFixable, |
|
94 | - $showSources=false, |
|
95 | - $width=80, |
|
96 | - $interactive=false, |
|
97 | - $toScreen=true |
|
98 | - ) { |
|
99 | - echo '{"totals":{"errors":'.$totalErrors.',"warnings":'.$totalWarnings.',"fixable":'.$totalFixable.'},"files":{'; |
|
100 | - echo rtrim($cachedData, ','); |
|
101 | - echo "}}".PHP_EOL; |
|
102 | - |
|
103 | - }//end generate() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + $filename = str_replace('\\', '\\\\', $report['filename']); |
|
36 | + $filename = str_replace('"', '\"', $filename); |
|
37 | + $filename = str_replace('/', '\/', $filename); |
|
38 | + echo '"'.$filename.'":{'; |
|
39 | + echo '"errors":'.$report['errors'].',"warnings":'.$report['warnings'].',"messages":['; |
|
40 | + |
|
41 | + $messages = ''; |
|
42 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
43 | + foreach ($lineErrors as $column => $colErrors) { |
|
44 | + foreach ($colErrors as $error) { |
|
45 | + $error['message'] = str_replace("\n", '\n', $error['message']); |
|
46 | + $error['message'] = str_replace("\r", '\r', $error['message']); |
|
47 | + $error['message'] = str_replace("\t", '\t', $error['message']); |
|
48 | + |
|
49 | + $fixable = false; |
|
50 | + if ($error['fixable'] === true) { |
|
51 | + $fixable = true; |
|
52 | + } |
|
53 | + |
|
54 | + $messagesObject = (object) $error; |
|
55 | + $messagesObject->line = $line; |
|
56 | + $messagesObject->column = $column; |
|
57 | + $messagesObject->fixable = $fixable; |
|
58 | + |
|
59 | + $messages .= json_encode($messagesObject).","; |
|
60 | + } |
|
61 | + } |
|
62 | + }//end foreach |
|
63 | + |
|
64 | + echo rtrim($messages, ','); |
|
65 | + echo ']},'; |
|
66 | + |
|
67 | + return true; |
|
68 | + |
|
69 | + }//end generateFileReport() |
|
70 | + |
|
71 | + |
|
72 | + /** |
|
73 | + * Generates a JSON report. |
|
74 | + * |
|
75 | + * @param string $cachedData Any partial report data that was returned from |
|
76 | + * generateFileReport during the run. |
|
77 | + * @param int $totalFiles Total number of files processed during the run. |
|
78 | + * @param int $totalErrors Total number of errors found during the run. |
|
79 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
80 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
81 | + * @param bool $showSources Show sources? |
|
82 | + * @param int $width Maximum allowed line width. |
|
83 | + * @param bool $interactive Are we running in interactive mode? |
|
84 | + * @param bool $toScreen Is the report being printed to screen? |
|
85 | + * |
|
86 | + * @return void |
|
87 | + */ |
|
88 | + public function generate( |
|
89 | + $cachedData, |
|
90 | + $totalFiles, |
|
91 | + $totalErrors, |
|
92 | + $totalWarnings, |
|
93 | + $totalFixable, |
|
94 | + $showSources=false, |
|
95 | + $width=80, |
|
96 | + $interactive=false, |
|
97 | + $toScreen=true |
|
98 | + ) { |
|
99 | + echo '{"totals":{"errors":'.$totalErrors.',"warnings":'.$totalWarnings.',"fixable":'.$totalFixable.'},"files":{'; |
|
100 | + echo rtrim($cachedData, ','); |
|
101 | + echo "}}".PHP_EOL; |
|
102 | + |
|
103 | + }//end generate() |
|
104 | 104 | |
105 | 105 | |
106 | 106 | }//end class |
@@ -15,77 +15,77 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Generate a partial report for a single processed file. |
|
20 | - * |
|
21 | - * Function should return TRUE if it printed or stored data about the file |
|
22 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
23 | - * its data should be counted in the grand totals. |
|
24 | - * |
|
25 | - * @param array $report Prepared report data. |
|
26 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
27 | - * @param bool $showSources Show sources? |
|
28 | - * @param int $width Maximum allowed line width. |
|
29 | - * |
|
30 | - * @return bool |
|
31 | - */ |
|
32 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | - { |
|
34 | - if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
35 | - // Nothing to print. |
|
36 | - return false; |
|
37 | - } |
|
18 | + /** |
|
19 | + * Generate a partial report for a single processed file. |
|
20 | + * |
|
21 | + * Function should return TRUE if it printed or stored data about the file |
|
22 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
23 | + * its data should be counted in the grand totals. |
|
24 | + * |
|
25 | + * @param array $report Prepared report data. |
|
26 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
27 | + * @param bool $showSources Show sources? |
|
28 | + * @param int $width Maximum allowed line width. |
|
29 | + * |
|
30 | + * @return bool |
|
31 | + */ |
|
32 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + { |
|
34 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
35 | + // Nothing to print. |
|
36 | + return false; |
|
37 | + } |
|
38 | 38 | |
39 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
40 | - foreach ($lineErrors as $column => $colErrors) { |
|
41 | - foreach ($colErrors as $error) { |
|
42 | - $filename = str_replace('"', '\"', $report['filename']); |
|
43 | - $message = str_replace('"', '\"', $error['message']); |
|
44 | - $type = strtolower($error['type']); |
|
45 | - $source = $error['source']; |
|
46 | - $severity = $error['severity']; |
|
47 | - $fixable = (int) $error['fixable']; |
|
48 | - echo "\"$filename\",$line,$column,$type,\"$message\",$source,$severity,$fixable".PHP_EOL; |
|
49 | - } |
|
50 | - } |
|
51 | - } |
|
39 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
40 | + foreach ($lineErrors as $column => $colErrors) { |
|
41 | + foreach ($colErrors as $error) { |
|
42 | + $filename = str_replace('"', '\"', $report['filename']); |
|
43 | + $message = str_replace('"', '\"', $error['message']); |
|
44 | + $type = strtolower($error['type']); |
|
45 | + $source = $error['source']; |
|
46 | + $severity = $error['severity']; |
|
47 | + $fixable = (int) $error['fixable']; |
|
48 | + echo "\"$filename\",$line,$column,$type,\"$message\",$source,$severity,$fixable".PHP_EOL; |
|
49 | + } |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | - return true; |
|
53 | + return true; |
|
54 | 54 | |
55 | - }//end generateFileReport() |
|
55 | + }//end generateFileReport() |
|
56 | 56 | |
57 | 57 | |
58 | - /** |
|
59 | - * Generates a csv report. |
|
60 | - * |
|
61 | - * @param string $cachedData Any partial report data that was returned from |
|
62 | - * generateFileReport during the run. |
|
63 | - * @param int $totalFiles Total number of files processed during the run. |
|
64 | - * @param int $totalErrors Total number of errors found during the run. |
|
65 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
66 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
67 | - * @param bool $showSources Show sources? |
|
68 | - * @param int $width Maximum allowed line width. |
|
69 | - * @param bool $interactive Are we running in interactive mode? |
|
70 | - * @param bool $toScreen Is the report being printed to screen? |
|
71 | - * |
|
72 | - * @return void |
|
73 | - */ |
|
74 | - public function generate( |
|
75 | - $cachedData, |
|
76 | - $totalFiles, |
|
77 | - $totalErrors, |
|
78 | - $totalWarnings, |
|
79 | - $totalFixable, |
|
80 | - $showSources=false, |
|
81 | - $width=80, |
|
82 | - $interactive=false, |
|
83 | - $toScreen=true |
|
84 | - ) { |
|
85 | - echo 'File,Line,Column,Type,Message,Source,Severity,Fixable'.PHP_EOL; |
|
86 | - echo $cachedData; |
|
58 | + /** |
|
59 | + * Generates a csv report. |
|
60 | + * |
|
61 | + * @param string $cachedData Any partial report data that was returned from |
|
62 | + * generateFileReport during the run. |
|
63 | + * @param int $totalFiles Total number of files processed during the run. |
|
64 | + * @param int $totalErrors Total number of errors found during the run. |
|
65 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
66 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
67 | + * @param bool $showSources Show sources? |
|
68 | + * @param int $width Maximum allowed line width. |
|
69 | + * @param bool $interactive Are we running in interactive mode? |
|
70 | + * @param bool $toScreen Is the report being printed to screen? |
|
71 | + * |
|
72 | + * @return void |
|
73 | + */ |
|
74 | + public function generate( |
|
75 | + $cachedData, |
|
76 | + $totalFiles, |
|
77 | + $totalErrors, |
|
78 | + $totalWarnings, |
|
79 | + $totalFixable, |
|
80 | + $showSources=false, |
|
81 | + $width=80, |
|
82 | + $interactive=false, |
|
83 | + $toScreen=true |
|
84 | + ) { |
|
85 | + echo 'File,Line,Column,Type,Message,Source,Severity,Fixable'.PHP_EOL; |
|
86 | + echo $cachedData; |
|
87 | 87 | |
88 | - }//end generate() |
|
88 | + }//end generate() |
|
89 | 89 | |
90 | 90 | |
91 | 91 | }//end class |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @return bool |
31 | 31 | */ |
32 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
32 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
33 | 33 | { |
34 | 34 | if ($report['errors'] === 0 && $report['warnings'] === 0) { |
35 | 35 | // Nothing to print. |
@@ -77,10 +77,10 @@ discard block |
||
77 | 77 | $totalErrors, |
78 | 78 | $totalWarnings, |
79 | 79 | $totalFixable, |
80 | - $showSources=false, |
|
81 | - $width=80, |
|
82 | - $interactive=false, |
|
83 | - $toScreen=true |
|
80 | + $showSources = false, |
|
81 | + $width = 80, |
|
82 | + $interactive = false, |
|
83 | + $toScreen = true |
|
84 | 84 | ) { |
85 | 85 | echo 'File,Line,Column,Type,Message,Source,Severity,Fixable'.PHP_EOL; |
86 | 86 | echo $cachedData; |
@@ -16,347 +16,347 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
36 | - // Nothing to print. |
|
37 | - return false; |
|
38 | - } |
|
39 | - |
|
40 | - // How many lines to show about and below the error line. |
|
41 | - $surroundingLines = 2; |
|
42 | - |
|
43 | - $file = $report['filename']; |
|
44 | - $tokens = $phpcsFile->getTokens(); |
|
45 | - if (empty($tokens) === true) { |
|
46 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
47 | - $startTime = microtime(true); |
|
48 | - echo 'CODE report is parsing '.basename($file).' '; |
|
49 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
50 | - echo "CODE report is forcing parse of $file".PHP_EOL; |
|
51 | - } |
|
52 | - |
|
53 | - try { |
|
54 | - $phpcsFile->parse(); |
|
55 | - } catch (\Exception $e) { |
|
56 | - // This is a second parse, so ignore exceptions. |
|
57 | - // They would have been added to the file's error list already. |
|
58 | - } |
|
59 | - |
|
60 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
61 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
62 | - if ($timeTaken < 1000) { |
|
63 | - $timeTaken = round($timeTaken); |
|
64 | - echo "DONE in {$timeTaken}ms"; |
|
65 | - } else { |
|
66 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
67 | - echo "DONE in $timeTaken secs"; |
|
68 | - } |
|
69 | - |
|
70 | - echo PHP_EOL; |
|
71 | - } |
|
72 | - |
|
73 | - $tokens = $phpcsFile->getTokens(); |
|
74 | - }//end if |
|
75 | - |
|
76 | - // Create an array that maps lines to the first token on the line. |
|
77 | - $lineTokens = []; |
|
78 | - $lastLine = 0; |
|
79 | - $stackPtr = 0; |
|
80 | - foreach ($tokens as $stackPtr => $token) { |
|
81 | - if ($token['line'] !== $lastLine) { |
|
82 | - if ($lastLine > 0) { |
|
83 | - $lineTokens[$lastLine]['end'] = ($stackPtr - 1); |
|
84 | - } |
|
85 | - |
|
86 | - $lastLine++; |
|
87 | - $lineTokens[$lastLine] = [ |
|
88 | - 'start' => $stackPtr, |
|
89 | - 'end' => null, |
|
90 | - ]; |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - // Make sure the last token in the file sits on an imaginary |
|
95 | - // last line so it is easier to generate code snippets at the |
|
96 | - // end of the file. |
|
97 | - $lineTokens[$lastLine]['end'] = $stackPtr; |
|
98 | - |
|
99 | - // Determine the longest code line we will be showing. |
|
100 | - $maxSnippetLength = 0; |
|
101 | - $eolLen = strlen($phpcsFile->eolChar); |
|
102 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
103 | - $startLine = max(($line - $surroundingLines), 1); |
|
104 | - $endLine = min(($line + $surroundingLines), $lastLine); |
|
105 | - |
|
106 | - $maxLineNumLength = strlen($endLine); |
|
107 | - |
|
108 | - for ($i = $startLine; $i <= $endLine; $i++) { |
|
109 | - if ($i === 1) { |
|
110 | - continue; |
|
111 | - } |
|
112 | - |
|
113 | - $lineLength = ($tokens[($lineTokens[$i]['start'] - 1)]['column'] + $tokens[($lineTokens[$i]['start'] - 1)]['length'] - $eolLen); |
|
114 | - $maxSnippetLength = max($lineLength, $maxSnippetLength); |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - $maxSnippetLength += ($maxLineNumLength + 8); |
|
119 | - |
|
120 | - // Determine the longest error message we will be showing. |
|
121 | - $maxErrorLength = 0; |
|
122 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
123 | - foreach ($lineErrors as $column => $colErrors) { |
|
124 | - foreach ($colErrors as $error) { |
|
125 | - $length = strlen($error['message']); |
|
126 | - if ($showSources === true) { |
|
127 | - $length += (strlen($error['source']) + 3); |
|
128 | - } |
|
129 | - |
|
130 | - $maxErrorLength = max($maxErrorLength, ($length + 1)); |
|
131 | - } |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - // The padding that all lines will require that are printing an error message overflow. |
|
136 | - if ($report['warnings'] > 0) { |
|
137 | - $typeLength = 7; |
|
138 | - } else { |
|
139 | - $typeLength = 5; |
|
140 | - } |
|
141 | - |
|
142 | - $errorPadding = str_repeat(' ', ($maxLineNumLength + 7)); |
|
143 | - $errorPadding .= str_repeat(' ', $typeLength); |
|
144 | - $errorPadding .= ' '; |
|
145 | - if ($report['fixable'] > 0) { |
|
146 | - $errorPadding .= ' '; |
|
147 | - } |
|
148 | - |
|
149 | - $errorPaddingLength = strlen($errorPadding); |
|
150 | - |
|
151 | - // The maximum amount of space an error message can use. |
|
152 | - $maxErrorSpace = ($width - $errorPaddingLength); |
|
153 | - if ($showSources === true) { |
|
154 | - // Account for the chars used to print colors. |
|
155 | - $maxErrorSpace += 8; |
|
156 | - } |
|
157 | - |
|
158 | - // Figure out the max report width we need and can use. |
|
159 | - $fileLength = strlen($file); |
|
160 | - $maxWidth = max(($fileLength + 6), ($maxErrorLength + $errorPaddingLength)); |
|
161 | - $width = max(min($width, $maxWidth), $maxSnippetLength); |
|
162 | - if ($width < 70) { |
|
163 | - $width = 70; |
|
164 | - } |
|
165 | - |
|
166 | - // Print the file header. |
|
167 | - echo PHP_EOL."\033[1mFILE: "; |
|
168 | - if ($fileLength <= ($width - 6)) { |
|
169 | - echo $file; |
|
170 | - } else { |
|
171 | - echo '...'.substr($file, ($fileLength - ($width - 6))); |
|
172 | - } |
|
173 | - |
|
174 | - echo "\033[0m".PHP_EOL; |
|
175 | - echo str_repeat('-', $width).PHP_EOL; |
|
176 | - |
|
177 | - echo "\033[1m".'FOUND '.$report['errors'].' ERROR'; |
|
178 | - if ($report['errors'] !== 1) { |
|
179 | - echo 'S'; |
|
180 | - } |
|
181 | - |
|
182 | - if ($report['warnings'] > 0) { |
|
183 | - echo ' AND '.$report['warnings'].' WARNING'; |
|
184 | - if ($report['warnings'] !== 1) { |
|
185 | - echo 'S'; |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - echo ' AFFECTING '.count($report['messages']).' LINE'; |
|
190 | - if (count($report['messages']) !== 1) { |
|
191 | - echo 'S'; |
|
192 | - } |
|
193 | - |
|
194 | - echo "\033[0m".PHP_EOL; |
|
195 | - |
|
196 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
197 | - $startLine = max(($line - $surroundingLines), 1); |
|
198 | - $endLine = min(($line + $surroundingLines), $lastLine); |
|
199 | - |
|
200 | - $snippet = ''; |
|
201 | - if (isset($lineTokens[$startLine]) === true) { |
|
202 | - for ($i = $lineTokens[$startLine]['start']; $i <= $lineTokens[$endLine]['end']; $i++) { |
|
203 | - $snippetLine = $tokens[$i]['line']; |
|
204 | - if ($lineTokens[$snippetLine]['start'] === $i) { |
|
205 | - // Starting a new line. |
|
206 | - if ($snippetLine === $line) { |
|
207 | - $snippet .= "\033[1m".'>> '; |
|
208 | - } else { |
|
209 | - $snippet .= ' '; |
|
210 | - } |
|
211 | - |
|
212 | - $snippet .= str_repeat(' ', ($maxLineNumLength - strlen($snippetLine))); |
|
213 | - $snippet .= $snippetLine.': '; |
|
214 | - if ($snippetLine === $line) { |
|
215 | - $snippet .= "\033[0m"; |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - if (isset($tokens[$i]['orig_content']) === true) { |
|
220 | - $tokenContent = $tokens[$i]['orig_content']; |
|
221 | - } else { |
|
222 | - $tokenContent = $tokens[$i]['content']; |
|
223 | - } |
|
224 | - |
|
225 | - if (strpos($tokenContent, "\t") !== false) { |
|
226 | - $token = $tokens[$i]; |
|
227 | - $token['content'] = $tokenContent; |
|
228 | - if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|
229 | - $tab = "\000"; |
|
230 | - } else { |
|
231 | - $tab = "\033[30;1m»\033[0m"; |
|
232 | - } |
|
233 | - |
|
234 | - $phpcsFile->tokenizer->replaceTabsInToken($token, $tab, "\000"); |
|
235 | - $tokenContent = $token['content']; |
|
236 | - } |
|
237 | - |
|
238 | - $tokenContent = Util\Common::prepareForOutput($tokenContent, ["\r", "\n", "\t"]); |
|
239 | - $tokenContent = str_replace("\000", ' ', $tokenContent); |
|
240 | - |
|
241 | - $underline = false; |
|
242 | - if ($snippetLine === $line && isset($lineErrors[$tokens[$i]['column']]) === true) { |
|
243 | - $underline = true; |
|
244 | - } |
|
245 | - |
|
246 | - // Underline invisible characters as well. |
|
247 | - if ($underline === true && trim($tokenContent) === '') { |
|
248 | - $snippet .= "\033[4m".' '."\033[0m".$tokenContent; |
|
249 | - } else { |
|
250 | - if ($underline === true) { |
|
251 | - $snippet .= "\033[4m"; |
|
252 | - } |
|
253 | - |
|
254 | - $snippet .= $tokenContent; |
|
255 | - |
|
256 | - if ($underline === true) { |
|
257 | - $snippet .= "\033[0m"; |
|
258 | - } |
|
259 | - } |
|
260 | - }//end for |
|
261 | - }//end if |
|
262 | - |
|
263 | - echo str_repeat('-', $width).PHP_EOL; |
|
264 | - |
|
265 | - foreach ($lineErrors as $column => $colErrors) { |
|
266 | - foreach ($colErrors as $error) { |
|
267 | - $padding = ($maxLineNumLength - strlen($line)); |
|
268 | - echo 'LINE '.str_repeat(' ', $padding).$line.': '; |
|
269 | - |
|
270 | - if ($error['type'] === 'ERROR') { |
|
271 | - echo "\033[31mERROR\033[0m"; |
|
272 | - if ($report['warnings'] > 0) { |
|
273 | - echo ' '; |
|
274 | - } |
|
275 | - } else { |
|
276 | - echo "\033[33mWARNING\033[0m"; |
|
277 | - } |
|
278 | - |
|
279 | - echo ' '; |
|
280 | - if ($report['fixable'] > 0) { |
|
281 | - echo '['; |
|
282 | - if ($error['fixable'] === true) { |
|
283 | - echo 'x'; |
|
284 | - } else { |
|
285 | - echo ' '; |
|
286 | - } |
|
287 | - |
|
288 | - echo '] '; |
|
289 | - } |
|
290 | - |
|
291 | - $message = $error['message']; |
|
292 | - $message = str_replace("\n", "\n".$errorPadding, $message); |
|
293 | - if ($showSources === true) { |
|
294 | - $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')'; |
|
295 | - } |
|
296 | - |
|
297 | - $errorMsg = wordwrap( |
|
298 | - $message, |
|
299 | - $maxErrorSpace, |
|
300 | - PHP_EOL.$errorPadding |
|
301 | - ); |
|
302 | - |
|
303 | - echo $errorMsg.PHP_EOL; |
|
304 | - }//end foreach |
|
305 | - }//end foreach |
|
306 | - |
|
307 | - echo str_repeat('-', $width).PHP_EOL; |
|
308 | - echo rtrim($snippet).PHP_EOL; |
|
309 | - }//end foreach |
|
310 | - |
|
311 | - echo str_repeat('-', $width).PHP_EOL; |
|
312 | - if ($report['fixable'] > 0) { |
|
313 | - echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL; |
|
314 | - echo str_repeat('-', $width).PHP_EOL; |
|
315 | - } |
|
316 | - |
|
317 | - return true; |
|
318 | - |
|
319 | - }//end generateFileReport() |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * Prints all errors and warnings for each file processed. |
|
324 | - * |
|
325 | - * @param string $cachedData Any partial report data that was returned from |
|
326 | - * generateFileReport during the run. |
|
327 | - * @param int $totalFiles Total number of files processed during the run. |
|
328 | - * @param int $totalErrors Total number of errors found during the run. |
|
329 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
330 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
331 | - * @param bool $showSources Show sources? |
|
332 | - * @param int $width Maximum allowed line width. |
|
333 | - * @param bool $interactive Are we running in interactive mode? |
|
334 | - * @param bool $toScreen Is the report being printed to screen? |
|
335 | - * |
|
336 | - * @return void |
|
337 | - */ |
|
338 | - public function generate( |
|
339 | - $cachedData, |
|
340 | - $totalFiles, |
|
341 | - $totalErrors, |
|
342 | - $totalWarnings, |
|
343 | - $totalFixable, |
|
344 | - $showSources=false, |
|
345 | - $width=80, |
|
346 | - $interactive=false, |
|
347 | - $toScreen=true |
|
348 | - ) { |
|
349 | - if ($cachedData === '') { |
|
350 | - return; |
|
351 | - } |
|
352 | - |
|
353 | - echo $cachedData; |
|
354 | - |
|
355 | - if ($toScreen === true && $interactive === false) { |
|
356 | - Util\Timing::printRunTime(); |
|
357 | - } |
|
358 | - |
|
359 | - }//end generate() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
36 | + // Nothing to print. |
|
37 | + return false; |
|
38 | + } |
|
39 | + |
|
40 | + // How many lines to show about and below the error line. |
|
41 | + $surroundingLines = 2; |
|
42 | + |
|
43 | + $file = $report['filename']; |
|
44 | + $tokens = $phpcsFile->getTokens(); |
|
45 | + if (empty($tokens) === true) { |
|
46 | + if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
47 | + $startTime = microtime(true); |
|
48 | + echo 'CODE report is parsing '.basename($file).' '; |
|
49 | + } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
50 | + echo "CODE report is forcing parse of $file".PHP_EOL; |
|
51 | + } |
|
52 | + |
|
53 | + try { |
|
54 | + $phpcsFile->parse(); |
|
55 | + } catch (\Exception $e) { |
|
56 | + // This is a second parse, so ignore exceptions. |
|
57 | + // They would have been added to the file's error list already. |
|
58 | + } |
|
59 | + |
|
60 | + if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
61 | + $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
62 | + if ($timeTaken < 1000) { |
|
63 | + $timeTaken = round($timeTaken); |
|
64 | + echo "DONE in {$timeTaken}ms"; |
|
65 | + } else { |
|
66 | + $timeTaken = round(($timeTaken / 1000), 2); |
|
67 | + echo "DONE in $timeTaken secs"; |
|
68 | + } |
|
69 | + |
|
70 | + echo PHP_EOL; |
|
71 | + } |
|
72 | + |
|
73 | + $tokens = $phpcsFile->getTokens(); |
|
74 | + }//end if |
|
75 | + |
|
76 | + // Create an array that maps lines to the first token on the line. |
|
77 | + $lineTokens = []; |
|
78 | + $lastLine = 0; |
|
79 | + $stackPtr = 0; |
|
80 | + foreach ($tokens as $stackPtr => $token) { |
|
81 | + if ($token['line'] !== $lastLine) { |
|
82 | + if ($lastLine > 0) { |
|
83 | + $lineTokens[$lastLine]['end'] = ($stackPtr - 1); |
|
84 | + } |
|
85 | + |
|
86 | + $lastLine++; |
|
87 | + $lineTokens[$lastLine] = [ |
|
88 | + 'start' => $stackPtr, |
|
89 | + 'end' => null, |
|
90 | + ]; |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + // Make sure the last token in the file sits on an imaginary |
|
95 | + // last line so it is easier to generate code snippets at the |
|
96 | + // end of the file. |
|
97 | + $lineTokens[$lastLine]['end'] = $stackPtr; |
|
98 | + |
|
99 | + // Determine the longest code line we will be showing. |
|
100 | + $maxSnippetLength = 0; |
|
101 | + $eolLen = strlen($phpcsFile->eolChar); |
|
102 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
103 | + $startLine = max(($line - $surroundingLines), 1); |
|
104 | + $endLine = min(($line + $surroundingLines), $lastLine); |
|
105 | + |
|
106 | + $maxLineNumLength = strlen($endLine); |
|
107 | + |
|
108 | + for ($i = $startLine; $i <= $endLine; $i++) { |
|
109 | + if ($i === 1) { |
|
110 | + continue; |
|
111 | + } |
|
112 | + |
|
113 | + $lineLength = ($tokens[($lineTokens[$i]['start'] - 1)]['column'] + $tokens[($lineTokens[$i]['start'] - 1)]['length'] - $eolLen); |
|
114 | + $maxSnippetLength = max($lineLength, $maxSnippetLength); |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + $maxSnippetLength += ($maxLineNumLength + 8); |
|
119 | + |
|
120 | + // Determine the longest error message we will be showing. |
|
121 | + $maxErrorLength = 0; |
|
122 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
123 | + foreach ($lineErrors as $column => $colErrors) { |
|
124 | + foreach ($colErrors as $error) { |
|
125 | + $length = strlen($error['message']); |
|
126 | + if ($showSources === true) { |
|
127 | + $length += (strlen($error['source']) + 3); |
|
128 | + } |
|
129 | + |
|
130 | + $maxErrorLength = max($maxErrorLength, ($length + 1)); |
|
131 | + } |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + // The padding that all lines will require that are printing an error message overflow. |
|
136 | + if ($report['warnings'] > 0) { |
|
137 | + $typeLength = 7; |
|
138 | + } else { |
|
139 | + $typeLength = 5; |
|
140 | + } |
|
141 | + |
|
142 | + $errorPadding = str_repeat(' ', ($maxLineNumLength + 7)); |
|
143 | + $errorPadding .= str_repeat(' ', $typeLength); |
|
144 | + $errorPadding .= ' '; |
|
145 | + if ($report['fixable'] > 0) { |
|
146 | + $errorPadding .= ' '; |
|
147 | + } |
|
148 | + |
|
149 | + $errorPaddingLength = strlen($errorPadding); |
|
150 | + |
|
151 | + // The maximum amount of space an error message can use. |
|
152 | + $maxErrorSpace = ($width - $errorPaddingLength); |
|
153 | + if ($showSources === true) { |
|
154 | + // Account for the chars used to print colors. |
|
155 | + $maxErrorSpace += 8; |
|
156 | + } |
|
157 | + |
|
158 | + // Figure out the max report width we need and can use. |
|
159 | + $fileLength = strlen($file); |
|
160 | + $maxWidth = max(($fileLength + 6), ($maxErrorLength + $errorPaddingLength)); |
|
161 | + $width = max(min($width, $maxWidth), $maxSnippetLength); |
|
162 | + if ($width < 70) { |
|
163 | + $width = 70; |
|
164 | + } |
|
165 | + |
|
166 | + // Print the file header. |
|
167 | + echo PHP_EOL."\033[1mFILE: "; |
|
168 | + if ($fileLength <= ($width - 6)) { |
|
169 | + echo $file; |
|
170 | + } else { |
|
171 | + echo '...'.substr($file, ($fileLength - ($width - 6))); |
|
172 | + } |
|
173 | + |
|
174 | + echo "\033[0m".PHP_EOL; |
|
175 | + echo str_repeat('-', $width).PHP_EOL; |
|
176 | + |
|
177 | + echo "\033[1m".'FOUND '.$report['errors'].' ERROR'; |
|
178 | + if ($report['errors'] !== 1) { |
|
179 | + echo 'S'; |
|
180 | + } |
|
181 | + |
|
182 | + if ($report['warnings'] > 0) { |
|
183 | + echo ' AND '.$report['warnings'].' WARNING'; |
|
184 | + if ($report['warnings'] !== 1) { |
|
185 | + echo 'S'; |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + echo ' AFFECTING '.count($report['messages']).' LINE'; |
|
190 | + if (count($report['messages']) !== 1) { |
|
191 | + echo 'S'; |
|
192 | + } |
|
193 | + |
|
194 | + echo "\033[0m".PHP_EOL; |
|
195 | + |
|
196 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
197 | + $startLine = max(($line - $surroundingLines), 1); |
|
198 | + $endLine = min(($line + $surroundingLines), $lastLine); |
|
199 | + |
|
200 | + $snippet = ''; |
|
201 | + if (isset($lineTokens[$startLine]) === true) { |
|
202 | + for ($i = $lineTokens[$startLine]['start']; $i <= $lineTokens[$endLine]['end']; $i++) { |
|
203 | + $snippetLine = $tokens[$i]['line']; |
|
204 | + if ($lineTokens[$snippetLine]['start'] === $i) { |
|
205 | + // Starting a new line. |
|
206 | + if ($snippetLine === $line) { |
|
207 | + $snippet .= "\033[1m".'>> '; |
|
208 | + } else { |
|
209 | + $snippet .= ' '; |
|
210 | + } |
|
211 | + |
|
212 | + $snippet .= str_repeat(' ', ($maxLineNumLength - strlen($snippetLine))); |
|
213 | + $snippet .= $snippetLine.': '; |
|
214 | + if ($snippetLine === $line) { |
|
215 | + $snippet .= "\033[0m"; |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + if (isset($tokens[$i]['orig_content']) === true) { |
|
220 | + $tokenContent = $tokens[$i]['orig_content']; |
|
221 | + } else { |
|
222 | + $tokenContent = $tokens[$i]['content']; |
|
223 | + } |
|
224 | + |
|
225 | + if (strpos($tokenContent, "\t") !== false) { |
|
226 | + $token = $tokens[$i]; |
|
227 | + $token['content'] = $tokenContent; |
|
228 | + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|
229 | + $tab = "\000"; |
|
230 | + } else { |
|
231 | + $tab = "\033[30;1m»\033[0m"; |
|
232 | + } |
|
233 | + |
|
234 | + $phpcsFile->tokenizer->replaceTabsInToken($token, $tab, "\000"); |
|
235 | + $tokenContent = $token['content']; |
|
236 | + } |
|
237 | + |
|
238 | + $tokenContent = Util\Common::prepareForOutput($tokenContent, ["\r", "\n", "\t"]); |
|
239 | + $tokenContent = str_replace("\000", ' ', $tokenContent); |
|
240 | + |
|
241 | + $underline = false; |
|
242 | + if ($snippetLine === $line && isset($lineErrors[$tokens[$i]['column']]) === true) { |
|
243 | + $underline = true; |
|
244 | + } |
|
245 | + |
|
246 | + // Underline invisible characters as well. |
|
247 | + if ($underline === true && trim($tokenContent) === '') { |
|
248 | + $snippet .= "\033[4m".' '."\033[0m".$tokenContent; |
|
249 | + } else { |
|
250 | + if ($underline === true) { |
|
251 | + $snippet .= "\033[4m"; |
|
252 | + } |
|
253 | + |
|
254 | + $snippet .= $tokenContent; |
|
255 | + |
|
256 | + if ($underline === true) { |
|
257 | + $snippet .= "\033[0m"; |
|
258 | + } |
|
259 | + } |
|
260 | + }//end for |
|
261 | + }//end if |
|
262 | + |
|
263 | + echo str_repeat('-', $width).PHP_EOL; |
|
264 | + |
|
265 | + foreach ($lineErrors as $column => $colErrors) { |
|
266 | + foreach ($colErrors as $error) { |
|
267 | + $padding = ($maxLineNumLength - strlen($line)); |
|
268 | + echo 'LINE '.str_repeat(' ', $padding).$line.': '; |
|
269 | + |
|
270 | + if ($error['type'] === 'ERROR') { |
|
271 | + echo "\033[31mERROR\033[0m"; |
|
272 | + if ($report['warnings'] > 0) { |
|
273 | + echo ' '; |
|
274 | + } |
|
275 | + } else { |
|
276 | + echo "\033[33mWARNING\033[0m"; |
|
277 | + } |
|
278 | + |
|
279 | + echo ' '; |
|
280 | + if ($report['fixable'] > 0) { |
|
281 | + echo '['; |
|
282 | + if ($error['fixable'] === true) { |
|
283 | + echo 'x'; |
|
284 | + } else { |
|
285 | + echo ' '; |
|
286 | + } |
|
287 | + |
|
288 | + echo '] '; |
|
289 | + } |
|
290 | + |
|
291 | + $message = $error['message']; |
|
292 | + $message = str_replace("\n", "\n".$errorPadding, $message); |
|
293 | + if ($showSources === true) { |
|
294 | + $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')'; |
|
295 | + } |
|
296 | + |
|
297 | + $errorMsg = wordwrap( |
|
298 | + $message, |
|
299 | + $maxErrorSpace, |
|
300 | + PHP_EOL.$errorPadding |
|
301 | + ); |
|
302 | + |
|
303 | + echo $errorMsg.PHP_EOL; |
|
304 | + }//end foreach |
|
305 | + }//end foreach |
|
306 | + |
|
307 | + echo str_repeat('-', $width).PHP_EOL; |
|
308 | + echo rtrim($snippet).PHP_EOL; |
|
309 | + }//end foreach |
|
310 | + |
|
311 | + echo str_repeat('-', $width).PHP_EOL; |
|
312 | + if ($report['fixable'] > 0) { |
|
313 | + echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL; |
|
314 | + echo str_repeat('-', $width).PHP_EOL; |
|
315 | + } |
|
316 | + |
|
317 | + return true; |
|
318 | + |
|
319 | + }//end generateFileReport() |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * Prints all errors and warnings for each file processed. |
|
324 | + * |
|
325 | + * @param string $cachedData Any partial report data that was returned from |
|
326 | + * generateFileReport during the run. |
|
327 | + * @param int $totalFiles Total number of files processed during the run. |
|
328 | + * @param int $totalErrors Total number of errors found during the run. |
|
329 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
330 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
331 | + * @param bool $showSources Show sources? |
|
332 | + * @param int $width Maximum allowed line width. |
|
333 | + * @param bool $interactive Are we running in interactive mode? |
|
334 | + * @param bool $toScreen Is the report being printed to screen? |
|
335 | + * |
|
336 | + * @return void |
|
337 | + */ |
|
338 | + public function generate( |
|
339 | + $cachedData, |
|
340 | + $totalFiles, |
|
341 | + $totalErrors, |
|
342 | + $totalWarnings, |
|
343 | + $totalFixable, |
|
344 | + $showSources=false, |
|
345 | + $width=80, |
|
346 | + $interactive=false, |
|
347 | + $toScreen=true |
|
348 | + ) { |
|
349 | + if ($cachedData === '') { |
|
350 | + return; |
|
351 | + } |
|
352 | + |
|
353 | + echo $cachedData; |
|
354 | + |
|
355 | + if ($toScreen === true && $interactive === false) { |
|
356 | + Util\Timing::printRunTime(); |
|
357 | + } |
|
358 | + |
|
359 | + }//end generate() |
|
360 | 360 | |
361 | 361 | |
362 | 362 | }//end class |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | if ($report['errors'] === 0 && $report['warnings'] === 0) { |
36 | 36 | // Nothing to print. |
@@ -341,10 +341,10 @@ discard block |
||
341 | 341 | $totalErrors, |
342 | 342 | $totalWarnings, |
343 | 343 | $totalFixable, |
344 | - $showSources=false, |
|
345 | - $width=80, |
|
346 | - $interactive=false, |
|
347 | - $toScreen=true |
|
344 | + $showSources = false, |
|
345 | + $width = 80, |
|
346 | + $interactive = false, |
|
347 | + $toScreen = true |
|
348 | 348 | ) { |
349 | 349 | if ($cachedData === '') { |
350 | 350 | return; |
@@ -16,94 +16,94 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - $out = new \XMLWriter; |
|
36 | - $out->openMemory(); |
|
37 | - $out->setIndent(true); |
|
38 | - |
|
39 | - if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
40 | - // Nothing to print. |
|
41 | - return false; |
|
42 | - } |
|
43 | - |
|
44 | - $out->startElement('file'); |
|
45 | - $out->writeAttribute('name', $report['filename']); |
|
46 | - |
|
47 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
48 | - foreach ($lineErrors as $column => $colErrors) { |
|
49 | - foreach ($colErrors as $error) { |
|
50 | - $error['type'] = strtolower($error['type']); |
|
51 | - if ($phpcsFile->config->encoding !== 'utf-8') { |
|
52 | - $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']); |
|
53 | - } |
|
54 | - |
|
55 | - $out->startElement('error'); |
|
56 | - $out->writeAttribute('line', $line); |
|
57 | - $out->writeAttribute('column', $column); |
|
58 | - $out->writeAttribute('severity', $error['type']); |
|
59 | - $out->writeAttribute('message', $error['message']); |
|
60 | - $out->writeAttribute('source', $error['source']); |
|
61 | - $out->endElement(); |
|
62 | - } |
|
63 | - } |
|
64 | - }//end foreach |
|
65 | - |
|
66 | - $out->endElement(); |
|
67 | - echo $out->flush(); |
|
68 | - |
|
69 | - return true; |
|
70 | - |
|
71 | - }//end generateFileReport() |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * Prints all violations for processed files, in a Checkstyle format. |
|
76 | - * |
|
77 | - * @param string $cachedData Any partial report data that was returned from |
|
78 | - * generateFileReport during the run. |
|
79 | - * @param int $totalFiles Total number of files processed during the run. |
|
80 | - * @param int $totalErrors Total number of errors found during the run. |
|
81 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
82 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
83 | - * @param bool $showSources Show sources? |
|
84 | - * @param int $width Maximum allowed line width. |
|
85 | - * @param bool $interactive Are we running in interactive mode? |
|
86 | - * @param bool $toScreen Is the report being printed to screen? |
|
87 | - * |
|
88 | - * @return void |
|
89 | - */ |
|
90 | - public function generate( |
|
91 | - $cachedData, |
|
92 | - $totalFiles, |
|
93 | - $totalErrors, |
|
94 | - $totalWarnings, |
|
95 | - $totalFixable, |
|
96 | - $showSources=false, |
|
97 | - $width=80, |
|
98 | - $interactive=false, |
|
99 | - $toScreen=true |
|
100 | - ) { |
|
101 | - echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
|
102 | - echo '<checkstyle version="'.Config::VERSION.'">'.PHP_EOL; |
|
103 | - echo $cachedData; |
|
104 | - echo '</checkstyle>'.PHP_EOL; |
|
105 | - |
|
106 | - }//end generate() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + $out = new \XMLWriter; |
|
36 | + $out->openMemory(); |
|
37 | + $out->setIndent(true); |
|
38 | + |
|
39 | + if ($report['errors'] === 0 && $report['warnings'] === 0) { |
|
40 | + // Nothing to print. |
|
41 | + return false; |
|
42 | + } |
|
43 | + |
|
44 | + $out->startElement('file'); |
|
45 | + $out->writeAttribute('name', $report['filename']); |
|
46 | + |
|
47 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
48 | + foreach ($lineErrors as $column => $colErrors) { |
|
49 | + foreach ($colErrors as $error) { |
|
50 | + $error['type'] = strtolower($error['type']); |
|
51 | + if ($phpcsFile->config->encoding !== 'utf-8') { |
|
52 | + $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']); |
|
53 | + } |
|
54 | + |
|
55 | + $out->startElement('error'); |
|
56 | + $out->writeAttribute('line', $line); |
|
57 | + $out->writeAttribute('column', $column); |
|
58 | + $out->writeAttribute('severity', $error['type']); |
|
59 | + $out->writeAttribute('message', $error['message']); |
|
60 | + $out->writeAttribute('source', $error['source']); |
|
61 | + $out->endElement(); |
|
62 | + } |
|
63 | + } |
|
64 | + }//end foreach |
|
65 | + |
|
66 | + $out->endElement(); |
|
67 | + echo $out->flush(); |
|
68 | + |
|
69 | + return true; |
|
70 | + |
|
71 | + }//end generateFileReport() |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * Prints all violations for processed files, in a Checkstyle format. |
|
76 | + * |
|
77 | + * @param string $cachedData Any partial report data that was returned from |
|
78 | + * generateFileReport during the run. |
|
79 | + * @param int $totalFiles Total number of files processed during the run. |
|
80 | + * @param int $totalErrors Total number of errors found during the run. |
|
81 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
82 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
83 | + * @param bool $showSources Show sources? |
|
84 | + * @param int $width Maximum allowed line width. |
|
85 | + * @param bool $interactive Are we running in interactive mode? |
|
86 | + * @param bool $toScreen Is the report being printed to screen? |
|
87 | + * |
|
88 | + * @return void |
|
89 | + */ |
|
90 | + public function generate( |
|
91 | + $cachedData, |
|
92 | + $totalFiles, |
|
93 | + $totalErrors, |
|
94 | + $totalWarnings, |
|
95 | + $totalFixable, |
|
96 | + $showSources=false, |
|
97 | + $width=80, |
|
98 | + $interactive=false, |
|
99 | + $toScreen=true |
|
100 | + ) { |
|
101 | + echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
|
102 | + echo '<checkstyle version="'.Config::VERSION.'">'.PHP_EOL; |
|
103 | + echo $cachedData; |
|
104 | + echo '</checkstyle>'.PHP_EOL; |
|
105 | + |
|
106 | + }//end generate() |
|
107 | 107 | |
108 | 108 | |
109 | 109 | }//end class |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | $out = new \XMLWriter; |
36 | 36 | $out->openMemory(); |
@@ -93,10 +93,10 @@ discard block |
||
93 | 93 | $totalErrors, |
94 | 94 | $totalWarnings, |
95 | 95 | $totalFixable, |
96 | - $showSources=false, |
|
97 | - $width=80, |
|
98 | - $interactive=false, |
|
99 | - $toScreen=true |
|
96 | + $showSources = false, |
|
97 | + $width = 80, |
|
98 | + $interactive = false, |
|
99 | + $toScreen = true |
|
100 | 100 | ) { |
101 | 101 | echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; |
102 | 102 | echo '<checkstyle version="'.Config::VERSION.'">'.PHP_EOL; |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | $metrics = $phpcsFile->getMetrics(); |
36 | 36 | foreach ($metrics as $metric => $data) { |
@@ -66,10 +66,10 @@ discard block |
||
66 | 66 | $totalErrors, |
67 | 67 | $totalWarnings, |
68 | 68 | $totalFixable, |
69 | - $showSources=false, |
|
70 | - $width=80, |
|
71 | - $interactive=false, |
|
72 | - $toScreen=true |
|
69 | + $showSources = false, |
|
70 | + $width = 80, |
|
71 | + $interactive = false, |
|
72 | + $toScreen = true |
|
73 | 73 | ) { |
74 | 74 | $lines = explode(PHP_EOL, $cachedData); |
75 | 75 | array_pop($lines); |
@@ -16,157 +16,157 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - $metrics = $phpcsFile->getMetrics(); |
|
36 | - foreach ($metrics as $metric => $data) { |
|
37 | - foreach ($data['values'] as $value => $count) { |
|
38 | - echo "$metric>>$value>>$count".PHP_EOL; |
|
39 | - } |
|
40 | - } |
|
41 | - |
|
42 | - return true; |
|
43 | - |
|
44 | - }//end generateFileReport() |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * Prints the source of all errors and warnings. |
|
49 | - * |
|
50 | - * @param string $cachedData Any partial report data that was returned from |
|
51 | - * generateFileReport during the run. |
|
52 | - * @param int $totalFiles Total number of files processed during the run. |
|
53 | - * @param int $totalErrors Total number of errors found during the run. |
|
54 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
55 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
56 | - * @param bool $showSources Show sources? |
|
57 | - * @param int $width Maximum allowed line width. |
|
58 | - * @param bool $interactive Are we running in interactive mode? |
|
59 | - * @param bool $toScreen Is the report being printed to screen? |
|
60 | - * |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - public function generate( |
|
64 | - $cachedData, |
|
65 | - $totalFiles, |
|
66 | - $totalErrors, |
|
67 | - $totalWarnings, |
|
68 | - $totalFixable, |
|
69 | - $showSources=false, |
|
70 | - $width=80, |
|
71 | - $interactive=false, |
|
72 | - $toScreen=true |
|
73 | - ) { |
|
74 | - $lines = explode(PHP_EOL, $cachedData); |
|
75 | - array_pop($lines); |
|
76 | - |
|
77 | - if (empty($lines) === true) { |
|
78 | - return; |
|
79 | - } |
|
80 | - |
|
81 | - $metrics = []; |
|
82 | - foreach ($lines as $line) { |
|
83 | - $parts = explode('>>', $line); |
|
84 | - $metric = $parts[0]; |
|
85 | - $value = $parts[1]; |
|
86 | - $count = $parts[2]; |
|
87 | - if (isset($metrics[$metric]) === false) { |
|
88 | - $metrics[$metric] = []; |
|
89 | - } |
|
90 | - |
|
91 | - if (isset($metrics[$metric][$value]) === false) { |
|
92 | - $metrics[$metric][$value] = $count; |
|
93 | - } else { |
|
94 | - $metrics[$metric][$value] += $count; |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - ksort($metrics); |
|
99 | - |
|
100 | - echo PHP_EOL."\033[1m".'PHP CODE SNIFFER INFORMATION REPORT'."\033[0m".PHP_EOL; |
|
101 | - echo str_repeat('-', 70).PHP_EOL; |
|
102 | - |
|
103 | - foreach ($metrics as $metric => $values) { |
|
104 | - if (count($values) === 1) { |
|
105 | - $count = reset($values); |
|
106 | - $value = key($values); |
|
107 | - |
|
108 | - echo "$metric: \033[4m$value\033[0m [$count/$count, 100%]".PHP_EOL; |
|
109 | - } else { |
|
110 | - $totalCount = 0; |
|
111 | - $valueWidth = 0; |
|
112 | - foreach ($values as $value => $count) { |
|
113 | - $totalCount += $count; |
|
114 | - $valueWidth = max($valueWidth, strlen($value)); |
|
115 | - } |
|
116 | - |
|
117 | - // Length of the total string, plus however many |
|
118 | - // thousands separators there are. |
|
119 | - $countWidth = strlen($totalCount); |
|
120 | - $thousandSeparatorCount = floor($countWidth / 3); |
|
121 | - $countWidth += $thousandSeparatorCount; |
|
122 | - |
|
123 | - // Account for 'total' line. |
|
124 | - $valueWidth = max(5, $valueWidth); |
|
125 | - |
|
126 | - echo "$metric:".PHP_EOL; |
|
127 | - |
|
128 | - ksort($values, SORT_NATURAL); |
|
129 | - arsort($values); |
|
130 | - |
|
131 | - $percentPrefixWidth = 0; |
|
132 | - $percentWidth = 6; |
|
133 | - foreach ($values as $value => $count) { |
|
134 | - $percent = round(($count / $totalCount * 100), 2); |
|
135 | - $percentPrefix = ''; |
|
136 | - if ($percent === 0.00) { |
|
137 | - $percent = 0.01; |
|
138 | - $percentPrefix = '<'; |
|
139 | - $percentPrefixWidth = 2; |
|
140 | - $percentWidth = 4; |
|
141 | - } |
|
142 | - |
|
143 | - printf( |
|
144 | - "\t%-{$valueWidth}s => %{$countWidth}s (%{$percentPrefixWidth}s%{$percentWidth}.2f%%)".PHP_EOL, |
|
145 | - $value, |
|
146 | - number_format($count), |
|
147 | - $percentPrefix, |
|
148 | - $percent |
|
149 | - ); |
|
150 | - } |
|
151 | - |
|
152 | - echo "\t".str_repeat('-', ($valueWidth + $countWidth + 15)).PHP_EOL; |
|
153 | - printf( |
|
154 | - "\t%-{$valueWidth}s => %{$countWidth}s (100.00%%)".PHP_EOL, |
|
155 | - 'total', |
|
156 | - number_format($totalCount) |
|
157 | - ); |
|
158 | - }//end if |
|
159 | - |
|
160 | - echo PHP_EOL; |
|
161 | - }//end foreach |
|
162 | - |
|
163 | - echo str_repeat('-', 70).PHP_EOL; |
|
164 | - |
|
165 | - if ($toScreen === true && $interactive === false) { |
|
166 | - Timing::printRunTime(); |
|
167 | - } |
|
168 | - |
|
169 | - }//end generate() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + $metrics = $phpcsFile->getMetrics(); |
|
36 | + foreach ($metrics as $metric => $data) { |
|
37 | + foreach ($data['values'] as $value => $count) { |
|
38 | + echo "$metric>>$value>>$count".PHP_EOL; |
|
39 | + } |
|
40 | + } |
|
41 | + |
|
42 | + return true; |
|
43 | + |
|
44 | + }//end generateFileReport() |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * Prints the source of all errors and warnings. |
|
49 | + * |
|
50 | + * @param string $cachedData Any partial report data that was returned from |
|
51 | + * generateFileReport during the run. |
|
52 | + * @param int $totalFiles Total number of files processed during the run. |
|
53 | + * @param int $totalErrors Total number of errors found during the run. |
|
54 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
55 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
56 | + * @param bool $showSources Show sources? |
|
57 | + * @param int $width Maximum allowed line width. |
|
58 | + * @param bool $interactive Are we running in interactive mode? |
|
59 | + * @param bool $toScreen Is the report being printed to screen? |
|
60 | + * |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + public function generate( |
|
64 | + $cachedData, |
|
65 | + $totalFiles, |
|
66 | + $totalErrors, |
|
67 | + $totalWarnings, |
|
68 | + $totalFixable, |
|
69 | + $showSources=false, |
|
70 | + $width=80, |
|
71 | + $interactive=false, |
|
72 | + $toScreen=true |
|
73 | + ) { |
|
74 | + $lines = explode(PHP_EOL, $cachedData); |
|
75 | + array_pop($lines); |
|
76 | + |
|
77 | + if (empty($lines) === true) { |
|
78 | + return; |
|
79 | + } |
|
80 | + |
|
81 | + $metrics = []; |
|
82 | + foreach ($lines as $line) { |
|
83 | + $parts = explode('>>', $line); |
|
84 | + $metric = $parts[0]; |
|
85 | + $value = $parts[1]; |
|
86 | + $count = $parts[2]; |
|
87 | + if (isset($metrics[$metric]) === false) { |
|
88 | + $metrics[$metric] = []; |
|
89 | + } |
|
90 | + |
|
91 | + if (isset($metrics[$metric][$value]) === false) { |
|
92 | + $metrics[$metric][$value] = $count; |
|
93 | + } else { |
|
94 | + $metrics[$metric][$value] += $count; |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + ksort($metrics); |
|
99 | + |
|
100 | + echo PHP_EOL."\033[1m".'PHP CODE SNIFFER INFORMATION REPORT'."\033[0m".PHP_EOL; |
|
101 | + echo str_repeat('-', 70).PHP_EOL; |
|
102 | + |
|
103 | + foreach ($metrics as $metric => $values) { |
|
104 | + if (count($values) === 1) { |
|
105 | + $count = reset($values); |
|
106 | + $value = key($values); |
|
107 | + |
|
108 | + echo "$metric: \033[4m$value\033[0m [$count/$count, 100%]".PHP_EOL; |
|
109 | + } else { |
|
110 | + $totalCount = 0; |
|
111 | + $valueWidth = 0; |
|
112 | + foreach ($values as $value => $count) { |
|
113 | + $totalCount += $count; |
|
114 | + $valueWidth = max($valueWidth, strlen($value)); |
|
115 | + } |
|
116 | + |
|
117 | + // Length of the total string, plus however many |
|
118 | + // thousands separators there are. |
|
119 | + $countWidth = strlen($totalCount); |
|
120 | + $thousandSeparatorCount = floor($countWidth / 3); |
|
121 | + $countWidth += $thousandSeparatorCount; |
|
122 | + |
|
123 | + // Account for 'total' line. |
|
124 | + $valueWidth = max(5, $valueWidth); |
|
125 | + |
|
126 | + echo "$metric:".PHP_EOL; |
|
127 | + |
|
128 | + ksort($values, SORT_NATURAL); |
|
129 | + arsort($values); |
|
130 | + |
|
131 | + $percentPrefixWidth = 0; |
|
132 | + $percentWidth = 6; |
|
133 | + foreach ($values as $value => $count) { |
|
134 | + $percent = round(($count / $totalCount * 100), 2); |
|
135 | + $percentPrefix = ''; |
|
136 | + if ($percent === 0.00) { |
|
137 | + $percent = 0.01; |
|
138 | + $percentPrefix = '<'; |
|
139 | + $percentPrefixWidth = 2; |
|
140 | + $percentWidth = 4; |
|
141 | + } |
|
142 | + |
|
143 | + printf( |
|
144 | + "\t%-{$valueWidth}s => %{$countWidth}s (%{$percentPrefixWidth}s%{$percentWidth}.2f%%)".PHP_EOL, |
|
145 | + $value, |
|
146 | + number_format($count), |
|
147 | + $percentPrefix, |
|
148 | + $percent |
|
149 | + ); |
|
150 | + } |
|
151 | + |
|
152 | + echo "\t".str_repeat('-', ($valueWidth + $countWidth + 15)).PHP_EOL; |
|
153 | + printf( |
|
154 | + "\t%-{$valueWidth}s => %{$countWidth}s (100.00%%)".PHP_EOL, |
|
155 | + 'total', |
|
156 | + number_format($totalCount) |
|
157 | + ); |
|
158 | + }//end if |
|
159 | + |
|
160 | + echo PHP_EOL; |
|
161 | + }//end foreach |
|
162 | + |
|
163 | + echo str_repeat('-', 70).PHP_EOL; |
|
164 | + |
|
165 | + if ($toScreen === true && $interactive === false) { |
|
166 | + Timing::printRunTime(); |
|
167 | + } |
|
168 | + |
|
169 | + }//end generate() |
|
170 | 170 | |
171 | 171 | |
172 | 172 | }//end class |
@@ -16,116 +16,116 @@ |
||
16 | 16 | { |
17 | 17 | |
18 | 18 | |
19 | - /** |
|
20 | - * Generate a partial report for a single processed file. |
|
21 | - * |
|
22 | - * Function should return TRUE if it printed or stored data about the file |
|
23 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | - * its data should be counted in the grand totals. |
|
25 | - * |
|
26 | - * @param array $report Prepared report data. |
|
27 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | - * @param bool $showSources Show sources? |
|
29 | - * @param int $width Maximum allowed line width. |
|
30 | - * |
|
31 | - * @return bool |
|
32 | - */ |
|
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | - { |
|
35 | - $errors = $phpcsFile->getFixableCount(); |
|
36 | - if ($errors === 0) { |
|
37 | - return false; |
|
38 | - } |
|
39 | - |
|
40 | - $phpcsFile->disableCaching(); |
|
41 | - $tokens = $phpcsFile->getTokens(); |
|
42 | - if (empty($tokens) === true) { |
|
43 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
44 | - $startTime = microtime(true); |
|
45 | - echo 'DIFF report is parsing '.basename($report['filename']).' '; |
|
46 | - } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
47 | - echo 'DIFF report is forcing parse of '.$report['filename'].PHP_EOL; |
|
48 | - } |
|
49 | - |
|
50 | - $phpcsFile->parse(); |
|
51 | - |
|
52 | - if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
53 | - $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
54 | - if ($timeTaken < 1000) { |
|
55 | - $timeTaken = round($timeTaken); |
|
56 | - echo "DONE in {$timeTaken}ms"; |
|
57 | - } else { |
|
58 | - $timeTaken = round(($timeTaken / 1000), 2); |
|
59 | - echo "DONE in $timeTaken secs"; |
|
60 | - } |
|
61 | - |
|
62 | - echo PHP_EOL; |
|
63 | - } |
|
64 | - |
|
65 | - $phpcsFile->fixer->startFile($phpcsFile); |
|
66 | - }//end if |
|
67 | - |
|
68 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
69 | - ob_end_clean(); |
|
70 | - echo "\t*** START FILE FIXING ***".PHP_EOL; |
|
71 | - } |
|
72 | - |
|
73 | - $fixed = $phpcsFile->fixer->fixFile(); |
|
74 | - |
|
75 | - if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
76 | - echo "\t*** END FILE FIXING ***".PHP_EOL; |
|
77 | - ob_start(); |
|
78 | - } |
|
79 | - |
|
80 | - if ($fixed === false) { |
|
81 | - return false; |
|
82 | - } |
|
83 | - |
|
84 | - $diff = $phpcsFile->fixer->generateDiff(); |
|
85 | - if ($diff === '') { |
|
86 | - // Nothing to print. |
|
87 | - return false; |
|
88 | - } |
|
89 | - |
|
90 | - echo $diff.PHP_EOL; |
|
91 | - return true; |
|
92 | - |
|
93 | - }//end generateFileReport() |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * Prints all errors and warnings for each file processed. |
|
98 | - * |
|
99 | - * @param string $cachedData Any partial report data that was returned from |
|
100 | - * generateFileReport during the run. |
|
101 | - * @param int $totalFiles Total number of files processed during the run. |
|
102 | - * @param int $totalErrors Total number of errors found during the run. |
|
103 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
104 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
105 | - * @param bool $showSources Show sources? |
|
106 | - * @param int $width Maximum allowed line width. |
|
107 | - * @param bool $interactive Are we running in interactive mode? |
|
108 | - * @param bool $toScreen Is the report being printed to screen? |
|
109 | - * |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - public function generate( |
|
113 | - $cachedData, |
|
114 | - $totalFiles, |
|
115 | - $totalErrors, |
|
116 | - $totalWarnings, |
|
117 | - $totalFixable, |
|
118 | - $showSources=false, |
|
119 | - $width=80, |
|
120 | - $interactive=false, |
|
121 | - $toScreen=true |
|
122 | - ) { |
|
123 | - echo $cachedData; |
|
124 | - if ($toScreen === true && $cachedData !== '') { |
|
125 | - echo PHP_EOL; |
|
126 | - } |
|
127 | - |
|
128 | - }//end generate() |
|
19 | + /** |
|
20 | + * Generate a partial report for a single processed file. |
|
21 | + * |
|
22 | + * Function should return TRUE if it printed or stored data about the file |
|
23 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
24 | + * its data should be counted in the grand totals. |
|
25 | + * |
|
26 | + * @param array $report Prepared report data. |
|
27 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
28 | + * @param bool $showSources Show sources? |
|
29 | + * @param int $width Maximum allowed line width. |
|
30 | + * |
|
31 | + * @return bool |
|
32 | + */ |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
34 | + { |
|
35 | + $errors = $phpcsFile->getFixableCount(); |
|
36 | + if ($errors === 0) { |
|
37 | + return false; |
|
38 | + } |
|
39 | + |
|
40 | + $phpcsFile->disableCaching(); |
|
41 | + $tokens = $phpcsFile->getTokens(); |
|
42 | + if (empty($tokens) === true) { |
|
43 | + if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
44 | + $startTime = microtime(true); |
|
45 | + echo 'DIFF report is parsing '.basename($report['filename']).' '; |
|
46 | + } else if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
47 | + echo 'DIFF report is forcing parse of '.$report['filename'].PHP_EOL; |
|
48 | + } |
|
49 | + |
|
50 | + $phpcsFile->parse(); |
|
51 | + |
|
52 | + if (PHP_CODESNIFFER_VERBOSITY === 1) { |
|
53 | + $timeTaken = ((microtime(true) - $startTime) * 1000); |
|
54 | + if ($timeTaken < 1000) { |
|
55 | + $timeTaken = round($timeTaken); |
|
56 | + echo "DONE in {$timeTaken}ms"; |
|
57 | + } else { |
|
58 | + $timeTaken = round(($timeTaken / 1000), 2); |
|
59 | + echo "DONE in $timeTaken secs"; |
|
60 | + } |
|
61 | + |
|
62 | + echo PHP_EOL; |
|
63 | + } |
|
64 | + |
|
65 | + $phpcsFile->fixer->startFile($phpcsFile); |
|
66 | + }//end if |
|
67 | + |
|
68 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
69 | + ob_end_clean(); |
|
70 | + echo "\t*** START FILE FIXING ***".PHP_EOL; |
|
71 | + } |
|
72 | + |
|
73 | + $fixed = $phpcsFile->fixer->fixFile(); |
|
74 | + |
|
75 | + if (PHP_CODESNIFFER_VERBOSITY > 1) { |
|
76 | + echo "\t*** END FILE FIXING ***".PHP_EOL; |
|
77 | + ob_start(); |
|
78 | + } |
|
79 | + |
|
80 | + if ($fixed === false) { |
|
81 | + return false; |
|
82 | + } |
|
83 | + |
|
84 | + $diff = $phpcsFile->fixer->generateDiff(); |
|
85 | + if ($diff === '') { |
|
86 | + // Nothing to print. |
|
87 | + return false; |
|
88 | + } |
|
89 | + |
|
90 | + echo $diff.PHP_EOL; |
|
91 | + return true; |
|
92 | + |
|
93 | + }//end generateFileReport() |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * Prints all errors and warnings for each file processed. |
|
98 | + * |
|
99 | + * @param string $cachedData Any partial report data that was returned from |
|
100 | + * generateFileReport during the run. |
|
101 | + * @param int $totalFiles Total number of files processed during the run. |
|
102 | + * @param int $totalErrors Total number of errors found during the run. |
|
103 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
104 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
105 | + * @param bool $showSources Show sources? |
|
106 | + * @param int $width Maximum allowed line width. |
|
107 | + * @param bool $interactive Are we running in interactive mode? |
|
108 | + * @param bool $toScreen Is the report being printed to screen? |
|
109 | + * |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + public function generate( |
|
113 | + $cachedData, |
|
114 | + $totalFiles, |
|
115 | + $totalErrors, |
|
116 | + $totalWarnings, |
|
117 | + $totalFixable, |
|
118 | + $showSources=false, |
|
119 | + $width=80, |
|
120 | + $interactive=false, |
|
121 | + $toScreen=true |
|
122 | + ) { |
|
123 | + echo $cachedData; |
|
124 | + if ($toScreen === true && $cachedData !== '') { |
|
125 | + echo PHP_EOL; |
|
126 | + } |
|
127 | + |
|
128 | + }//end generate() |
|
129 | 129 | |
130 | 130 | |
131 | 131 | }//end class |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @return bool |
32 | 32 | */ |
33 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
33 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
34 | 34 | { |
35 | 35 | $errors = $phpcsFile->getFixableCount(); |
36 | 36 | if ($errors === 0) { |
@@ -115,10 +115,10 @@ discard block |
||
115 | 115 | $totalErrors, |
116 | 116 | $totalWarnings, |
117 | 117 | $totalFixable, |
118 | - $showSources=false, |
|
119 | - $width=80, |
|
120 | - $interactive=false, |
|
121 | - $toScreen=true |
|
118 | + $showSources = false, |
|
119 | + $width = 80, |
|
120 | + $interactive = false, |
|
121 | + $toScreen = true |
|
122 | 122 | ) { |
123 | 123 | echo $cachedData; |
124 | 124 | if ($toScreen === true && $cachedData !== '') { |
@@ -16,361 +16,361 @@ |
||
16 | 16 | abstract class VersionControl implements Report |
17 | 17 | { |
18 | 18 | |
19 | - /** |
|
20 | - * The name of the report we want in the output. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $reportName = 'VERSION CONTROL'; |
|
25 | - |
|
26 | - |
|
27 | - /** |
|
28 | - * Generate a partial report for a single processed file. |
|
29 | - * |
|
30 | - * Function should return TRUE if it printed or stored data about the file |
|
31 | - * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
32 | - * its data should be counted in the grand totals. |
|
33 | - * |
|
34 | - * @param array $report Prepared report data. |
|
35 | - * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
36 | - * @param bool $showSources Show sources? |
|
37 | - * @param int $width Maximum allowed line width. |
|
38 | - * |
|
39 | - * @return bool |
|
40 | - */ |
|
41 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
42 | - { |
|
43 | - $blames = $this->getBlameContent($report['filename']); |
|
44 | - |
|
45 | - $authorCache = []; |
|
46 | - $praiseCache = []; |
|
47 | - $sourceCache = []; |
|
48 | - |
|
49 | - foreach ($report['messages'] as $line => $lineErrors) { |
|
50 | - $author = 'Unknown'; |
|
51 | - if (isset($blames[($line - 1)]) === true) { |
|
52 | - $blameAuthor = $this->getAuthor($blames[($line - 1)]); |
|
53 | - if ($blameAuthor !== false) { |
|
54 | - $author = $blameAuthor; |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - if (isset($authorCache[$author]) === false) { |
|
59 | - $authorCache[$author] = 0; |
|
60 | - $praiseCache[$author] = [ |
|
61 | - 'good' => 0, |
|
62 | - 'bad' => 0, |
|
63 | - ]; |
|
64 | - } |
|
65 | - |
|
66 | - $praiseCache[$author]['bad']++; |
|
67 | - |
|
68 | - foreach ($lineErrors as $column => $colErrors) { |
|
69 | - foreach ($colErrors as $error) { |
|
70 | - $authorCache[$author]++; |
|
71 | - |
|
72 | - if ($showSources === true) { |
|
73 | - $source = $error['source']; |
|
74 | - if (isset($sourceCache[$author][$source]) === false) { |
|
75 | - $sourceCache[$author][$source] = [ |
|
76 | - 'count' => 1, |
|
77 | - 'fixable' => $error['fixable'], |
|
78 | - ]; |
|
79 | - } else { |
|
80 | - $sourceCache[$author][$source]['count']++; |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - unset($blames[($line - 1)]); |
|
87 | - }//end foreach |
|
88 | - |
|
89 | - // Now go through and give the authors some credit for |
|
90 | - // all the lines that do not have errors. |
|
91 | - foreach ($blames as $line) { |
|
92 | - $author = $this->getAuthor($line); |
|
93 | - if ($author === false) { |
|
94 | - $author = 'Unknown'; |
|
95 | - } |
|
96 | - |
|
97 | - if (isset($authorCache[$author]) === false) { |
|
98 | - // This author doesn't have any errors. |
|
99 | - if (PHP_CODESNIFFER_VERBOSITY === 0) { |
|
100 | - continue; |
|
101 | - } |
|
102 | - |
|
103 | - $authorCache[$author] = 0; |
|
104 | - $praiseCache[$author] = [ |
|
105 | - 'good' => 0, |
|
106 | - 'bad' => 0, |
|
107 | - ]; |
|
108 | - } |
|
109 | - |
|
110 | - $praiseCache[$author]['good']++; |
|
111 | - }//end foreach |
|
112 | - |
|
113 | - foreach ($authorCache as $author => $errors) { |
|
114 | - echo "AUTHOR>>$author>>$errors".PHP_EOL; |
|
115 | - } |
|
116 | - |
|
117 | - foreach ($praiseCache as $author => $praise) { |
|
118 | - echo "PRAISE>>$author>>".$praise['good'].'>>'.$praise['bad'].PHP_EOL; |
|
119 | - } |
|
120 | - |
|
121 | - foreach ($sourceCache as $author => $sources) { |
|
122 | - foreach ($sources as $source => $sourceData) { |
|
123 | - $count = $sourceData['count']; |
|
124 | - $fixable = (int) $sourceData['fixable']; |
|
125 | - echo "SOURCE>>$author>>$source>>$count>>$fixable".PHP_EOL; |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - return true; |
|
130 | - |
|
131 | - }//end generateFileReport() |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * Prints the author of all errors and warnings, as given by "version control blame". |
|
136 | - * |
|
137 | - * @param string $cachedData Any partial report data that was returned from |
|
138 | - * generateFileReport during the run. |
|
139 | - * @param int $totalFiles Total number of files processed during the run. |
|
140 | - * @param int $totalErrors Total number of errors found during the run. |
|
141 | - * @param int $totalWarnings Total number of warnings found during the run. |
|
142 | - * @param int $totalFixable Total number of problems that can be fixed. |
|
143 | - * @param bool $showSources Show sources? |
|
144 | - * @param int $width Maximum allowed line width. |
|
145 | - * @param bool $interactive Are we running in interactive mode? |
|
146 | - * @param bool $toScreen Is the report being printed to screen? |
|
147 | - * |
|
148 | - * @return void |
|
149 | - */ |
|
150 | - public function generate( |
|
151 | - $cachedData, |
|
152 | - $totalFiles, |
|
153 | - $totalErrors, |
|
154 | - $totalWarnings, |
|
155 | - $totalFixable, |
|
156 | - $showSources=false, |
|
157 | - $width=80, |
|
158 | - $interactive=false, |
|
159 | - $toScreen=true |
|
160 | - ) { |
|
161 | - $errorsShown = ($totalErrors + $totalWarnings); |
|
162 | - if ($errorsShown === 0) { |
|
163 | - // Nothing to show. |
|
164 | - return; |
|
165 | - } |
|
166 | - |
|
167 | - $lines = explode(PHP_EOL, $cachedData); |
|
168 | - array_pop($lines); |
|
169 | - |
|
170 | - if (empty($lines) === true) { |
|
171 | - return; |
|
172 | - } |
|
173 | - |
|
174 | - $authorCache = []; |
|
175 | - $praiseCache = []; |
|
176 | - $sourceCache = []; |
|
177 | - |
|
178 | - foreach ($lines as $line) { |
|
179 | - $parts = explode('>>', $line); |
|
180 | - switch ($parts[0]) { |
|
181 | - case 'AUTHOR': |
|
182 | - if (isset($authorCache[$parts[1]]) === false) { |
|
183 | - $authorCache[$parts[1]] = $parts[2]; |
|
184 | - } else { |
|
185 | - $authorCache[$parts[1]] += $parts[2]; |
|
186 | - } |
|
187 | - break; |
|
188 | - case 'PRAISE': |
|
189 | - if (isset($praiseCache[$parts[1]]) === false) { |
|
190 | - $praiseCache[$parts[1]] = [ |
|
191 | - 'good' => $parts[2], |
|
192 | - 'bad' => $parts[3], |
|
193 | - ]; |
|
194 | - } else { |
|
195 | - $praiseCache[$parts[1]]['good'] += $parts[2]; |
|
196 | - $praiseCache[$parts[1]]['bad'] += $parts[3]; |
|
197 | - } |
|
198 | - break; |
|
199 | - case 'SOURCE': |
|
200 | - if (isset($praiseCache[$parts[1]]) === false) { |
|
201 | - $praiseCache[$parts[1]] = []; |
|
202 | - } |
|
203 | - |
|
204 | - if (isset($sourceCache[$parts[1]][$parts[2]]) === false) { |
|
205 | - $sourceCache[$parts[1]][$parts[2]] = [ |
|
206 | - 'count' => $parts[3], |
|
207 | - 'fixable' => (bool) $parts[4], |
|
208 | - ]; |
|
209 | - } else { |
|
210 | - $sourceCache[$parts[1]][$parts[2]]['count'] += $parts[3]; |
|
211 | - } |
|
212 | - break; |
|
213 | - default: |
|
214 | - break; |
|
215 | - }//end switch |
|
216 | - }//end foreach |
|
217 | - |
|
218 | - // Make sure the report width isn't too big. |
|
219 | - $maxLength = 0; |
|
220 | - foreach ($authorCache as $author => $count) { |
|
221 | - $maxLength = max($maxLength, strlen($author)); |
|
222 | - if ($showSources === true && isset($sourceCache[$author]) === true) { |
|
223 | - foreach ($sourceCache[$author] as $source => $sourceData) { |
|
224 | - if ($source === 'count') { |
|
225 | - continue; |
|
226 | - } |
|
227 | - |
|
228 | - $maxLength = max($maxLength, (strlen($source) + 9)); |
|
229 | - } |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - $width = min($width, ($maxLength + 30)); |
|
234 | - $width = max($width, 70); |
|
235 | - arsort($authorCache); |
|
236 | - |
|
237 | - echo PHP_EOL."\033[1m".'PHP CODE SNIFFER '.$this->reportName.' BLAME SUMMARY'."\033[0m".PHP_EOL; |
|
238 | - echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
|
239 | - if ($showSources === true) { |
|
240 | - echo 'AUTHOR SOURCE'.str_repeat(' ', ($width - 43)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
|
241 | - echo str_repeat('-', $width).PHP_EOL; |
|
242 | - } else { |
|
243 | - echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
|
244 | - echo str_repeat('-', $width).PHP_EOL; |
|
245 | - } |
|
246 | - |
|
247 | - echo "\033[0m"; |
|
248 | - |
|
249 | - if ($showSources === true) { |
|
250 | - $maxSniffWidth = ($width - 15); |
|
251 | - |
|
252 | - if ($totalFixable > 0) { |
|
253 | - $maxSniffWidth -= 4; |
|
254 | - } |
|
255 | - } |
|
256 | - |
|
257 | - $fixableSources = 0; |
|
258 | - |
|
259 | - foreach ($authorCache as $author => $count) { |
|
260 | - if ($praiseCache[$author]['good'] === 0) { |
|
261 | - $percent = 0; |
|
262 | - } else { |
|
263 | - $total = ($praiseCache[$author]['bad'] + $praiseCache[$author]['good']); |
|
264 | - $percent = round(($praiseCache[$author]['bad'] / $total * 100), 2); |
|
265 | - } |
|
266 | - |
|
267 | - $overallPercent = '('.round((($count / $errorsShown) * 100), 2).')'; |
|
268 | - $authorPercent = '('.$percent.')'; |
|
269 | - $line = str_repeat(' ', (6 - strlen($count))).$count; |
|
270 | - $line = str_repeat(' ', (12 - strlen($overallPercent))).$overallPercent.$line; |
|
271 | - $line = str_repeat(' ', (11 - strlen($authorPercent))).$authorPercent.$line; |
|
272 | - $line = $author.str_repeat(' ', ($width - strlen($author) - strlen($line))).$line; |
|
273 | - |
|
274 | - if ($showSources === true) { |
|
275 | - $line = "\033[1m$line\033[0m"; |
|
276 | - } |
|
277 | - |
|
278 | - echo $line.PHP_EOL; |
|
279 | - |
|
280 | - if ($showSources === true && isset($sourceCache[$author]) === true) { |
|
281 | - $errors = $sourceCache[$author]; |
|
282 | - asort($errors); |
|
283 | - $errors = array_reverse($errors); |
|
284 | - |
|
285 | - foreach ($errors as $source => $sourceData) { |
|
286 | - if ($source === 'count') { |
|
287 | - continue; |
|
288 | - } |
|
289 | - |
|
290 | - $count = $sourceData['count']; |
|
291 | - |
|
292 | - $srcLength = strlen($source); |
|
293 | - if ($srcLength > $maxSniffWidth) { |
|
294 | - $source = substr($source, 0, $maxSniffWidth); |
|
295 | - } |
|
296 | - |
|
297 | - $line = str_repeat(' ', (5 - strlen($count))).$count; |
|
298 | - |
|
299 | - echo ' '; |
|
300 | - if ($totalFixable > 0) { |
|
301 | - echo '['; |
|
302 | - if ($sourceData['fixable'] === true) { |
|
303 | - echo 'x'; |
|
304 | - $fixableSources++; |
|
305 | - } else { |
|
306 | - echo ' '; |
|
307 | - } |
|
308 | - |
|
309 | - echo '] '; |
|
310 | - } |
|
311 | - |
|
312 | - echo $source; |
|
313 | - if ($totalFixable > 0) { |
|
314 | - echo str_repeat(' ', ($width - 18 - strlen($source))); |
|
315 | - } else { |
|
316 | - echo str_repeat(' ', ($width - 14 - strlen($source))); |
|
317 | - } |
|
318 | - |
|
319 | - echo $line.PHP_EOL; |
|
320 | - }//end foreach |
|
321 | - }//end if |
|
322 | - }//end foreach |
|
323 | - |
|
324 | - echo str_repeat('-', $width).PHP_EOL; |
|
325 | - echo "\033[1m".'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION'; |
|
326 | - if ($errorsShown !== 1) { |
|
327 | - echo 'S'; |
|
328 | - } |
|
329 | - |
|
330 | - echo ' WERE COMMITTED BY '.count($authorCache).' AUTHOR'; |
|
331 | - if (count($authorCache) !== 1) { |
|
332 | - echo 'S'; |
|
333 | - } |
|
334 | - |
|
335 | - echo "\033[0m"; |
|
336 | - |
|
337 | - if ($totalFixable > 0) { |
|
338 | - if ($showSources === true) { |
|
339 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
340 | - echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
|
341 | - } else { |
|
342 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
343 | - echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m"; |
|
344 | - } |
|
345 | - } |
|
346 | - |
|
347 | - echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
348 | - |
|
349 | - if ($toScreen === true && $interactive === false) { |
|
350 | - Timing::printRunTime(); |
|
351 | - } |
|
352 | - |
|
353 | - }//end generate() |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * Extract the author from a blame line. |
|
358 | - * |
|
359 | - * @param string $line Line to parse. |
|
360 | - * |
|
361 | - * @return mixed string or false if impossible to recover. |
|
362 | - */ |
|
363 | - abstract protected function getAuthor($line); |
|
364 | - |
|
365 | - |
|
366 | - /** |
|
367 | - * Gets the blame output. |
|
368 | - * |
|
369 | - * @param string $filename File to blame. |
|
370 | - * |
|
371 | - * @return array |
|
372 | - */ |
|
373 | - abstract protected function getBlameContent($filename); |
|
19 | + /** |
|
20 | + * The name of the report we want in the output. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $reportName = 'VERSION CONTROL'; |
|
25 | + |
|
26 | + |
|
27 | + /** |
|
28 | + * Generate a partial report for a single processed file. |
|
29 | + * |
|
30 | + * Function should return TRUE if it printed or stored data about the file |
|
31 | + * and FALSE if it ignored the file. Returning TRUE indicates that the file and |
|
32 | + * its data should be counted in the grand totals. |
|
33 | + * |
|
34 | + * @param array $report Prepared report data. |
|
35 | + * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. |
|
36 | + * @param bool $showSources Show sources? |
|
37 | + * @param int $width Maximum allowed line width. |
|
38 | + * |
|
39 | + * @return bool |
|
40 | + */ |
|
41 | + public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
42 | + { |
|
43 | + $blames = $this->getBlameContent($report['filename']); |
|
44 | + |
|
45 | + $authorCache = []; |
|
46 | + $praiseCache = []; |
|
47 | + $sourceCache = []; |
|
48 | + |
|
49 | + foreach ($report['messages'] as $line => $lineErrors) { |
|
50 | + $author = 'Unknown'; |
|
51 | + if (isset($blames[($line - 1)]) === true) { |
|
52 | + $blameAuthor = $this->getAuthor($blames[($line - 1)]); |
|
53 | + if ($blameAuthor !== false) { |
|
54 | + $author = $blameAuthor; |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + if (isset($authorCache[$author]) === false) { |
|
59 | + $authorCache[$author] = 0; |
|
60 | + $praiseCache[$author] = [ |
|
61 | + 'good' => 0, |
|
62 | + 'bad' => 0, |
|
63 | + ]; |
|
64 | + } |
|
65 | + |
|
66 | + $praiseCache[$author]['bad']++; |
|
67 | + |
|
68 | + foreach ($lineErrors as $column => $colErrors) { |
|
69 | + foreach ($colErrors as $error) { |
|
70 | + $authorCache[$author]++; |
|
71 | + |
|
72 | + if ($showSources === true) { |
|
73 | + $source = $error['source']; |
|
74 | + if (isset($sourceCache[$author][$source]) === false) { |
|
75 | + $sourceCache[$author][$source] = [ |
|
76 | + 'count' => 1, |
|
77 | + 'fixable' => $error['fixable'], |
|
78 | + ]; |
|
79 | + } else { |
|
80 | + $sourceCache[$author][$source]['count']++; |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + unset($blames[($line - 1)]); |
|
87 | + }//end foreach |
|
88 | + |
|
89 | + // Now go through and give the authors some credit for |
|
90 | + // all the lines that do not have errors. |
|
91 | + foreach ($blames as $line) { |
|
92 | + $author = $this->getAuthor($line); |
|
93 | + if ($author === false) { |
|
94 | + $author = 'Unknown'; |
|
95 | + } |
|
96 | + |
|
97 | + if (isset($authorCache[$author]) === false) { |
|
98 | + // This author doesn't have any errors. |
|
99 | + if (PHP_CODESNIFFER_VERBOSITY === 0) { |
|
100 | + continue; |
|
101 | + } |
|
102 | + |
|
103 | + $authorCache[$author] = 0; |
|
104 | + $praiseCache[$author] = [ |
|
105 | + 'good' => 0, |
|
106 | + 'bad' => 0, |
|
107 | + ]; |
|
108 | + } |
|
109 | + |
|
110 | + $praiseCache[$author]['good']++; |
|
111 | + }//end foreach |
|
112 | + |
|
113 | + foreach ($authorCache as $author => $errors) { |
|
114 | + echo "AUTHOR>>$author>>$errors".PHP_EOL; |
|
115 | + } |
|
116 | + |
|
117 | + foreach ($praiseCache as $author => $praise) { |
|
118 | + echo "PRAISE>>$author>>".$praise['good'].'>>'.$praise['bad'].PHP_EOL; |
|
119 | + } |
|
120 | + |
|
121 | + foreach ($sourceCache as $author => $sources) { |
|
122 | + foreach ($sources as $source => $sourceData) { |
|
123 | + $count = $sourceData['count']; |
|
124 | + $fixable = (int) $sourceData['fixable']; |
|
125 | + echo "SOURCE>>$author>>$source>>$count>>$fixable".PHP_EOL; |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + return true; |
|
130 | + |
|
131 | + }//end generateFileReport() |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * Prints the author of all errors and warnings, as given by "version control blame". |
|
136 | + * |
|
137 | + * @param string $cachedData Any partial report data that was returned from |
|
138 | + * generateFileReport during the run. |
|
139 | + * @param int $totalFiles Total number of files processed during the run. |
|
140 | + * @param int $totalErrors Total number of errors found during the run. |
|
141 | + * @param int $totalWarnings Total number of warnings found during the run. |
|
142 | + * @param int $totalFixable Total number of problems that can be fixed. |
|
143 | + * @param bool $showSources Show sources? |
|
144 | + * @param int $width Maximum allowed line width. |
|
145 | + * @param bool $interactive Are we running in interactive mode? |
|
146 | + * @param bool $toScreen Is the report being printed to screen? |
|
147 | + * |
|
148 | + * @return void |
|
149 | + */ |
|
150 | + public function generate( |
|
151 | + $cachedData, |
|
152 | + $totalFiles, |
|
153 | + $totalErrors, |
|
154 | + $totalWarnings, |
|
155 | + $totalFixable, |
|
156 | + $showSources=false, |
|
157 | + $width=80, |
|
158 | + $interactive=false, |
|
159 | + $toScreen=true |
|
160 | + ) { |
|
161 | + $errorsShown = ($totalErrors + $totalWarnings); |
|
162 | + if ($errorsShown === 0) { |
|
163 | + // Nothing to show. |
|
164 | + return; |
|
165 | + } |
|
166 | + |
|
167 | + $lines = explode(PHP_EOL, $cachedData); |
|
168 | + array_pop($lines); |
|
169 | + |
|
170 | + if (empty($lines) === true) { |
|
171 | + return; |
|
172 | + } |
|
173 | + |
|
174 | + $authorCache = []; |
|
175 | + $praiseCache = []; |
|
176 | + $sourceCache = []; |
|
177 | + |
|
178 | + foreach ($lines as $line) { |
|
179 | + $parts = explode('>>', $line); |
|
180 | + switch ($parts[0]) { |
|
181 | + case 'AUTHOR': |
|
182 | + if (isset($authorCache[$parts[1]]) === false) { |
|
183 | + $authorCache[$parts[1]] = $parts[2]; |
|
184 | + } else { |
|
185 | + $authorCache[$parts[1]] += $parts[2]; |
|
186 | + } |
|
187 | + break; |
|
188 | + case 'PRAISE': |
|
189 | + if (isset($praiseCache[$parts[1]]) === false) { |
|
190 | + $praiseCache[$parts[1]] = [ |
|
191 | + 'good' => $parts[2], |
|
192 | + 'bad' => $parts[3], |
|
193 | + ]; |
|
194 | + } else { |
|
195 | + $praiseCache[$parts[1]]['good'] += $parts[2]; |
|
196 | + $praiseCache[$parts[1]]['bad'] += $parts[3]; |
|
197 | + } |
|
198 | + break; |
|
199 | + case 'SOURCE': |
|
200 | + if (isset($praiseCache[$parts[1]]) === false) { |
|
201 | + $praiseCache[$parts[1]] = []; |
|
202 | + } |
|
203 | + |
|
204 | + if (isset($sourceCache[$parts[1]][$parts[2]]) === false) { |
|
205 | + $sourceCache[$parts[1]][$parts[2]] = [ |
|
206 | + 'count' => $parts[3], |
|
207 | + 'fixable' => (bool) $parts[4], |
|
208 | + ]; |
|
209 | + } else { |
|
210 | + $sourceCache[$parts[1]][$parts[2]]['count'] += $parts[3]; |
|
211 | + } |
|
212 | + break; |
|
213 | + default: |
|
214 | + break; |
|
215 | + }//end switch |
|
216 | + }//end foreach |
|
217 | + |
|
218 | + // Make sure the report width isn't too big. |
|
219 | + $maxLength = 0; |
|
220 | + foreach ($authorCache as $author => $count) { |
|
221 | + $maxLength = max($maxLength, strlen($author)); |
|
222 | + if ($showSources === true && isset($sourceCache[$author]) === true) { |
|
223 | + foreach ($sourceCache[$author] as $source => $sourceData) { |
|
224 | + if ($source === 'count') { |
|
225 | + continue; |
|
226 | + } |
|
227 | + |
|
228 | + $maxLength = max($maxLength, (strlen($source) + 9)); |
|
229 | + } |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + $width = min($width, ($maxLength + 30)); |
|
234 | + $width = max($width, 70); |
|
235 | + arsort($authorCache); |
|
236 | + |
|
237 | + echo PHP_EOL."\033[1m".'PHP CODE SNIFFER '.$this->reportName.' BLAME SUMMARY'."\033[0m".PHP_EOL; |
|
238 | + echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
|
239 | + if ($showSources === true) { |
|
240 | + echo 'AUTHOR SOURCE'.str_repeat(' ', ($width - 43)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
|
241 | + echo str_repeat('-', $width).PHP_EOL; |
|
242 | + } else { |
|
243 | + echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
|
244 | + echo str_repeat('-', $width).PHP_EOL; |
|
245 | + } |
|
246 | + |
|
247 | + echo "\033[0m"; |
|
248 | + |
|
249 | + if ($showSources === true) { |
|
250 | + $maxSniffWidth = ($width - 15); |
|
251 | + |
|
252 | + if ($totalFixable > 0) { |
|
253 | + $maxSniffWidth -= 4; |
|
254 | + } |
|
255 | + } |
|
256 | + |
|
257 | + $fixableSources = 0; |
|
258 | + |
|
259 | + foreach ($authorCache as $author => $count) { |
|
260 | + if ($praiseCache[$author]['good'] === 0) { |
|
261 | + $percent = 0; |
|
262 | + } else { |
|
263 | + $total = ($praiseCache[$author]['bad'] + $praiseCache[$author]['good']); |
|
264 | + $percent = round(($praiseCache[$author]['bad'] / $total * 100), 2); |
|
265 | + } |
|
266 | + |
|
267 | + $overallPercent = '('.round((($count / $errorsShown) * 100), 2).')'; |
|
268 | + $authorPercent = '('.$percent.')'; |
|
269 | + $line = str_repeat(' ', (6 - strlen($count))).$count; |
|
270 | + $line = str_repeat(' ', (12 - strlen($overallPercent))).$overallPercent.$line; |
|
271 | + $line = str_repeat(' ', (11 - strlen($authorPercent))).$authorPercent.$line; |
|
272 | + $line = $author.str_repeat(' ', ($width - strlen($author) - strlen($line))).$line; |
|
273 | + |
|
274 | + if ($showSources === true) { |
|
275 | + $line = "\033[1m$line\033[0m"; |
|
276 | + } |
|
277 | + |
|
278 | + echo $line.PHP_EOL; |
|
279 | + |
|
280 | + if ($showSources === true && isset($sourceCache[$author]) === true) { |
|
281 | + $errors = $sourceCache[$author]; |
|
282 | + asort($errors); |
|
283 | + $errors = array_reverse($errors); |
|
284 | + |
|
285 | + foreach ($errors as $source => $sourceData) { |
|
286 | + if ($source === 'count') { |
|
287 | + continue; |
|
288 | + } |
|
289 | + |
|
290 | + $count = $sourceData['count']; |
|
291 | + |
|
292 | + $srcLength = strlen($source); |
|
293 | + if ($srcLength > $maxSniffWidth) { |
|
294 | + $source = substr($source, 0, $maxSniffWidth); |
|
295 | + } |
|
296 | + |
|
297 | + $line = str_repeat(' ', (5 - strlen($count))).$count; |
|
298 | + |
|
299 | + echo ' '; |
|
300 | + if ($totalFixable > 0) { |
|
301 | + echo '['; |
|
302 | + if ($sourceData['fixable'] === true) { |
|
303 | + echo 'x'; |
|
304 | + $fixableSources++; |
|
305 | + } else { |
|
306 | + echo ' '; |
|
307 | + } |
|
308 | + |
|
309 | + echo '] '; |
|
310 | + } |
|
311 | + |
|
312 | + echo $source; |
|
313 | + if ($totalFixable > 0) { |
|
314 | + echo str_repeat(' ', ($width - 18 - strlen($source))); |
|
315 | + } else { |
|
316 | + echo str_repeat(' ', ($width - 14 - strlen($source))); |
|
317 | + } |
|
318 | + |
|
319 | + echo $line.PHP_EOL; |
|
320 | + }//end foreach |
|
321 | + }//end if |
|
322 | + }//end foreach |
|
323 | + |
|
324 | + echo str_repeat('-', $width).PHP_EOL; |
|
325 | + echo "\033[1m".'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION'; |
|
326 | + if ($errorsShown !== 1) { |
|
327 | + echo 'S'; |
|
328 | + } |
|
329 | + |
|
330 | + echo ' WERE COMMITTED BY '.count($authorCache).' AUTHOR'; |
|
331 | + if (count($authorCache) !== 1) { |
|
332 | + echo 'S'; |
|
333 | + } |
|
334 | + |
|
335 | + echo "\033[0m"; |
|
336 | + |
|
337 | + if ($totalFixable > 0) { |
|
338 | + if ($showSources === true) { |
|
339 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
340 | + echo "\033[1mPHPCBF CAN FIX THE $fixableSources MARKED SOURCES AUTOMATICALLY ($totalFixable VIOLATIONS IN TOTAL)\033[0m"; |
|
341 | + } else { |
|
342 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
|
343 | + echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m"; |
|
344 | + } |
|
345 | + } |
|
346 | + |
|
347 | + echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
|
348 | + |
|
349 | + if ($toScreen === true && $interactive === false) { |
|
350 | + Timing::printRunTime(); |
|
351 | + } |
|
352 | + |
|
353 | + }//end generate() |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * Extract the author from a blame line. |
|
358 | + * |
|
359 | + * @param string $line Line to parse. |
|
360 | + * |
|
361 | + * @return mixed string or false if impossible to recover. |
|
362 | + */ |
|
363 | + abstract protected function getAuthor($line); |
|
364 | + |
|
365 | + |
|
366 | + /** |
|
367 | + * Gets the blame output. |
|
368 | + * |
|
369 | + * @param string $filename File to blame. |
|
370 | + * |
|
371 | + * @return array |
|
372 | + */ |
|
373 | + abstract protected function getBlameContent($filename); |
|
374 | 374 | |
375 | 375 | |
376 | 376 | }//end class |
@@ -178,40 +178,40 @@ |
||
178 | 178 | foreach ($lines as $line) { |
179 | 179 | $parts = explode('>>', $line); |
180 | 180 | switch ($parts[0]) { |
181 | - case 'AUTHOR': |
|
182 | - if (isset($authorCache[$parts[1]]) === false) { |
|
183 | - $authorCache[$parts[1]] = $parts[2]; |
|
184 | - } else { |
|
185 | - $authorCache[$parts[1]] += $parts[2]; |
|
186 | - } |
|
187 | - break; |
|
188 | - case 'PRAISE': |
|
189 | - if (isset($praiseCache[$parts[1]]) === false) { |
|
190 | - $praiseCache[$parts[1]] = [ |
|
191 | - 'good' => $parts[2], |
|
192 | - 'bad' => $parts[3], |
|
193 | - ]; |
|
194 | - } else { |
|
195 | - $praiseCache[$parts[1]]['good'] += $parts[2]; |
|
196 | - $praiseCache[$parts[1]]['bad'] += $parts[3]; |
|
197 | - } |
|
198 | - break; |
|
199 | - case 'SOURCE': |
|
200 | - if (isset($praiseCache[$parts[1]]) === false) { |
|
201 | - $praiseCache[$parts[1]] = []; |
|
202 | - } |
|
203 | - |
|
204 | - if (isset($sourceCache[$parts[1]][$parts[2]]) === false) { |
|
205 | - $sourceCache[$parts[1]][$parts[2]] = [ |
|
206 | - 'count' => $parts[3], |
|
207 | - 'fixable' => (bool) $parts[4], |
|
208 | - ]; |
|
209 | - } else { |
|
210 | - $sourceCache[$parts[1]][$parts[2]]['count'] += $parts[3]; |
|
211 | - } |
|
212 | - break; |
|
213 | - default: |
|
214 | - break; |
|
181 | + case 'AUTHOR': |
|
182 | + if (isset($authorCache[$parts[1]]) === false) { |
|
183 | + $authorCache[$parts[1]] = $parts[2]; |
|
184 | + } else { |
|
185 | + $authorCache[$parts[1]] += $parts[2]; |
|
186 | + } |
|
187 | + break; |
|
188 | + case 'PRAISE': |
|
189 | + if (isset($praiseCache[$parts[1]]) === false) { |
|
190 | + $praiseCache[$parts[1]] = [ |
|
191 | + 'good' => $parts[2], |
|
192 | + 'bad' => $parts[3], |
|
193 | + ]; |
|
194 | + } else { |
|
195 | + $praiseCache[$parts[1]]['good'] += $parts[2]; |
|
196 | + $praiseCache[$parts[1]]['bad'] += $parts[3]; |
|
197 | + } |
|
198 | + break; |
|
199 | + case 'SOURCE': |
|
200 | + if (isset($praiseCache[$parts[1]]) === false) { |
|
201 | + $praiseCache[$parts[1]] = []; |
|
202 | + } |
|
203 | + |
|
204 | + if (isset($sourceCache[$parts[1]][$parts[2]]) === false) { |
|
205 | + $sourceCache[$parts[1]][$parts[2]] = [ |
|
206 | + 'count' => $parts[3], |
|
207 | + 'fixable' => (bool) $parts[4], |
|
208 | + ]; |
|
209 | + } else { |
|
210 | + $sourceCache[$parts[1]][$parts[2]]['count'] += $parts[3]; |
|
211 | + } |
|
212 | + break; |
|
213 | + default: |
|
214 | + break; |
|
215 | 215 | }//end switch |
216 | 216 | }//end foreach |
217 | 217 |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | * |
39 | 39 | * @return bool |
40 | 40 | */ |
41 | - public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
|
41 | + public function generateFileReport($report, File $phpcsFile, $showSources = false, $width = 80) |
|
42 | 42 | { |
43 | 43 | $blames = $this->getBlameContent($report['filename']); |
44 | 44 | |
@@ -153,10 +153,10 @@ discard block |
||
153 | 153 | $totalErrors, |
154 | 154 | $totalWarnings, |
155 | 155 | $totalFixable, |
156 | - $showSources=false, |
|
157 | - $width=80, |
|
158 | - $interactive=false, |
|
159 | - $toScreen=true |
|
156 | + $showSources = false, |
|
157 | + $width = 80, |
|
158 | + $interactive = false, |
|
159 | + $toScreen = true |
|
160 | 160 | ) { |
161 | 161 | $errorsShown = ($totalErrors + $totalWarnings); |
162 | 162 | if ($errorsShown === 0) { |