Chris@17: Chris@17: * @author Greg Sherwood 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 Json 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: $filename = str_replace('\\', '\\\\', $report['filename']); Chris@17: $filename = str_replace('"', '\"', $filename); Chris@17: $filename = str_replace('/', '\/', $filename); Chris@17: echo '"'.$filename.'":{'; Chris@17: echo '"errors":'.$report['errors'].',"warnings":'.$report['warnings'].',"messages":['; Chris@17: Chris@17: $messages = ''; Chris@17: foreach ($report['messages'] as $line => $lineErrors) { Chris@17: foreach ($lineErrors as $column => $colErrors) { Chris@17: foreach ($colErrors as $error) { Chris@17: $error['message'] = str_replace("\n", '\n', $error['message']); Chris@17: $error['message'] = str_replace("\r", '\r', $error['message']); Chris@17: $error['message'] = str_replace("\t", '\t', $error['message']); Chris@17: Chris@17: $fixable = false; Chris@17: if ($error['fixable'] === true) { Chris@17: $fixable = true; Chris@17: } Chris@17: Chris@17: $messagesObject = (object) $error; Chris@17: $messagesObject->line = $line; Chris@17: $messagesObject->column = $column; Chris@17: $messagesObject->fixable = $fixable; Chris@17: Chris@17: $messages .= json_encode($messagesObject).","; Chris@17: } Chris@17: } Chris@17: }//end foreach Chris@17: Chris@17: echo rtrim($messages, ','); Chris@17: echo ']},'; Chris@17: Chris@17: return true; Chris@17: Chris@17: }//end generateFileReport() Chris@17: Chris@17: Chris@17: /** Chris@17: * Generates a JSON report. 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 '{"totals":{"errors":'.$totalErrors.',"warnings":'.$totalWarnings.',"fixable":'.$totalFixable.'},"files":{'; Chris@17: echo rtrim($cachedData, ','); Chris@17: echo "}}"; Chris@17: Chris@17: }//end generate() Chris@17: Chris@17: Chris@17: }//end class