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