annotate vendor/squizlabs/php_codesniffer/src/Reports/Summary.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 * Summary report for PHP_CodeSniffer.
Chris@17 4 *
Chris@17 5 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@17 6 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@17 7 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@17 8 */
Chris@17 9
Chris@17 10 namespace PHP_CodeSniffer\Reports;
Chris@17 11
Chris@17 12 use PHP_CodeSniffer\Files\File;
Chris@17 13 use PHP_CodeSniffer\Util;
Chris@17 14
Chris@17 15 class Summary implements Report
Chris@17 16 {
Chris@17 17
Chris@17 18
Chris@17 19 /**
Chris@17 20 * Generate a partial report for a single processed file.
Chris@17 21 *
Chris@17 22 * Function should return TRUE if it printed or stored data about the file
Chris@17 23 * and FALSE if it ignored the file. Returning TRUE indicates that the file and
Chris@17 24 * its data should be counted in the grand totals.
Chris@17 25 *
Chris@17 26 * @param array $report Prepared report data.
Chris@17 27 * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on.
Chris@17 28 * @param bool $showSources Show sources?
Chris@17 29 * @param int $width Maximum allowed line width.
Chris@17 30 *
Chris@17 31 * @return bool
Chris@17 32 */
Chris@17 33 public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
Chris@17 34 {
Chris@17 35 if (PHP_CODESNIFFER_VERBOSITY === 0
Chris@17 36 && $report['errors'] === 0
Chris@17 37 && $report['warnings'] === 0
Chris@17 38 ) {
Chris@17 39 // Nothing to print.
Chris@17 40 return false;
Chris@17 41 }
Chris@17 42
Chris@17 43 echo $report['filename'].'>>'.$report['errors'].'>>'.$report['warnings'].PHP_EOL;
Chris@17 44 return true;
Chris@17 45
Chris@17 46 }//end generateFileReport()
Chris@17 47
Chris@17 48
Chris@17 49 /**
Chris@17 50 * Generates a summary of errors and warnings for each file processed.
Chris@17 51 *
Chris@17 52 * @param string $cachedData Any partial report data that was returned from
Chris@17 53 * generateFileReport during the run.
Chris@17 54 * @param int $totalFiles Total number of files processed during the run.
Chris@17 55 * @param int $totalErrors Total number of errors found during the run.
Chris@17 56 * @param int $totalWarnings Total number of warnings found during the run.
Chris@17 57 * @param int $totalFixable Total number of problems that can be fixed.
Chris@17 58 * @param bool $showSources Show sources?
Chris@17 59 * @param int $width Maximum allowed line width.
Chris@17 60 * @param bool $interactive Are we running in interactive mode?
Chris@17 61 * @param bool $toScreen Is the report being printed to screen?
Chris@17 62 *
Chris@17 63 * @return void
Chris@17 64 */
Chris@17 65 public function generate(
Chris@17 66 $cachedData,
Chris@17 67 $totalFiles,
Chris@17 68 $totalErrors,
Chris@17 69 $totalWarnings,
Chris@17 70 $totalFixable,
Chris@17 71 $showSources=false,
Chris@17 72 $width=80,
Chris@17 73 $interactive=false,
Chris@17 74 $toScreen=true
Chris@17 75 ) {
Chris@17 76 $lines = explode(PHP_EOL, $cachedData);
Chris@17 77 array_pop($lines);
Chris@17 78
Chris@17 79 if (empty($lines) === true) {
Chris@17 80 return;
Chris@17 81 }
Chris@17 82
Chris@17 83 $reportFiles = [];
Chris@17 84 $maxLength = 0;
Chris@17 85
Chris@17 86 foreach ($lines as $line) {
Chris@17 87 $parts = explode('>>', $line);
Chris@17 88 $fileLen = strlen($parts[0]);
Chris@17 89 $reportFiles[$parts[0]] = [
Chris@17 90 'errors' => $parts[1],
Chris@17 91 'warnings' => $parts[2],
Chris@17 92 'strlen' => $fileLen,
Chris@17 93 ];
Chris@17 94
Chris@17 95 $maxLength = max($maxLength, $fileLen);
Chris@17 96 }
Chris@17 97
Chris@17 98 uksort(
Chris@17 99 $reportFiles,
Chris@17 100 function ($keyA, $keyB) {
Chris@17 101 $pathPartsA = explode(DIRECTORY_SEPARATOR, $keyA);
Chris@17 102 $pathPartsB = explode(DIRECTORY_SEPARATOR, $keyB);
Chris@17 103
Chris@17 104 do {
Chris@17 105 $partA = array_shift($pathPartsA);
Chris@17 106 $partB = array_shift($pathPartsB);
Chris@17 107 } while ($partA === $partB && empty($pathPartsA) === false && empty($pathPartsB) === false);
Chris@17 108
Chris@17 109 if (empty($pathPartsA) === false && empty($pathPartsB) === true) {
Chris@17 110 return 1;
Chris@17 111 } else if (empty($pathPartsA) === true && empty($pathPartsB) === false) {
Chris@17 112 return -1;
Chris@17 113 } else {
Chris@17 114 return strcasecmp($partA, $partB);
Chris@17 115 }
Chris@17 116 }
Chris@17 117 );
Chris@17 118
Chris@17 119 $width = min($width, ($maxLength + 21));
Chris@17 120 $width = max($width, 70);
Chris@17 121
Chris@17 122 echo PHP_EOL."\033[1m".'PHP CODE SNIFFER REPORT SUMMARY'."\033[0m".PHP_EOL;
Chris@17 123 echo str_repeat('-', $width).PHP_EOL;
Chris@17 124 echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'ERRORS WARNINGS'."\033[0m".PHP_EOL;
Chris@17 125 echo str_repeat('-', $width).PHP_EOL;
Chris@17 126
Chris@17 127 foreach ($reportFiles as $file => $data) {
Chris@17 128 $padding = ($width - 18 - $data['strlen']);
Chris@17 129 if ($padding < 0) {
Chris@17 130 $file = '...'.substr($file, (($padding * -1) + 3));
Chris@17 131 $padding = 0;
Chris@17 132 }
Chris@17 133
Chris@17 134 echo $file.str_repeat(' ', $padding).' ';
Chris@17 135 if ($data['errors'] !== 0) {
Chris@17 136 echo "\033[31m".$data['errors']."\033[0m";
Chris@17 137 echo str_repeat(' ', (8 - strlen((string) $data['errors'])));
Chris@17 138 } else {
Chris@17 139 echo '0 ';
Chris@17 140 }
Chris@17 141
Chris@17 142 if ($data['warnings'] !== 0) {
Chris@17 143 echo "\033[33m".$data['warnings']."\033[0m";
Chris@17 144 } else {
Chris@17 145 echo '0';
Chris@17 146 }
Chris@17 147
Chris@17 148 echo PHP_EOL;
Chris@17 149 }//end foreach
Chris@17 150
Chris@17 151 echo str_repeat('-', $width).PHP_EOL;
Chris@17 152 echo "\033[1mA TOTAL OF $totalErrors ERROR";
Chris@17 153 if ($totalErrors !== 1) {
Chris@17 154 echo 'S';
Chris@17 155 }
Chris@17 156
Chris@17 157 echo ' AND '.$totalWarnings.' WARNING';
Chris@17 158 if ($totalWarnings !== 1) {
Chris@17 159 echo 'S';
Chris@17 160 }
Chris@17 161
Chris@17 162 echo ' WERE FOUND IN '.$totalFiles.' FILE';
Chris@17 163 if ($totalFiles !== 1) {
Chris@17 164 echo 'S';
Chris@17 165 }
Chris@17 166
Chris@17 167 echo "\033[0m";
Chris@17 168
Chris@17 169 if ($totalFixable > 0) {
Chris@17 170 echo PHP_EOL.str_repeat('-', $width).PHP_EOL;
Chris@17 171 echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m";
Chris@17 172 }
Chris@17 173
Chris@17 174 echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
Chris@17 175
Chris@17 176 if ($toScreen === true && $interactive === false) {
Chris@17 177 Util\Timing::printRunTime();
Chris@17 178 }
Chris@17 179
Chris@17 180 }//end generate()
Chris@17 181
Chris@17 182
Chris@17 183 }//end class