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: Chris@17: class Diff 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: return false; Chris@17: } Chris@17: Chris@17: $phpcsFile->disableCaching(); 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 'DIFF report is parsing '.basename($report['filename']).' '; Chris@17: } else if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo 'DIFF report is forcing parse of '.$report['filename'].PHP_EOL; Chris@17: } Chris@17: Chris@17: $phpcsFile->parse(); 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: $phpcsFile->fixer->startFile($phpcsFile); Chris@17: }//end if Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: ob_end_clean(); Chris@17: echo "\t*** START FILE FIXING ***".PHP_EOL; Chris@17: } Chris@17: Chris@17: $fixed = $phpcsFile->fixer->fixFile(); Chris@17: Chris@17: if (PHP_CODESNIFFER_VERBOSITY > 1) { Chris@17: echo "\t*** END FILE FIXING ***".PHP_EOL; Chris@17: ob_start(); Chris@17: } Chris@17: Chris@17: if ($fixed === false) { Chris@17: return false; Chris@17: } Chris@17: Chris@17: $diff = $phpcsFile->fixer->generateDiff(); Chris@17: if ($diff === '') { Chris@17: // Nothing to print. Chris@17: return false; Chris@17: } Chris@17: Chris@17: echo $diff.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: echo $cachedData; Chris@17: if ($toScreen === true && $cachedData !== '') { Chris@17: echo PHP_EOL; Chris@17: } Chris@17: Chris@17: }//end generate() Chris@17: Chris@17: Chris@17: }//end class