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