comparison vendor/squizlabs/php_codesniffer/src/Reports/Source.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php
2 /**
3 * Source report for PHP_CodeSniffer.
4 *
5 * @author Greg Sherwood <gsherwood@squiz.net>
6 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8 */
9
10 namespace PHP_CodeSniffer\Reports;
11
12 use PHP_CodeSniffer\Files\File;
13 use PHP_CodeSniffer\Util\Timing;
14
15 class Source implements Report
16 {
17
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()
334
335
336 }//end class