annotate vendor/squizlabs/php_codesniffer/src/Reports/Cbf.php @ 19:fa3358dc1485 tip

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