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\Exceptions\DeepExitException; Chris@17: use PHP_CodeSniffer\Files\File; Chris@17: use PHP_CodeSniffer\Util; Chris@17: Chris@17: class Cbf 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: $errors = $phpcsFile->getFixableCount(); Chris@17: if ($errors !== 0) { Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 0) { Chris@17: ob_end_clean(); Chris@17: $startTime = microtime(true); Chris@17: echo "\t=> Fixing file: $errors/$errors violations remaining"; Chris@17: } Chris@17: Chris@17: $fixed = $phpcsFile->fixer->fixFile(); Chris@17: } Chris@17: Chris@17: if ($phpcsFile->config->stdin === true) { Chris@17: // Replacing STDIN, so output current file to STDOUT Chris@17: // even if nothing was fixed. Exit here because we Chris@17: // can't process any more than 1 file in this setup. Chris@17: $fixedContent = $phpcsFile->fixer->getContents(); Chris@17: throw new DeepExitException($fixedContent, 1); Chris@17: } Chris@17: Chris@17: if ($errors === 0) { Chris@17: return false; Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 0) { Chris@17: if ($fixed === false) { Chris@17: echo 'ERROR'; Chris@17: } else { Chris@17: echo 'DONE'; Chris@17: } Chris@17: Chris@17: $timeTaken = ((microtime(true) - $startTime) * 1000); Chris@17: if ($timeTaken < 1000) { Chris@17: $timeTaken = round($timeTaken); Chris@17: echo " in {$timeTaken}ms".PHP_EOL; Chris@17: } else { Chris@17: $timeTaken = round(($timeTaken / 1000), 2); Chris@17: echo " in $timeTaken secs".PHP_EOL; Chris@17: } Chris@17: } Chris@17: Chris@17: if ($fixed === true) { Chris@17: // The filename in the report may be truncated due to a basepath setting Chris@17: // but we are using it for writing here and not display, Chris@17: // so find the correct path if basepath is in use. Chris@17: $newFilename = $report['filename'].$phpcsFile->config->suffix; Chris@17: if ($phpcsFile->config->basepath !== null) { Chris@17: $newFilename = $phpcsFile->config->basepath.DIRECTORY_SEPARATOR.$newFilename; Chris@17: } Chris@17: Chris@17: $newContent = $phpcsFile->fixer->getContents(); Chris@17: file_put_contents($newFilename, $newContent); Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 0) { Chris@17: if ($newFilename === $report['filename']) { Chris@17: echo "\t=> File was overwritten".PHP_EOL; Chris@17: } else { Chris@17: echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL; Chris@17: } Chris@17: } Chris@17: } Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 0) { Chris@17: ob_start(); Chris@17: } Chris@17: Chris@17: $errorCount = $phpcsFile->getErrorCount(); Chris@17: $warningCount = $phpcsFile->getWarningCount(); Chris@17: $fixableCount = $phpcsFile->getFixableCount(); Chris@17: $fixedCount = ($errors - $fixableCount); Chris@17: echo $report['filename'].">>$errorCount>>$warningCount>>$fixableCount>>$fixedCount".PHP_EOL; Chris@17: Chris@17: return $fixed; Chris@17: Chris@17: }//end generateFileReport() Chris@17: Chris@17: Chris@17: /** Chris@17: * Prints a summary of fixed files. 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: $lines = explode(PHP_EOL, $cachedData); Chris@17: array_pop($lines); Chris@17: Chris@17: if (empty($lines) === true) { Chris@17: echo PHP_EOL.'No fixable errors were found'.PHP_EOL; Chris@17: return; Chris@17: } Chris@17: Chris@17: $reportFiles = []; Chris@17: $maxLength = 0; Chris@17: $totalFixed = 0; Chris@17: $failures = 0; Chris@17: Chris@17: foreach ($lines as $line) { Chris@17: $parts = explode('>>', $line); Chris@17: $fileLen = strlen($parts[0]); Chris@17: $reportFiles[$parts[0]] = [ Chris@17: 'errors' => $parts[1], Chris@17: 'warnings' => $parts[2], Chris@17: 'fixable' => $parts[3], Chris@17: 'fixed' => $parts[4], Chris@17: 'strlen' => $fileLen, Chris@17: ]; Chris@17: Chris@17: $maxLength = max($maxLength, $fileLen); Chris@17: Chris@17: $totalFixed += $parts[4]; Chris@17: Chris@17: if ($parts[3] > 0) { Chris@17: $failures++; Chris@17: } Chris@17: } Chris@17: Chris@17: $width = min($width, ($maxLength + 21)); Chris@17: $width = max($width, 70); Chris@17: Chris@17: echo PHP_EOL."\033[1m".'PHPCBF RESULT SUMMARY'."\033[0m".PHP_EOL; Chris@17: echo str_repeat('-', $width).PHP_EOL; Chris@17: echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'FIXED REMAINING'."\033[0m".PHP_EOL; Chris@17: echo str_repeat('-', $width).PHP_EOL; Chris@17: Chris@17: foreach ($reportFiles as $file => $data) { Chris@17: $padding = ($width - 18 - $data['strlen']); Chris@17: if ($padding < 0) { Chris@17: $file = '...'.substr($file, (($padding * -1) + 3)); Chris@17: $padding = 0; Chris@17: } Chris@17: Chris@17: echo $file.str_repeat(' ', $padding).' '; Chris@17: Chris@17: if ($data['fixable'] > 0) { Chris@17: echo "\033[31mFAILED TO FIX\033[0m".PHP_EOL; Chris@17: continue; Chris@17: } Chris@17: Chris@17: $remaining = ($data['errors'] + $data['warnings']); Chris@17: Chris@17: if ($data['fixed'] !== 0) { Chris@17: echo $data['fixed']; Chris@17: echo str_repeat(' ', (7 - strlen((string) $data['fixed']))); Chris@17: } else { Chris@17: echo '0 '; Chris@17: } Chris@17: Chris@17: if ($remaining !== 0) { Chris@17: echo $remaining; Chris@17: } else { Chris@17: echo '0'; Chris@17: } Chris@17: Chris@17: echo PHP_EOL; Chris@17: }//end foreach Chris@17: Chris@17: echo str_repeat('-', $width).PHP_EOL; Chris@17: echo "\033[1mA TOTAL OF $totalFixed ERROR"; Chris@17: if ($totalFixed !== 1) { Chris@17: echo 'S'; Chris@17: } Chris@17: Chris@17: $numFiles = count($reportFiles); Chris@17: echo ' WERE FIXED IN '.$numFiles.' FILE'; Chris@17: if ($numFiles !== 1) { Chris@17: echo 'S'; Chris@17: } Chris@17: Chris@17: echo "\033[0m"; Chris@17: Chris@17: if ($failures > 0) { Chris@17: echo PHP_EOL.str_repeat('-', $width).PHP_EOL; Chris@17: echo "\033[1mPHPCBF FAILED TO FIX $failures FILE"; Chris@17: if ($failures !== 1) { Chris@17: echo 'S'; Chris@17: } Chris@17: Chris@17: echo "\033[0m"; Chris@17: } Chris@17: Chris@17: echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; 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