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