Chris@0: Chris@0: * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600) Chris@0: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: Chris@0: /** Chris@0: * CBF report for PHP_CodeSniffer. Chris@0: * Chris@0: * This report implements the various auto-fixing features of the Chris@0: * PHPCBF script and is not intended (or allowed) to be selected as a Chris@0: * report from the command line. Chris@0: * Chris@0: * PHP version 5 Chris@0: * Chris@0: * @category PHP Chris@0: * @package PHP_CodeSniffer Chris@0: * @author Greg Sherwood Chris@0: * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600) Chris@0: * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence Chris@0: * @version Release: @package_version@ Chris@0: * @link http://pear.php.net/package/PHP_CodeSniffer Chris@0: */ Chris@0: class PHP_CodeSniffer_Reports_Cbf implements PHP_CodeSniffer_Report Chris@0: { Chris@0: Chris@0: Chris@0: /** Chris@0: * Generate a partial report for a single processed file. Chris@0: * Chris@0: * Function should return TRUE if it printed or stored data about the file Chris@0: * and FALSE if it ignored the file. Returning TRUE indicates that the file and Chris@0: * its data should be counted in the grand totals. Chris@0: * Chris@0: * @param array $report Prepared report data. Chris@0: * @param PHP_CodeSniffer_File $phpcsFile The file being reported on. Chris@0: * @param boolean $showSources Show sources? Chris@0: * @param int $width Maximum allowed line width. Chris@0: * Chris@0: * @return boolean Chris@0: */ Chris@0: public function generateFileReport( Chris@0: $report, Chris@0: PHP_CodeSniffer_File $phpcsFile, Chris@0: $showSources=false, Chris@0: $width=80 Chris@0: ) { Chris@0: $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); Chris@0: $errors = $phpcsFile->getFixableCount(); Chris@0: if ($errors !== 0) { Chris@0: if (empty($cliValues['files']) === false) { Chris@0: ob_end_clean(); Chris@0: $errors = $phpcsFile->getFixableCount(); Chris@0: $startTime = microtime(true); Chris@0: echo "\t=> Fixing file: $errors/$errors violations remaining"; Chris@0: } Chris@0: Chris@0: $fixed = $phpcsFile->fixer->fixFile(); Chris@0: } Chris@0: Chris@0: if (empty($cliValues['files']) === true) { Chris@0: // Replacing STDIN, so output current file to STDOUT Chris@0: // even if nothing was fixed. Exit here because we Chris@0: // can't process any more than 1 file in this setup. Chris@0: echo $phpcsFile->fixer->getContents(); Chris@0: ob_end_flush(); Chris@0: exit(1); Chris@0: } Chris@0: Chris@0: if ($errors === 0) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: if ($fixed === false) { Chris@0: echo 'ERROR'; Chris@0: } else { Chris@0: echo 'DONE'; Chris@0: } Chris@0: Chris@0: $timeTaken = ((microtime(true) - $startTime) * 1000); Chris@0: if ($timeTaken < 1000) { Chris@0: $timeTaken = round($timeTaken); Chris@0: echo " in {$timeTaken}ms".PHP_EOL; Chris@0: } else { Chris@0: $timeTaken = round(($timeTaken / 1000), 2); Chris@0: echo " in $timeTaken secs".PHP_EOL; Chris@0: } Chris@0: Chris@0: if ($fixed === true) { Chris@0: $newFilename = $report['filename'].$cliValues['phpcbf-suffix']; Chris@0: $newContent = $phpcsFile->fixer->getContents(); Chris@0: file_put_contents($newFilename, $newContent); Chris@0: Chris@0: if ($newFilename === $report['filename']) { Chris@0: echo "\t=> File was overwritten".PHP_EOL; Chris@0: } else { Chris@0: echo "\t=> Fixed file written to ".basename($newFilename).PHP_EOL; Chris@0: } Chris@0: } Chris@0: Chris@0: ob_start(); Chris@0: Chris@0: return $fixed; Chris@0: Chris@0: }//end generateFileReport() Chris@0: Chris@0: Chris@0: /** Chris@0: * Prints all errors and warnings for each file processed. Chris@0: * Chris@0: * @param string $cachedData Any partial report data that was returned from Chris@0: * generateFileReport during the run. Chris@0: * @param int $totalFiles Total number of files processed during the run. Chris@0: * @param int $totalErrors Total number of errors found during the run. Chris@0: * @param int $totalWarnings Total number of warnings found during the run. Chris@0: * @param int $totalFixable Total number of problems that can be fixed. Chris@0: * @param boolean $showSources Show sources? Chris@0: * @param int $width Maximum allowed line width. Chris@0: * @param boolean $toScreen Is the report being printed to screen? Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function generate( Chris@0: $cachedData, Chris@0: $totalFiles, Chris@0: $totalErrors, Chris@0: $totalWarnings, Chris@0: $totalFixable, Chris@0: $showSources=false, Chris@0: $width=80, Chris@0: $toScreen=true Chris@0: ) { Chris@0: echo $cachedData; Chris@0: echo "Fixed $totalFiles files".PHP_EOL; Chris@0: Chris@0: }//end generate() Chris@0: Chris@0: Chris@0: }//end class