annotate vendor/squizlabs/php_codesniffer/CodeSniffer/Reports/Full.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Full report for PHP_CodeSniffer.
Chris@0 4 *
Chris@0 5 * PHP version 5
Chris@0 6 *
Chris@0 7 * @category PHP
Chris@0 8 * @package PHP_CodeSniffer
Chris@0 9 * @author Gabriele Santini <gsantini@sqli.com>
Chris@0 10 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@0 11 * @copyright 2009-2014 SQLI <www.sqli.com>
Chris@0 12 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@0 13 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@0 14 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 15 */
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Full report for PHP_CodeSniffer.
Chris@0 19 *
Chris@0 20 * PHP version 5
Chris@0 21 *
Chris@0 22 * @category PHP
Chris@0 23 * @package PHP_CodeSniffer
Chris@0 24 * @author Gabriele Santini <gsantini@sqli.com>
Chris@0 25 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@0 26 * @copyright 2009-2014 SQLI <www.sqli.com>
Chris@0 27 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@0 28 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@0 29 * @version Release: @package_version@
Chris@0 30 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 31 */
Chris@0 32 class PHP_CodeSniffer_Reports_Full implements PHP_CodeSniffer_Report
Chris@0 33 {
Chris@0 34
Chris@0 35
Chris@0 36 /**
Chris@0 37 * Generate a partial report for a single processed file.
Chris@0 38 *
Chris@0 39 * Function should return TRUE if it printed or stored data about the file
Chris@0 40 * and FALSE if it ignored the file. Returning TRUE indicates that the file and
Chris@0 41 * its data should be counted in the grand totals.
Chris@0 42 *
Chris@0 43 * @param array $report Prepared report data.
Chris@0 44 * @param PHP_CodeSniffer_File $phpcsFile The file being reported on.
Chris@0 45 * @param boolean $showSources Show sources?
Chris@0 46 * @param int $width Maximum allowed line width.
Chris@0 47 *
Chris@0 48 * @return boolean
Chris@0 49 */
Chris@0 50 public function generateFileReport(
Chris@0 51 $report,
Chris@0 52 PHP_CodeSniffer_File $phpcsFile,
Chris@0 53 $showSources=false,
Chris@0 54 $width=80
Chris@0 55 ) {
Chris@0 56 if ($report['errors'] === 0 && $report['warnings'] === 0) {
Chris@0 57 // Nothing to print.
Chris@0 58 return false;
Chris@0 59 }
Chris@0 60
Chris@0 61 // The length of the word ERROR or WARNING; used for padding.
Chris@0 62 if ($report['warnings'] > 0) {
Chris@0 63 $typeLength = 7;
Chris@0 64 } else {
Chris@0 65 $typeLength = 5;
Chris@0 66 }
Chris@0 67
Chris@0 68 // Work out the max line number length for formatting.
Chris@0 69 $maxLineNumLength = max(array_map('strlen', array_keys($report['messages'])));
Chris@0 70
Chris@0 71 // The padding that all lines will require that are
Chris@0 72 // printing an error message overflow.
Chris@0 73 $paddingLine2 = str_repeat(' ', ($maxLineNumLength + 1));
Chris@0 74 $paddingLine2 .= ' | ';
Chris@0 75 $paddingLine2 .= str_repeat(' ', $typeLength);
Chris@0 76 $paddingLine2 .= ' | ';
Chris@0 77 if ($report['fixable'] > 0) {
Chris@0 78 $paddingLine2 .= ' ';
Chris@0 79 }
Chris@0 80
Chris@0 81 $paddingLength = strlen($paddingLine2);
Chris@0 82
Chris@0 83 // Make sure the report width isn't too big.
Chris@0 84 $maxErrorLength = 0;
Chris@0 85 foreach ($report['messages'] as $line => $lineErrors) {
Chris@0 86 foreach ($lineErrors as $column => $colErrors) {
Chris@0 87 foreach ($colErrors as $error) {
Chris@0 88 $length = strlen($error['message']);
Chris@0 89 if ($showSources === true) {
Chris@0 90 $length += (strlen($error['source']) + 3);
Chris@0 91 }
Chris@0 92
Chris@0 93 $maxErrorLength = max($maxErrorLength, ($length + 1));
Chris@0 94 }
Chris@0 95 }
Chris@0 96 }
Chris@0 97
Chris@0 98 $file = $report['filename'];
Chris@0 99 $fileLength = strlen($file);
Chris@0 100 $maxWidth = max(($fileLength + 6), ($maxErrorLength + $paddingLength));
Chris@0 101 $width = min($width, $maxWidth);
Chris@0 102 if ($width < 70) {
Chris@0 103 $width = 70;
Chris@0 104 }
Chris@0 105
Chris@0 106 echo PHP_EOL."\033[1mFILE: ";
Chris@0 107 if ($fileLength <= ($width - 6)) {
Chris@0 108 echo $file;
Chris@0 109 } else {
Chris@0 110 echo '...'.substr($file, ($fileLength - ($width - 6)));
Chris@0 111 }
Chris@0 112
Chris@0 113 echo "\033[0m".PHP_EOL;
Chris@0 114 echo str_repeat('-', $width).PHP_EOL;
Chris@0 115
Chris@0 116 echo "\033[1m".'FOUND '.$report['errors'].' ERROR';
Chris@0 117 if ($report['errors'] !== 1) {
Chris@0 118 echo 'S';
Chris@0 119 }
Chris@0 120
Chris@0 121 if ($report['warnings'] > 0) {
Chris@0 122 echo ' AND '.$report['warnings'].' WARNING';
Chris@0 123 if ($report['warnings'] !== 1) {
Chris@0 124 echo 'S';
Chris@0 125 }
Chris@0 126 }
Chris@0 127
Chris@0 128 echo ' AFFECTING '.count($report['messages']).' LINE';
Chris@0 129 if (count($report['messages']) !== 1) {
Chris@0 130 echo 'S';
Chris@0 131 }
Chris@0 132
Chris@0 133 echo "\033[0m".PHP_EOL;
Chris@0 134 echo str_repeat('-', $width).PHP_EOL;
Chris@0 135
Chris@0 136 // The maximum amount of space an error message can use.
Chris@0 137 $maxErrorSpace = ($width - $paddingLength - 1);
Chris@0 138 if ($showSources === true) {
Chris@0 139 // Account for the chars used to print colors.
Chris@0 140 $maxErrorSpace += 8;
Chris@0 141 }
Chris@0 142
Chris@0 143 foreach ($report['messages'] as $line => $lineErrors) {
Chris@0 144 foreach ($lineErrors as $column => $colErrors) {
Chris@0 145 foreach ($colErrors as $error) {
Chris@0 146 $message = $error['message'];
Chris@0 147 $message = str_replace("\n", "\n".$paddingLine2, $message);
Chris@0 148 if ($showSources === true) {
Chris@0 149 $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')';
Chris@0 150 }
Chris@0 151
Chris@0 152 // The padding that goes on the front of the line.
Chris@0 153 $padding = ($maxLineNumLength - strlen($line));
Chris@0 154 $errorMsg = wordwrap(
Chris@0 155 $message,
Chris@0 156 $maxErrorSpace,
Chris@0 157 PHP_EOL.$paddingLine2
Chris@0 158 );
Chris@0 159
Chris@0 160 echo ' '.str_repeat(' ', $padding).$line.' | ';
Chris@0 161 if ($error['type'] === 'ERROR') {
Chris@0 162 echo "\033[31mERROR\033[0m";
Chris@0 163 if ($report['warnings'] > 0) {
Chris@0 164 echo ' ';
Chris@0 165 }
Chris@0 166 } else {
Chris@0 167 echo "\033[33mWARNING\033[0m";
Chris@0 168 }
Chris@0 169
Chris@0 170 echo ' | ';
Chris@0 171 if ($report['fixable'] > 0) {
Chris@0 172 echo '[';
Chris@0 173 if ($error['fixable'] === true) {
Chris@0 174 echo 'x';
Chris@0 175 } else {
Chris@0 176 echo ' ';
Chris@0 177 }
Chris@0 178
Chris@0 179 echo '] ';
Chris@0 180 }
Chris@0 181
Chris@0 182 echo $errorMsg.PHP_EOL;
Chris@0 183 }//end foreach
Chris@0 184 }//end foreach
Chris@0 185 }//end foreach
Chris@0 186
Chris@0 187 echo str_repeat('-', $width).PHP_EOL;
Chris@0 188 if ($report['fixable'] > 0) {
Chris@0 189 echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL;
Chris@0 190 echo str_repeat('-', $width).PHP_EOL;
Chris@0 191 }
Chris@0 192
Chris@0 193 echo PHP_EOL;
Chris@0 194 return true;
Chris@0 195
Chris@0 196 }//end generateFileReport()
Chris@0 197
Chris@0 198
Chris@0 199 /**
Chris@0 200 * Prints all errors and warnings for each file processed.
Chris@0 201 *
Chris@0 202 * @param string $cachedData Any partial report data that was returned from
Chris@0 203 * generateFileReport during the run.
Chris@0 204 * @param int $totalFiles Total number of files processed during the run.
Chris@0 205 * @param int $totalErrors Total number of errors found during the run.
Chris@0 206 * @param int $totalWarnings Total number of warnings found during the run.
Chris@0 207 * @param int $totalFixable Total number of problems that can be fixed.
Chris@0 208 * @param boolean $showSources Show sources?
Chris@0 209 * @param int $width Maximum allowed line width.
Chris@0 210 * @param boolean $toScreen Is the report being printed to screen?
Chris@0 211 *
Chris@0 212 * @return void
Chris@0 213 */
Chris@0 214 public function generate(
Chris@0 215 $cachedData,
Chris@0 216 $totalFiles,
Chris@0 217 $totalErrors,
Chris@0 218 $totalWarnings,
Chris@0 219 $totalFixable,
Chris@0 220 $showSources=false,
Chris@0 221 $width=80,
Chris@0 222 $toScreen=true
Chris@0 223 ) {
Chris@0 224 if ($cachedData === '') {
Chris@0 225 return;
Chris@0 226 }
Chris@0 227
Chris@0 228 echo $cachedData;
Chris@0 229
Chris@0 230 if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
Chris@0 231 PHP_CodeSniffer_Reporting::printRunTime();
Chris@0 232 }
Chris@0 233
Chris@0 234 }//end generate()
Chris@0 235
Chris@0 236
Chris@0 237 }//end class