annotate vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php @ 5:12f9dff5fda9 tip

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