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\Config; Chris@17: use PHP_CodeSniffer\Files\File; Chris@17: Chris@17: class Junit 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: Chris@17: $out->startElement('testsuite'); Chris@17: $out->writeAttribute('name', $report['filename']); Chris@17: $out->writeAttribute('errors', 0); Chris@17: Chris@17: if (count($report['messages']) === 0) { Chris@17: $out->writeAttribute('tests', 1); Chris@17: $out->writeAttribute('failures', 0); Chris@17: Chris@17: $out->startElement('testcase'); Chris@17: $out->writeAttribute('name', $report['filename']); Chris@17: $out->endElement(); Chris@17: } else { Chris@17: $failures = ($report['errors'] + $report['warnings']); Chris@17: $out->writeAttribute('tests', $failures); Chris@17: $out->writeAttribute('failures', $failures); 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: $out->startElement('testcase'); Chris@17: $out->writeAttribute('name', $error['source'].' at '.$report['filename']." ($line:$column)"); Chris@17: 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('failure'); Chris@17: $out->writeAttribute('type', $error['type']); Chris@17: $out->writeAttribute('message', $error['message']); Chris@17: $out->endElement(); Chris@17: Chris@17: $out->endElement(); Chris@17: } Chris@17: } Chris@17: } Chris@17: }//end if Chris@17: Chris@17: $out->endElement(); Chris@17: echo $out->flush(); 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: // Figure out the total number of tests. Chris@17: $tests = 0; Chris@17: $matches = []; Chris@17: preg_match_all('/tests="([0-9]+)"/', $cachedData, $matches); Chris@17: if (isset($matches[1]) === true) { Chris@17: foreach ($matches[1] as $match) { Chris@17: $tests += $match; Chris@17: } Chris@17: } Chris@17: Chris@17: $failures = ($totalErrors + $totalWarnings); 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