Chris@4: Chris@4: * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) Chris@4: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@4: */ Chris@4: Chris@4: namespace PHP_CodeSniffer\Reports; Chris@4: Chris@4: use PHP_CodeSniffer\Files\File; Chris@4: use PHP_CodeSniffer\Util; Chris@4: Chris@4: class Code implements Report Chris@4: { Chris@4: Chris@4: Chris@4: /** Chris@4: * Generate a partial report for a single processed file. Chris@4: * Chris@4: * Function should return TRUE if it printed or stored data about the file Chris@4: * and FALSE if it ignored the file. Returning TRUE indicates that the file and Chris@4: * its data should be counted in the grand totals. Chris@4: * Chris@4: * @param array $report Prepared report data. Chris@4: * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on. Chris@4: * @param bool $showSources Show sources? Chris@4: * @param int $width Maximum allowed line width. Chris@4: * Chris@4: * @return bool Chris@4: */ Chris@4: public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) Chris@4: { Chris@4: if ($report['errors'] === 0 && $report['warnings'] === 0) { Chris@4: // Nothing to print. Chris@4: return false; Chris@4: } Chris@4: Chris@4: // How many lines to show about and below the error line. Chris@4: $surroundingLines = 2; Chris@4: Chris@4: $file = $report['filename']; Chris@4: $tokens = $phpcsFile->getTokens(); Chris@4: if (empty($tokens) === true) { Chris@4: if (PHP_CODESNIFFER_VERBOSITY === 1) { Chris@4: $startTime = microtime(true); Chris@4: echo 'CODE report is parsing '.basename($file).' '; Chris@4: } else if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@4: echo "CODE report is forcing parse of $file".PHP_EOL; Chris@4: } Chris@4: Chris@4: try { Chris@4: $phpcsFile->parse(); Chris@4: } catch (\Exception $e) { Chris@4: // This is a second parse, so ignore exceptions. Chris@4: // They would have been added to the file's error list already. Chris@4: } Chris@4: Chris@4: if (PHP_CODESNIFFER_VERBOSITY === 1) { Chris@4: $timeTaken = ((microtime(true) - $startTime) * 1000); Chris@4: if ($timeTaken < 1000) { Chris@4: $timeTaken = round($timeTaken); Chris@4: echo "DONE in {$timeTaken}ms"; Chris@4: } else { Chris@4: $timeTaken = round(($timeTaken / 1000), 2); Chris@4: echo "DONE in $timeTaken secs"; Chris@4: } Chris@4: Chris@4: echo PHP_EOL; Chris@4: } Chris@4: Chris@4: $tokens = $phpcsFile->getTokens(); Chris@4: }//end if Chris@4: Chris@4: // Create an array that maps lines to the first token on the line. Chris@4: $lineTokens = []; Chris@4: $lastLine = 0; Chris@4: $stackPtr = 0; Chris@4: foreach ($tokens as $stackPtr => $token) { Chris@4: if ($token['line'] !== $lastLine) { Chris@4: if ($lastLine > 0) { Chris@4: $lineTokens[$lastLine]['end'] = ($stackPtr - 1); Chris@4: } Chris@4: Chris@4: $lastLine++; Chris@4: $lineTokens[$lastLine] = [ Chris@4: 'start' => $stackPtr, Chris@4: 'end' => null, Chris@4: ]; Chris@4: } Chris@4: } Chris@4: Chris@4: // Make sure the last token in the file sits on an imaginary Chris@4: // last line so it is easier to generate code snippets at the Chris@4: // end of the file. Chris@4: $lineTokens[$lastLine]['end'] = $stackPtr; Chris@4: Chris@4: // Determine the longest code line we will be showing. Chris@4: $maxSnippetLength = 0; Chris@4: $eolLen = strlen($phpcsFile->eolChar); Chris@4: foreach ($report['messages'] as $line => $lineErrors) { Chris@4: $startLine = max(($line - $surroundingLines), 1); Chris@4: $endLine = min(($line + $surroundingLines), $lastLine); Chris@4: Chris@4: $maxLineNumLength = strlen($endLine); Chris@4: Chris@4: for ($i = $startLine; $i <= $endLine; $i++) { Chris@4: if ($i === 1) { Chris@4: continue; Chris@4: } Chris@4: Chris@4: $lineLength = ($tokens[($lineTokens[$i]['start'] - 1)]['column'] + $tokens[($lineTokens[$i]['start'] - 1)]['length'] - $eolLen); Chris@4: $maxSnippetLength = max($lineLength, $maxSnippetLength); Chris@4: } Chris@4: } Chris@4: Chris@4: $maxSnippetLength += ($maxLineNumLength + 8); Chris@4: Chris@4: // Determine the longest error message we will be showing. Chris@4: $maxErrorLength = 0; Chris@4: foreach ($report['messages'] as $line => $lineErrors) { Chris@4: foreach ($lineErrors as $column => $colErrors) { Chris@4: foreach ($colErrors as $error) { Chris@4: $length = strlen($error['message']); Chris@4: if ($showSources === true) { Chris@4: $length += (strlen($error['source']) + 3); Chris@4: } Chris@4: Chris@4: $maxErrorLength = max($maxErrorLength, ($length + 1)); Chris@4: } Chris@4: } Chris@4: } Chris@4: Chris@4: // The padding that all lines will require that are printing an error message overflow. Chris@4: if ($report['warnings'] > 0) { Chris@4: $typeLength = 7; Chris@4: } else { Chris@4: $typeLength = 5; Chris@4: } Chris@4: Chris@4: $errorPadding = str_repeat(' ', ($maxLineNumLength + 7)); Chris@4: $errorPadding .= str_repeat(' ', $typeLength); Chris@4: $errorPadding .= ' '; Chris@4: if ($report['fixable'] > 0) { Chris@4: $errorPadding .= ' '; Chris@4: } Chris@4: Chris@4: $errorPaddingLength = strlen($errorPadding); Chris@4: Chris@4: // The maximum amount of space an error message can use. Chris@4: $maxErrorSpace = ($width - $errorPaddingLength); Chris@4: if ($showSources === true) { Chris@4: // Account for the chars used to print colors. Chris@4: $maxErrorSpace += 8; Chris@4: } Chris@4: Chris@4: // Figure out the max report width we need and can use. Chris@4: $fileLength = strlen($file); Chris@4: $maxWidth = max(($fileLength + 6), ($maxErrorLength + $errorPaddingLength)); Chris@4: $width = max(min($width, $maxWidth), $maxSnippetLength); Chris@4: if ($width < 70) { Chris@4: $width = 70; Chris@4: } Chris@4: Chris@4: // Print the file header. Chris@4: echo PHP_EOL."\033[1mFILE: "; Chris@4: if ($fileLength <= ($width - 6)) { Chris@4: echo $file; Chris@4: } else { Chris@4: echo '...'.substr($file, ($fileLength - ($width - 6))); Chris@4: } Chris@4: Chris@4: echo "\033[0m".PHP_EOL; Chris@4: echo str_repeat('-', $width).PHP_EOL; Chris@4: Chris@4: echo "\033[1m".'FOUND '.$report['errors'].' ERROR'; Chris@4: if ($report['errors'] !== 1) { Chris@4: echo 'S'; Chris@4: } Chris@4: Chris@4: if ($report['warnings'] > 0) { Chris@4: echo ' AND '.$report['warnings'].' WARNING'; Chris@4: if ($report['warnings'] !== 1) { Chris@4: echo 'S'; Chris@4: } Chris@4: } Chris@4: Chris@4: echo ' AFFECTING '.count($report['messages']).' LINE'; Chris@4: if (count($report['messages']) !== 1) { Chris@4: echo 'S'; Chris@4: } Chris@4: Chris@4: echo "\033[0m".PHP_EOL; Chris@4: Chris@4: foreach ($report['messages'] as $line => $lineErrors) { Chris@4: $startLine = max(($line - $surroundingLines), 1); Chris@4: $endLine = min(($line + $surroundingLines), $lastLine); Chris@4: Chris@4: $snippet = ''; Chris@4: if (isset($lineTokens[$startLine]) === true) { Chris@4: for ($i = $lineTokens[$startLine]['start']; $i <= $lineTokens[$endLine]['end']; $i++) { Chris@4: $snippetLine = $tokens[$i]['line']; Chris@4: if ($lineTokens[$snippetLine]['start'] === $i) { Chris@4: // Starting a new line. Chris@4: if ($snippetLine === $line) { Chris@4: $snippet .= "\033[1m".'>> '; Chris@4: } else { Chris@4: $snippet .= ' '; Chris@4: } Chris@4: Chris@4: $snippet .= str_repeat(' ', ($maxLineNumLength - strlen($snippetLine))); Chris@4: $snippet .= $snippetLine.': '; Chris@4: if ($snippetLine === $line) { Chris@4: $snippet .= "\033[0m"; Chris@4: } Chris@4: } Chris@4: Chris@4: if (isset($tokens[$i]['orig_content']) === true) { Chris@4: $tokenContent = $tokens[$i]['orig_content']; Chris@4: } else { Chris@4: $tokenContent = $tokens[$i]['content']; Chris@4: } Chris@4: Chris@4: if (strpos($tokenContent, "\t") !== false) { Chris@4: $token = $tokens[$i]; Chris@4: $token['content'] = $tokenContent; Chris@4: if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { Chris@4: $tab = "\000"; Chris@4: } else { Chris@4: $tab = "\033[30;1m»\033[0m"; Chris@4: } Chris@4: Chris@4: $phpcsFile->tokenizer->replaceTabsInToken($token, $tab, "\000"); Chris@4: $tokenContent = $token['content']; Chris@4: } Chris@4: Chris@4: $tokenContent = Util\Common::prepareForOutput($tokenContent, ["\r", "\n", "\t"]); Chris@4: $tokenContent = str_replace("\000", ' ', $tokenContent); Chris@4: Chris@4: $underline = false; Chris@4: if ($snippetLine === $line && isset($lineErrors[$tokens[$i]['column']]) === true) { Chris@4: $underline = true; Chris@4: } Chris@4: Chris@4: // Underline invisible characters as well. Chris@4: if ($underline === true && trim($tokenContent) === '') { Chris@4: $snippet .= "\033[4m".' '."\033[0m".$tokenContent; Chris@4: } else { Chris@4: if ($underline === true) { Chris@4: $snippet .= "\033[4m"; Chris@4: } Chris@4: Chris@4: $snippet .= $tokenContent; Chris@4: Chris@4: if ($underline === true) { Chris@4: $snippet .= "\033[0m"; Chris@4: } Chris@4: } Chris@4: }//end for Chris@4: }//end if Chris@4: Chris@4: echo str_repeat('-', $width).PHP_EOL; Chris@4: Chris@4: foreach ($lineErrors as $column => $colErrors) { Chris@4: foreach ($colErrors as $error) { Chris@4: $padding = ($maxLineNumLength - strlen($line)); Chris@4: echo 'LINE '.str_repeat(' ', $padding).$line.': '; Chris@4: Chris@4: if ($error['type'] === 'ERROR') { Chris@4: echo "\033[31mERROR\033[0m"; Chris@4: if ($report['warnings'] > 0) { Chris@4: echo ' '; Chris@4: } Chris@4: } else { Chris@4: echo "\033[33mWARNING\033[0m"; Chris@4: } Chris@4: Chris@4: echo ' '; Chris@4: if ($report['fixable'] > 0) { Chris@4: echo '['; Chris@4: if ($error['fixable'] === true) { Chris@4: echo 'x'; Chris@4: } else { Chris@4: echo ' '; Chris@4: } Chris@4: Chris@4: echo '] '; Chris@4: } Chris@4: Chris@4: $message = $error['message']; Chris@4: $message = str_replace("\n", "\n".$errorPadding, $message); Chris@4: if ($showSources === true) { Chris@4: $message = "\033[1m".$message."\033[0m".' ('.$error['source'].')'; Chris@4: } Chris@4: Chris@4: $errorMsg = wordwrap( Chris@4: $message, Chris@4: $maxErrorSpace, Chris@4: PHP_EOL.$errorPadding Chris@4: ); Chris@4: Chris@4: echo $errorMsg.PHP_EOL; Chris@4: }//end foreach Chris@4: }//end foreach Chris@4: Chris@4: echo str_repeat('-', $width).PHP_EOL; Chris@4: echo rtrim($snippet).PHP_EOL; Chris@4: }//end foreach Chris@4: Chris@4: echo str_repeat('-', $width).PHP_EOL; Chris@4: if ($report['fixable'] > 0) { Chris@4: echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL; Chris@4: echo str_repeat('-', $width).PHP_EOL; Chris@4: } Chris@4: Chris@4: return true; Chris@4: Chris@4: }//end generateFileReport() Chris@4: Chris@4: Chris@4: /** Chris@4: * Prints all errors and warnings for each file processed. Chris@4: * Chris@4: * @param string $cachedData Any partial report data that was returned from Chris@4: * generateFileReport during the run. Chris@4: * @param int $totalFiles Total number of files processed during the run. Chris@4: * @param int $totalErrors Total number of errors found during the run. Chris@4: * @param int $totalWarnings Total number of warnings found during the run. Chris@4: * @param int $totalFixable Total number of problems that can be fixed. Chris@4: * @param bool $showSources Show sources? Chris@4: * @param int $width Maximum allowed line width. Chris@4: * @param bool $interactive Are we running in interactive mode? Chris@4: * @param bool $toScreen Is the report being printed to screen? Chris@4: * Chris@4: * @return void Chris@4: */ Chris@4: public function generate( Chris@4: $cachedData, Chris@4: $totalFiles, Chris@4: $totalErrors, Chris@4: $totalWarnings, Chris@4: $totalFixable, Chris@4: $showSources=false, Chris@4: $width=80, Chris@4: $interactive=false, Chris@4: $toScreen=true Chris@4: ) { Chris@4: if ($cachedData === '') { Chris@4: return; Chris@4: } Chris@4: Chris@4: echo $cachedData; Chris@4: Chris@4: if ($toScreen === true && $interactive === false) { Chris@4: Util\Timing::printRunTime(); Chris@4: } Chris@4: Chris@4: }//end generate() Chris@4: Chris@4: Chris@4: }//end class