comparison vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
1 <?php
2 /**
3 * CBF report for PHP_CodeSniffer.
4 *
5 * This report implements the various auto-fixing features of the
6 * PHPCBF script and is not intended (or allowed) to be selected as a
7 * report from the command line.
8 *
9 * @author Greg Sherwood <gsherwood@squiz.net>
10 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
11 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12 */
13
14 namespace PHP_CodeSniffer\Reports;
15
16 use PHP_CodeSniffer\Exceptions\DeepExitException;
17 use PHP_CodeSniffer\Files\File;
18 use PHP_CodeSniffer\Util;
19
20 class Cbf implements Report
21 {
22
23
24 /**
25 * Generate a partial report for a single processed file.
26 *
27 * Function should return TRUE if it printed or stored data about the file
28 * and FALSE if it ignored the file. Returning TRUE indicates that the file and
29 * its data should be counted in the grand totals.
30 *
31 * @param array $report Prepared report data.
32 * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on.
33 * @param bool $showSources Show sources?
34 * @param int $width Maximum allowed line width.
35 *
36 * @return bool
37 */
38 public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
39 {
40 $errors = $phpcsFile->getFixableCount();
41 if ($errors !== 0) {
42 if (PHP_CODESNIFFER_VERBOSITY > 0) {
43 ob_end_clean();
44 $startTime = microtime(true);
45 echo "\t=> Fixing file: $errors/$errors violations remaining";
46 }
47
48 $fixed = $phpcsFile->fixer->fixFile();
49 }
50
51 if ($phpcsFile->config->stdin === true) {
52 // Replacing STDIN, so output current file to STDOUT
53 // even if nothing was fixed. Exit here because we
54 // can't process any more than 1 file in this setup.
55 $fixedContent = $phpcsFile->fixer->getContents();
56 throw new DeepExitException($fixedContent, 1);
57 }
58
59 if ($errors === 0) {
60 return false;
61 }
62
63 if (PHP_CODESNIFFER_VERBOSITY > 0) {
64 if ($fixed === false) {
65 echo 'ERROR';
66 } else {
67 echo 'DONE';
68 }
69
70 $timeTaken = ((microtime(true) - $startTime) * 1000);
71 if ($timeTaken < 1000) {
72 $timeTaken = round($timeTaken);
73 echo " in {$timeTaken}ms".PHP_EOL;
74 } else {
75 $timeTaken = round(($timeTaken / 1000), 2);
76 echo " in $timeTaken secs".PHP_EOL;
77 }
78 }
79
80 if ($fixed === true) {
81 // The filename in the report may be truncated due to a basepath setting
82 // but we are using it for writing here and not display,
83 // so find the correct path if basepath is in use.
84 $newFilename = $report['filename'].$phpcsFile->config->suffix;
85 if ($phpcsFile->config->basepath !== null) {
86 $newFilename = $phpcsFile->config->basepath.DIRECTORY_SEPARATOR.$newFilename;
87 }
88
89 $newContent = $phpcsFile->fixer->getContents();
90 file_put_contents($newFilename, $newContent);
91
92 if (PHP_CODESNIFFER_VERBOSITY > 0) {
93 if ($newFilename === $report['filename']) {
94 echo "\t=> File was overwritten".PHP_EOL;
95 } else {
96 echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL;
97 }
98 }
99 }
100
101 if (PHP_CODESNIFFER_VERBOSITY > 0) {
102 ob_start();
103 }
104
105 $errorCount = $phpcsFile->getErrorCount();
106 $warningCount = $phpcsFile->getWarningCount();
107 $fixableCount = $phpcsFile->getFixableCount();
108 $fixedCount = ($errors - $fixableCount);
109 echo $report['filename'].">>$errorCount>>$warningCount>>$fixableCount>>$fixedCount".PHP_EOL;
110
111 return $fixed;
112
113 }//end generateFileReport()
114
115
116 /**
117 * Prints a summary of fixed files.
118 *
119 * @param string $cachedData Any partial report data that was returned from
120 * generateFileReport during the run.
121 * @param int $totalFiles Total number of files processed during the run.
122 * @param int $totalErrors Total number of errors found during the run.
123 * @param int $totalWarnings Total number of warnings found during the run.
124 * @param int $totalFixable Total number of problems that can be fixed.
125 * @param bool $showSources Show sources?
126 * @param int $width Maximum allowed line width.
127 * @param bool $interactive Are we running in interactive mode?
128 * @param bool $toScreen Is the report being printed to screen?
129 *
130 * @return void
131 */
132 public function generate(
133 $cachedData,
134 $totalFiles,
135 $totalErrors,
136 $totalWarnings,
137 $totalFixable,
138 $showSources=false,
139 $width=80,
140 $interactive=false,
141 $toScreen=true
142 ) {
143 $lines = explode(PHP_EOL, $cachedData);
144 array_pop($lines);
145
146 if (empty($lines) === true) {
147 echo PHP_EOL.'No fixable errors were found'.PHP_EOL;
148 return;
149 }
150
151 $reportFiles = [];
152 $maxLength = 0;
153 $totalFixed = 0;
154 $failures = 0;
155
156 foreach ($lines as $line) {
157 $parts = explode('>>', $line);
158 $fileLen = strlen($parts[0]);
159 $reportFiles[$parts[0]] = [
160 'errors' => $parts[1],
161 'warnings' => $parts[2],
162 'fixable' => $parts[3],
163 'fixed' => $parts[4],
164 'strlen' => $fileLen,
165 ];
166
167 $maxLength = max($maxLength, $fileLen);
168
169 $totalFixed += $parts[4];
170
171 if ($parts[3] > 0) {
172 $failures++;
173 }
174 }
175
176 $width = min($width, ($maxLength + 21));
177 $width = max($width, 70);
178
179 echo PHP_EOL."\033[1m".'PHPCBF RESULT SUMMARY'."\033[0m".PHP_EOL;
180 echo str_repeat('-', $width).PHP_EOL;
181 echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'FIXED REMAINING'."\033[0m".PHP_EOL;
182 echo str_repeat('-', $width).PHP_EOL;
183
184 foreach ($reportFiles as $file => $data) {
185 $padding = ($width - 18 - $data['strlen']);
186 if ($padding < 0) {
187 $file = '...'.substr($file, (($padding * -1) + 3));
188 $padding = 0;
189 }
190
191 echo $file.str_repeat(' ', $padding).' ';
192
193 if ($data['fixable'] > 0) {
194 echo "\033[31mFAILED TO FIX\033[0m".PHP_EOL;
195 continue;
196 }
197
198 $remaining = ($data['errors'] + $data['warnings']);
199
200 if ($data['fixed'] !== 0) {
201 echo $data['fixed'];
202 echo str_repeat(' ', (7 - strlen((string) $data['fixed'])));
203 } else {
204 echo '0 ';
205 }
206
207 if ($remaining !== 0) {
208 echo $remaining;
209 } else {
210 echo '0';
211 }
212
213 echo PHP_EOL;
214 }//end foreach
215
216 echo str_repeat('-', $width).PHP_EOL;
217 echo "\033[1mA TOTAL OF $totalFixed ERROR";
218 if ($totalFixed !== 1) {
219 echo 'S';
220 }
221
222 $numFiles = count($reportFiles);
223 echo ' WERE FIXED IN '.$numFiles.' FILE';
224 if ($numFiles !== 1) {
225 echo 'S';
226 }
227
228 echo "\033[0m";
229
230 if ($failures > 0) {
231 echo PHP_EOL.str_repeat('-', $width).PHP_EOL;
232 echo "\033[1mPHPCBF FAILED TO FIX $failures FILE";
233 if ($failures !== 1) {
234 echo 'S';
235 }
236
237 echo "\033[0m";
238 }
239
240 echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
241
242 if ($toScreen === true && $interactive === false) {
243 Util\Timing::printRunTime();
244 }
245
246 }//end generate()
247
248
249 }//end class