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