Chris@0: Chris@0: * @author Greg Sherwood Chris@0: * @copyright 2009-2014 SQLI Chris@0: * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) Chris@0: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Full report for PHP_CodeSniffer. Chris@0: * Chris@0: * PHP version 5 Chris@0: * Chris@0: * @category PHP Chris@0: * @package PHP_CodeSniffer Chris@0: * @author Gabriele Santini Chris@0: * @author Greg Sherwood Chris@0: * @copyright 2009-2014 SQLI Chris@0: * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) Chris@0: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @version Release: @package_version@ Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: class PHP_CodeSniffer_Reports_Full implements PHP_CodeSniffer_Report Chris@0: { Chris@0: Chris@0: Chris@0: /** Chris@0: * Generate a partial report for a single processed file. Chris@0: * Chris@0: * Function should return TRUE if it printed or stored data about the file Chris@0: * and FALSE if it ignored the file. Returning TRUE indicates that the file and Chris@0: * its data should be counted in the grand totals. Chris@0: * Chris@0: * @param array $report Prepared report data. Chris@0: * @param PHP_CodeSniffer_File $phpcsFile The file being reported on. Chris@0: * @param boolean $showSources Show sources? Chris@0: * @param int $width Maximum allowed line width. Chris@0: * Chris@0: * @return boolean Chris@0: */ Chris@0: public function generateFileReport( Chris@0: $report, Chris@0: PHP_CodeSniffer_File $phpcsFile, Chris@0: $showSources=false, Chris@0: $width=80 Chris@0: ) { Chris@0: if ($report['errors'] === 0 && $report['warnings'] === 0) { Chris@0: // Nothing to print. Chris@0: return false; Chris@0: } Chris@0: Chris@0: // The length of the word ERROR or WARNING; used for padding. Chris@0: if ($report['warnings'] > 0) { Chris@0: $typeLength = 7; Chris@0: } else { Chris@0: $typeLength = 5; Chris@0: } Chris@0: Chris@0: // Work out the max line number length for formatting. Chris@0: $maxLineNumLength = max(array_map('strlen', array_keys($report['messages']))); Chris@0: Chris@0: // The padding that all lines will require that are Chris@0: // printing an error message overflow. Chris@0: $paddingLine2 = str_repeat(' ', ($maxLineNumLength + 1)); Chris@0: $paddingLine2 .= ' | '; Chris@0: $paddingLine2 .= str_repeat(' ', $typeLength); Chris@0: $paddingLine2 .= ' | '; Chris@0: if ($report['fixable'] > 0) { Chris@0: $paddingLine2 .= ' '; Chris@0: } Chris@0: Chris@0: $paddingLength = strlen($paddingLine2); Chris@0: Chris@0: // Make sure the report width isn't too big. Chris@0: $maxErrorLength = 0; Chris@0: foreach ($report['messages'] as $line => $lineErrors) { Chris@0: foreach ($lineErrors as $column => $colErrors) { Chris@0: foreach ($colErrors as $error) { Chris@0: $length = strlen($error['message']); Chris@0: if ($showSources === true) { Chris@0: $length += (strlen($error['source']) + 3); Chris@0: } Chris@0: Chris@0: $maxErrorLength = max($maxErrorLength, ($length + 1)); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $file = $report['filename']; Chris@0: $fileLength = strlen($file); Chris@0: $maxWidth = max(($fileLength + 6), ($maxErrorLength + $paddingLength)); Chris@0: $width = min($width, $maxWidth); Chris@0: if ($width < 70) { Chris@0: $width = 70; Chris@0: } Chris@0: Chris@0: echo PHP_EOL."\033[1mFILE: "; Chris@0: if ($fileLength <= ($width - 6)) { Chris@0: echo $file; Chris@0: } else { Chris@0: echo '...'.substr($file, ($fileLength - ($width - 6))); Chris@0: } Chris@0: Chris@0: echo "\033[0m".PHP_EOL; Chris@0: echo str_repeat('-', $width).PHP_EOL; Chris@0: Chris@0: echo "\033[1m".'FOUND '.$report['errors'].' ERROR'; Chris@0: if ($report['errors'] !== 1) { Chris@0: echo 'S'; Chris@0: } Chris@0: Chris@0: if ($report['warnings'] > 0) { Chris@0: echo ' AND '.$report['warnings'].' WARNING'; Chris@0: if ($report['warnings'] !== 1) { Chris@0: echo 'S'; Chris@0: } Chris@0: } Chris@0: Chris@0: echo ' AFFECTING '.count($report['messages']).' LINE'; Chris@0: if (count($report['messages']) !== 1) { Chris@0: echo 'S'; Chris@0: } Chris@0: Chris@0: echo "\033[0m".PHP_EOL; Chris@0: echo str_repeat('-', $width).PHP_EOL; Chris@0: Chris@0: // The maximum amount of space an error message can use. Chris@0: $maxErrorSpace = ($width - $paddingLength - 1); Chris@0: if ($showSources === true) { Chris@0: // Account for the chars used to print colors. Chris@0: $maxErrorSpace += 8; Chris@0: } Chris@0: Chris@0: foreach ($report['messages'] as $line => $lineErrors) { Chris@0: foreach ($lineErrors as $column => $colErrors) { Chris@0: foreach ($colErrors as $error) { Chris@0: $message = $error['message']; Chris@0: $message = str_replace("\n", "\n".$paddingLine2, $message); Chris@0: if ($showSources === true) { Chris@0: $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')'; Chris@0: } Chris@0: Chris@0: // The padding that goes on the front of the line. Chris@0: $padding = ($maxLineNumLength - strlen($line)); Chris@0: $errorMsg = wordwrap( Chris@0: $message, Chris@0: $maxErrorSpace, Chris@0: PHP_EOL.$paddingLine2 Chris@0: ); Chris@0: Chris@0: echo ' '.str_repeat(' ', $padding).$line.' | '; Chris@0: if ($error['type'] === 'ERROR') { Chris@0: echo "\033[31mERROR\033[0m"; Chris@0: if ($report['warnings'] > 0) { Chris@0: echo ' '; Chris@0: } Chris@0: } else { Chris@0: echo "\033[33mWARNING\033[0m"; Chris@0: } Chris@0: Chris@0: echo ' | '; Chris@0: if ($report['fixable'] > 0) { Chris@0: echo '['; Chris@0: if ($error['fixable'] === true) { Chris@0: echo 'x'; Chris@0: } else { Chris@0: echo ' '; Chris@0: } Chris@0: Chris@0: echo '] '; Chris@0: } Chris@0: Chris@0: echo $errorMsg.PHP_EOL; Chris@0: }//end foreach Chris@0: }//end foreach Chris@0: }//end foreach Chris@0: Chris@0: echo str_repeat('-', $width).PHP_EOL; Chris@0: if ($report['fixable'] > 0) { Chris@0: echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL; Chris@0: echo str_repeat('-', $width).PHP_EOL; Chris@0: } Chris@0: Chris@0: echo PHP_EOL; Chris@0: return true; Chris@0: Chris@0: }//end generateFileReport() Chris@0: Chris@0: Chris@0: /** Chris@0: * Prints all errors and warnings for each file processed. Chris@0: * Chris@0: * @param string $cachedData Any partial report data that was returned from Chris@0: * generateFileReport during the run. Chris@0: * @param int $totalFiles Total number of files processed during the run. Chris@0: * @param int $totalErrors Total number of errors found during the run. Chris@0: * @param int $totalWarnings Total number of warnings found during the run. Chris@0: * @param int $totalFixable Total number of problems that can be fixed. Chris@0: * @param boolean $showSources Show sources? Chris@0: * @param int $width Maximum allowed line width. Chris@0: * @param boolean $toScreen Is the report being printed to screen? Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function generate( Chris@0: $cachedData, Chris@0: $totalFiles, Chris@0: $totalErrors, Chris@0: $totalWarnings, Chris@0: $totalFixable, Chris@0: $showSources=false, Chris@0: $width=80, Chris@0: $toScreen=true Chris@0: ) { Chris@0: if ($cachedData === '') { Chris@0: return; Chris@0: } Chris@0: Chris@0: echo $cachedData; Chris@0: Chris@0: if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) { Chris@0: PHP_CodeSniffer_Reporting::printRunTime(); Chris@0: } Chris@0: Chris@0: }//end generate() Chris@0: Chris@0: Chris@0: }//end class