annotate vendor/squizlabs/php_codesniffer/src/Reports/Full.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 * Full 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 Full 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 ($report['errors'] === 0 && $report['warnings'] === 0) {
Chris@17 36 // Nothing to print.
Chris@17 37 return false;
Chris@17 38 }
Chris@17 39
Chris@17 40 // The length of the word ERROR or WARNING; used for padding.
Chris@17 41 if ($report['warnings'] > 0) {
Chris@17 42 $typeLength = 7;
Chris@17 43 } else {
Chris@17 44 $typeLength = 5;
Chris@17 45 }
Chris@17 46
Chris@17 47 // Work out the max line number length for formatting.
Chris@17 48 $maxLineNumLength = max(array_map('strlen', array_keys($report['messages'])));
Chris@17 49
Chris@17 50 // The padding that all lines will require that are
Chris@17 51 // printing an error message overflow.
Chris@17 52 $paddingLine2 = str_repeat(' ', ($maxLineNumLength + 1));
Chris@17 53 $paddingLine2 .= ' | ';
Chris@17 54 $paddingLine2 .= str_repeat(' ', $typeLength);
Chris@17 55 $paddingLine2 .= ' | ';
Chris@17 56 if ($report['fixable'] > 0) {
Chris@17 57 $paddingLine2 .= ' ';
Chris@17 58 }
Chris@17 59
Chris@17 60 $paddingLength = strlen($paddingLine2);
Chris@17 61
Chris@17 62 // Make sure the report width isn't too big.
Chris@17 63 $maxErrorLength = 0;
Chris@17 64 foreach ($report['messages'] as $line => $lineErrors) {
Chris@17 65 foreach ($lineErrors as $column => $colErrors) {
Chris@17 66 foreach ($colErrors as $error) {
Chris@17 67 $length = strlen($error['message']);
Chris@17 68 if ($showSources === true) {
Chris@17 69 $length += (strlen($error['source']) + 3);
Chris@17 70 }
Chris@17 71
Chris@17 72 $maxErrorLength = max($maxErrorLength, ($length + 1));
Chris@17 73 }
Chris@17 74 }
Chris@17 75 }
Chris@17 76
Chris@17 77 $file = $report['filename'];
Chris@17 78 $fileLength = strlen($file);
Chris@17 79 $maxWidth = max(($fileLength + 6), ($maxErrorLength + $paddingLength));
Chris@17 80 $width = min($width, $maxWidth);
Chris@17 81 if ($width < 70) {
Chris@17 82 $width = 70;
Chris@17 83 }
Chris@17 84
Chris@17 85 echo PHP_EOL."\033[1mFILE: ";
Chris@17 86 if ($fileLength <= ($width - 6)) {
Chris@17 87 echo $file;
Chris@17 88 } else {
Chris@17 89 echo '...'.substr($file, ($fileLength - ($width - 6)));
Chris@17 90 }
Chris@17 91
Chris@17 92 echo "\033[0m".PHP_EOL;
Chris@17 93 echo str_repeat('-', $width).PHP_EOL;
Chris@17 94
Chris@17 95 echo "\033[1m".'FOUND '.$report['errors'].' ERROR';
Chris@17 96 if ($report['errors'] !== 1) {
Chris@17 97 echo 'S';
Chris@17 98 }
Chris@17 99
Chris@17 100 if ($report['warnings'] > 0) {
Chris@17 101 echo ' AND '.$report['warnings'].' WARNING';
Chris@17 102 if ($report['warnings'] !== 1) {
Chris@17 103 echo 'S';
Chris@17 104 }
Chris@17 105 }
Chris@17 106
Chris@17 107 echo ' AFFECTING '.count($report['messages']).' LINE';
Chris@17 108 if (count($report['messages']) !== 1) {
Chris@17 109 echo 'S';
Chris@17 110 }
Chris@17 111
Chris@17 112 echo "\033[0m".PHP_EOL;
Chris@17 113 echo str_repeat('-', $width).PHP_EOL;
Chris@17 114
Chris@17 115 // The maximum amount of space an error message can use.
Chris@17 116 $maxErrorSpace = ($width - $paddingLength - 1);
Chris@17 117
Chris@17 118 foreach ($report['messages'] as $line => $lineErrors) {
Chris@17 119 foreach ($lineErrors as $column => $colErrors) {
Chris@17 120 foreach ($colErrors as $error) {
Chris@17 121 $message = $error['message'];
Chris@17 122 $msgLines = [$message];
Chris@17 123 if (strpos($message, "\n") !== false) {
Chris@17 124 $msgLines = explode("\n", $message);
Chris@17 125 }
Chris@17 126
Chris@17 127 $errorMsg = '';
Chris@17 128 $lastLine = (count($msgLines) - 1);
Chris@17 129 foreach ($msgLines as $k => $msgLine) {
Chris@17 130 if ($k === 0) {
Chris@17 131 if ($showSources === true) {
Chris@17 132 $errorMsg .= "\033[1m";
Chris@17 133 }
Chris@17 134 } else {
Chris@17 135 $errorMsg .= PHP_EOL.$paddingLine2;
Chris@17 136 }
Chris@17 137
Chris@17 138 if ($k === $lastLine && $showSources === true) {
Chris@17 139 $msgLine .= "\033[0m".' ('.$error['source'].')';
Chris@17 140 }
Chris@17 141
Chris@17 142 $errorMsg .= wordwrap(
Chris@17 143 $msgLine,
Chris@17 144 $maxErrorSpace,
Chris@17 145 PHP_EOL.$paddingLine2
Chris@17 146 );
Chris@17 147 }
Chris@17 148
Chris@17 149 // The padding that goes on the front of the line.
Chris@17 150 $padding = ($maxLineNumLength - strlen($line));
Chris@17 151
Chris@17 152 echo ' '.str_repeat(' ', $padding).$line.' | ';
Chris@17 153 if ($error['type'] === 'ERROR') {
Chris@17 154 echo "\033[31mERROR\033[0m";
Chris@17 155 if ($report['warnings'] > 0) {
Chris@17 156 echo ' ';
Chris@17 157 }
Chris@17 158 } else {
Chris@17 159 echo "\033[33mWARNING\033[0m";
Chris@17 160 }
Chris@17 161
Chris@17 162 echo ' | ';
Chris@17 163 if ($report['fixable'] > 0) {
Chris@17 164 echo '[';
Chris@17 165 if ($error['fixable'] === true) {
Chris@17 166 echo 'x';
Chris@17 167 } else {
Chris@17 168 echo ' ';
Chris@17 169 }
Chris@17 170
Chris@17 171 echo '] ';
Chris@17 172 }
Chris@17 173
Chris@17 174 echo $errorMsg.PHP_EOL;
Chris@17 175 }//end foreach
Chris@17 176 }//end foreach
Chris@17 177 }//end foreach
Chris@17 178
Chris@17 179 echo str_repeat('-', $width).PHP_EOL;
Chris@17 180 if ($report['fixable'] > 0) {
Chris@17 181 echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL;
Chris@17 182 echo str_repeat('-', $width).PHP_EOL;
Chris@17 183 }
Chris@17 184
Chris@17 185 echo PHP_EOL;
Chris@17 186 return true;
Chris@17 187
Chris@17 188 }//end generateFileReport()
Chris@17 189
Chris@17 190
Chris@17 191 /**
Chris@17 192 * Prints all errors and warnings for each file processed.
Chris@17 193 *
Chris@17 194 * @param string $cachedData Any partial report data that was returned from
Chris@17 195 * generateFileReport during the run.
Chris@17 196 * @param int $totalFiles Total number of files processed during the run.
Chris@17 197 * @param int $totalErrors Total number of errors found during the run.
Chris@17 198 * @param int $totalWarnings Total number of warnings found during the run.
Chris@17 199 * @param int $totalFixable Total number of problems that can be fixed.
Chris@17 200 * @param bool $showSources Show sources?
Chris@17 201 * @param int $width Maximum allowed line width.
Chris@17 202 * @param bool $interactive Are we running in interactive mode?
Chris@17 203 * @param bool $toScreen Is the report being printed to screen?
Chris@17 204 *
Chris@17 205 * @return void
Chris@17 206 */
Chris@17 207 public function generate(
Chris@17 208 $cachedData,
Chris@17 209 $totalFiles,
Chris@17 210 $totalErrors,
Chris@17 211 $totalWarnings,
Chris@17 212 $totalFixable,
Chris@17 213 $showSources=false,
Chris@17 214 $width=80,
Chris@17 215 $interactive=false,
Chris@17 216 $toScreen=true
Chris@17 217 ) {
Chris@17 218 if ($cachedData === '') {
Chris@17 219 return;
Chris@17 220 }
Chris@17 221
Chris@17 222 echo $cachedData;
Chris@17 223
Chris@17 224 if ($toScreen === true && $interactive === false) {
Chris@17 225 Util\Timing::printRunTime();
Chris@17 226 }
Chris@17 227
Chris@17 228 }//end generate()
Chris@17 229
Chris@17 230
Chris@17 231 }//end class