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