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 Code 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: // How many lines to show about and below the error line. Chris@17: $surroundingLines = 2; Chris@17: Chris@17: $file = $report['filename']; Chris@17: $tokens = $phpcsFile->getTokens(); Chris@17: if (empty($tokens) === true) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY === 1) { Chris@17: $startTime = microtime(true); Chris@17: echo 'CODE report is parsing '.basename($file).' '; Chris@17: } else if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "CODE report is forcing parse of $file".PHP_EOL; Chris@17: } Chris@17: Chris@17: try { Chris@17: $phpcsFile->parse(); Chris@17: } catch (\Exception $e) { Chris@17: // This is a second parse, so ignore exceptions. Chris@17: // They would have been added to the file's error list already. Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY === 1) { Chris@17: $timeTaken = ((microtime(true) - $startTime) * 1000); Chris@17: if ($timeTaken < 1000) { Chris@17: $timeTaken = round($timeTaken); Chris@17: echo "DONE in {$timeTaken}ms"; Chris@17: } else { Chris@17: $timeTaken = round(($timeTaken / 1000), 2); Chris@17: echo "DONE in $timeTaken secs"; Chris@17: } Chris@17: Chris@17: echo PHP_EOL; Chris@17: } Chris@17: Chris@17: $tokens = $phpcsFile->getTokens(); Chris@17: }//end if Chris@17: Chris@17: // Create an array that maps lines to the first token on the line. Chris@17: $lineTokens = []; Chris@17: $lastLine = 0; Chris@17: $stackPtr = 0; Chris@17: foreach ($tokens as $stackPtr => $token) { Chris@17: if ($token['line'] !== $lastLine) { Chris@17: if ($lastLine > 0) { Chris@17: $lineTokens[$lastLine]['end'] = ($stackPtr - 1); Chris@17: } Chris@17: Chris@17: $lastLine++; Chris@17: $lineTokens[$lastLine] = [ Chris@17: 'start' => $stackPtr, Chris@17: 'end' => null, Chris@17: ]; Chris@17: } Chris@17: } Chris@17: Chris@17: // Make sure the last token in the file sits on an imaginary Chris@17: // last line so it is easier to generate code snippets at the Chris@17: // end of the file. Chris@17: $lineTokens[$lastLine]['end'] = $stackPtr; Chris@17: Chris@17: // Determine the longest code line we will be showing. Chris@17: $maxSnippetLength = 0; Chris@17: $eolLen = strlen($phpcsFile->eolChar); Chris@17: foreach ($report['messages'] as $line => $lineErrors) { Chris@17: $startLine = max(($line - $surroundingLines), 1); Chris@17: $endLine = min(($line + $surroundingLines), $lastLine); Chris@17: Chris@17: $maxLineNumLength = strlen($endLine); Chris@17: Chris@17: for ($i = $startLine; $i <= $endLine; $i++) { Chris@17: if ($i === 1) { Chris@17: continue; Chris@17: } Chris@17: Chris@17: $lineLength = ($tokens[($lineTokens[$i]['start'] - 1)]['column'] + $tokens[($lineTokens[$i]['start'] - 1)]['length'] - $eolLen); Chris@17: $maxSnippetLength = max($lineLength, $maxSnippetLength); Chris@17: } Chris@17: } Chris@17: Chris@17: $maxSnippetLength += ($maxLineNumLength + 8); Chris@17: Chris@17: // Determine the longest error message we will be showing. 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: // The padding that all lines will require that are printing an error message overflow. Chris@17: if ($report['warnings'] > 0) { Chris@17: $typeLength = 7; Chris@17: } else { Chris@17: $typeLength = 5; Chris@17: } Chris@17: Chris@17: $errorPadding = str_repeat(' ', ($maxLineNumLength + 7)); Chris@17: $errorPadding .= str_repeat(' ', $typeLength); Chris@17: $errorPadding .= ' '; Chris@17: if ($report['fixable'] > 0) { Chris@17: $errorPadding .= ' '; Chris@17: } Chris@17: Chris@17: $errorPaddingLength = strlen($errorPadding); Chris@17: Chris@17: // The maximum amount of space an error message can use. Chris@17: $maxErrorSpace = ($width - $errorPaddingLength); Chris@17: if ($showSources === true) { Chris@17: // Account for the chars used to print colors. Chris@17: $maxErrorSpace += 8; Chris@17: } Chris@17: Chris@17: // Figure out the max report width we need and can use. Chris@17: $fileLength = strlen($file); Chris@17: $maxWidth = max(($fileLength + 6), ($maxErrorLength + $errorPaddingLength)); Chris@17: $width = max(min($width, $maxWidth), $maxSnippetLength); Chris@17: if ($width < 70) { Chris@17: $width = 70; Chris@17: } Chris@17: Chris@17: // Print the file header. 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: Chris@17: foreach ($report['messages'] as $line => $lineErrors) { Chris@17: $startLine = max(($line - $surroundingLines), 1); Chris@17: $endLine = min(($line + $surroundingLines), $lastLine); Chris@17: Chris@17: $snippet = ''; Chris@17: if (isset($lineTokens[$startLine]) === true) { Chris@17: for ($i = $lineTokens[$startLine]['start']; $i <= $lineTokens[$endLine]['end']; $i++) { Chris@17: $snippetLine = $tokens[$i]['line']; Chris@17: if ($lineTokens[$snippetLine]['start'] === $i) { Chris@17: // Starting a new line. Chris@17: if ($snippetLine === $line) { Chris@17: $snippet .= "\033[1m".'>> '; Chris@17: } else { Chris@17: $snippet .= ' '; Chris@17: } Chris@17: Chris@17: $snippet .= str_repeat(' ', ($maxLineNumLength - strlen($snippetLine))); Chris@17: $snippet .= $snippetLine.': '; Chris@17: if ($snippetLine === $line) { Chris@17: $snippet .= "\033[0m"; Chris@17: } Chris@17: } Chris@17: Chris@17: if (isset($tokens[$i]['orig_content']) === true) { Chris@17: $tokenContent = $tokens[$i]['orig_content']; Chris@17: } else { Chris@17: $tokenContent = $tokens[$i]['content']; Chris@17: } Chris@17: Chris@17: if (strpos($tokenContent, "\t") !== false) { Chris@17: $token = $tokens[$i]; Chris@17: $token['content'] = $tokenContent; Chris@17: if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { Chris@17: $tab = "\000"; Chris@17: } else { Chris@17: $tab = "\033[30;1m»\033[0m"; Chris@17: } Chris@17: Chris@17: $phpcsFile->tokenizer->replaceTabsInToken($token, $tab, "\000"); Chris@17: $tokenContent = $token['content']; Chris@17: } Chris@17: Chris@17: $tokenContent = Util\Common::prepareForOutput($tokenContent, ["\r", "\n", "\t"]); Chris@17: $tokenContent = str_replace("\000", ' ', $tokenContent); Chris@17: Chris@17: $underline = false; Chris@17: if ($snippetLine === $line && isset($lineErrors[$tokens[$i]['column']]) === true) { Chris@17: $underline = true; Chris@17: } Chris@17: Chris@17: // Underline invisible characters as well. Chris@17: if ($underline === true && trim($tokenContent) === '') { Chris@17: $snippet .= "\033[4m".' '."\033[0m".$tokenContent; Chris@17: } else { Chris@17: if ($underline === true) { Chris@17: $snippet .= "\033[4m"; Chris@17: } Chris@17: Chris@17: $snippet .= $tokenContent; Chris@17: Chris@17: if ($underline === true) { Chris@17: $snippet .= "\033[0m"; Chris@17: } Chris@17: } Chris@17: }//end for Chris@17: }//end if Chris@17: Chris@17: echo str_repeat('-', $width).PHP_EOL; Chris@17: Chris@17: foreach ($lineErrors as $column => $colErrors) { Chris@17: foreach ($colErrors as $error) { Chris@17: $padding = ($maxLineNumLength - strlen($line)); Chris@17: echo 'LINE '.str_repeat(' ', $padding).$line.': '; Chris@17: 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: $message = $error['message']; Chris@17: $message = str_replace("\n", "\n".$errorPadding, $message); Chris@17: if ($showSources === true) { Chris@17: $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')'; Chris@17: } Chris@17: Chris@17: $errorMsg = wordwrap( Chris@17: $message, Chris@17: $maxErrorSpace, Chris@17: PHP_EOL.$errorPadding Chris@17: ); Chris@17: Chris@17: echo $errorMsg.PHP_EOL; Chris@17: }//end foreach Chris@17: }//end foreach Chris@17: Chris@17: echo str_repeat('-', $width).PHP_EOL; Chris@17: echo rtrim($snippet).PHP_EOL; 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: 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