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\Config; Chris@4: use PHP_CodeSniffer\Files\File; Chris@4: Chris@4: class Junit 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: $out = new \XMLWriter; Chris@4: $out->openMemory(); Chris@4: $out->setIndent(true); Chris@4: Chris@4: $out->startElement('testsuite'); Chris@4: $out->writeAttribute('name', $report['filename']); Chris@4: $out->writeAttribute('errors', 0); Chris@4: Chris@4: if (count($report['messages']) === 0) { Chris@4: $out->writeAttribute('tests', 1); Chris@4: $out->writeAttribute('failures', 0); Chris@4: Chris@4: $out->startElement('testcase'); Chris@4: $out->writeAttribute('name', $report['filename']); Chris@4: $out->endElement(); Chris@4: } else { Chris@4: $failures = ($report['errors'] + $report['warnings']); Chris@4: $out->writeAttribute('tests', $failures); Chris@4: $out->writeAttribute('failures', $failures); Chris@4: Chris@4: foreach ($report['messages'] as $line => $lineErrors) { Chris@4: foreach ($lineErrors as $column => $colErrors) { Chris@4: foreach ($colErrors as $error) { Chris@4: $out->startElement('testcase'); Chris@4: $out->writeAttribute('name', $error['source'].' at '.$report['filename']." ($line:$column)"); Chris@4: Chris@4: $error['type'] = strtolower($error['type']); Chris@4: if ($phpcsFile->config->encoding !== 'utf-8') { Chris@4: $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']); Chris@4: } Chris@4: Chris@4: $out->startElement('failure'); Chris@4: $out->writeAttribute('type', $error['type']); Chris@4: $out->writeAttribute('message', $error['message']); Chris@4: $out->endElement(); Chris@4: Chris@4: $out->endElement(); Chris@4: } Chris@4: } Chris@4: } Chris@4: }//end if Chris@4: Chris@4: $out->endElement(); Chris@4: echo $out->flush(); Chris@4: return true; Chris@4: Chris@4: }//end generateFileReport() Chris@4: Chris@4: Chris@4: /** Chris@4: * Prints all violations for processed files, in a proprietary XML format. 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: // Figure out the total number of tests. Chris@4: $tests = 0; Chris@4: $matches = []; Chris@4: preg_match_all('/tests="([0-9]+)"/', $cachedData, $matches); Chris@4: if (isset($matches[1]) === true) { Chris@4: foreach ($matches[1] as $match) { Chris@4: $tests += $match; Chris@4: } Chris@4: } Chris@4: Chris@4: $failures = ($totalErrors + $totalWarnings); Chris@4: echo ''.PHP_EOL; Chris@4: echo ''.PHP_EOL; Chris@4: echo $cachedData; Chris@4: echo ''.PHP_EOL; Chris@4: Chris@4: }//end generate() Chris@4: Chris@4: Chris@4: }//end class