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\Config; Chris@17: use PHP_CodeSniffer\Files\File; Chris@17: Chris@17: class Xml 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: $out = new \XMLWriter; Chris@17: $out->openMemory(); Chris@17: $out->setIndent(true); Chris@17: $out->setIndentString(' '); Chris@17: $out->startDocument('1.0', 'UTF-8'); Chris@17: Chris@17: if ($report['errors'] === 0 && $report['warnings'] === 0) { Chris@17: // Nothing to print. Chris@17: return false; Chris@17: } Chris@17: Chris@17: $out->startElement('file'); Chris@17: $out->writeAttribute('name', $report['filename']); Chris@17: $out->writeAttribute('errors', $report['errors']); Chris@17: $out->writeAttribute('warnings', $report['warnings']); Chris@17: $out->writeAttribute('fixable', $report['fixable']); Chris@17: Chris@17: foreach ($report['messages'] as $line => $lineErrors) { Chris@17: foreach ($lineErrors as $column => $colErrors) { Chris@17: foreach ($colErrors as $error) { Chris@17: $error['type'] = strtolower($error['type']); Chris@17: if ($phpcsFile->config->encoding !== 'utf-8') { Chris@17: $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']); Chris@17: } Chris@17: Chris@17: $out->startElement($error['type']); Chris@17: $out->writeAttribute('line', $line); Chris@17: $out->writeAttribute('column', $column); Chris@17: $out->writeAttribute('source', $error['source']); Chris@17: $out->writeAttribute('severity', $error['severity']); Chris@17: $out->writeAttribute('fixable', (int) $error['fixable']); Chris@17: $out->text($error['message']); Chris@17: $out->endElement(); Chris@17: } Chris@17: } Chris@17: }//end foreach Chris@17: Chris@17: $out->endElement(); Chris@17: Chris@17: // Remove the start of the document because we will Chris@17: // add that manually later. We only have it in here to Chris@17: // properly set the encoding. Chris@17: $content = $out->flush(); Chris@17: $content = substr($content, (strpos($content, PHP_EOL) + strlen(PHP_EOL))); Chris@17: echo $content; Chris@17: Chris@17: return true; Chris@17: Chris@17: }//end generateFileReport() Chris@17: Chris@17: Chris@17: /** Chris@17: * Prints all violations for processed files, in a proprietary XML format. 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 ''.PHP_EOL; Chris@17: echo ''.PHP_EOL; Chris@17: echo $cachedData; Chris@17: echo ''.PHP_EOL; Chris@17: Chris@17: }//end generate() Chris@17: Chris@17: Chris@17: }//end class