comparison vendor/squizlabs/php_codesniffer/src/Reports/Info.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 * Info 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 Info 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 $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 $countWidth = strlen($totalCount);
118 $nrOfThousandSeps = floor($countWidth / 3);
119 $countWidth += $nrOfThousandSeps;
120
121 // Account for 'total' line.
122 $valueWidth = max(5, $valueWidth);
123
124 echo "$metric:".PHP_EOL;
125
126 ksort($values, SORT_NATURAL);
127 arsort($values);
128
129 $percentPrefixWidth = 0;
130 $percentWidth = 6;
131 foreach ($values as $value => $count) {
132 $percent = round(($count / $totalCount * 100), 2);
133 $percentPrefix = '';
134 if ($percent === 0.00) {
135 $percent = 0.01;
136 $percentPrefix = '<';
137 $percentPrefixWidth = 2;
138 $percentWidth = 4;
139 }
140
141 printf(
142 "\t%-{$valueWidth}s => %{$countWidth}s (%{$percentPrefixWidth}s%{$percentWidth}.2f%%)".PHP_EOL,
143 $value,
144 number_format($count),
145 $percentPrefix,
146 $percent
147 );
148 }
149
150 echo "\t".str_repeat('-', ($valueWidth + $countWidth + 15)).PHP_EOL;
151 printf(
152 "\t%-{$valueWidth}s => %{$countWidth}s (100.00%%)".PHP_EOL,
153 'total',
154 number_format($totalCount)
155 );
156 }//end if
157
158 echo PHP_EOL;
159 }//end foreach
160
161 echo str_repeat('-', 70).PHP_EOL;
162
163 if ($toScreen === true && $interactive === false) {
164 Timing::printRunTime();
165 }
166
167 }//end generate()
168
169
170 }//end class