annotate vendor/squizlabs/php_codesniffer/src/Reports/Junit.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@4 1 <?php
Chris@4 2 /**
Chris@4 3 * JUnit report for PHP_CodeSniffer.
Chris@4 4 *
Chris@4 5 * @author Oleg Lobach <oleg@lobach.info>
Chris@4 6 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@4 7 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@4 8 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@4 9 */
Chris@4 10
Chris@4 11 namespace PHP_CodeSniffer\Reports;
Chris@4 12
Chris@4 13 use PHP_CodeSniffer\Config;
Chris@4 14 use PHP_CodeSniffer\Files\File;
Chris@4 15
Chris@4 16 class Junit implements Report
Chris@4 17 {
Chris@4 18
Chris@4 19
Chris@4 20 /**
Chris@4 21 * Generate a partial report for a single processed file.
Chris@4 22 *
Chris@4 23 * Function should return TRUE if it printed or stored data about the file
Chris@4 24 * and FALSE if it ignored the file. Returning TRUE indicates that the file and
Chris@4 25 * its data should be counted in the grand totals.
Chris@4 26 *
Chris@4 27 * @param array $report Prepared report data.
Chris@4 28 * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on.
Chris@4 29 * @param bool $showSources Show sources?
Chris@4 30 * @param int $width Maximum allowed line width.
Chris@4 31 *
Chris@4 32 * @return bool
Chris@4 33 */
Chris@4 34 public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
Chris@4 35 {
Chris@4 36 $out = new \XMLWriter;
Chris@4 37 $out->openMemory();
Chris@4 38 $out->setIndent(true);
Chris@4 39
Chris@4 40 $out->startElement('testsuite');
Chris@4 41 $out->writeAttribute('name', $report['filename']);
Chris@4 42 $out->writeAttribute('errors', 0);
Chris@4 43
Chris@4 44 if (count($report['messages']) === 0) {
Chris@4 45 $out->writeAttribute('tests', 1);
Chris@4 46 $out->writeAttribute('failures', 0);
Chris@4 47
Chris@4 48 $out->startElement('testcase');
Chris@4 49 $out->writeAttribute('name', $report['filename']);
Chris@4 50 $out->endElement();
Chris@4 51 } else {
Chris@4 52 $failures = ($report['errors'] + $report['warnings']);
Chris@4 53 $out->writeAttribute('tests', $failures);
Chris@4 54 $out->writeAttribute('failures', $failures);
Chris@4 55
Chris@4 56 foreach ($report['messages'] as $line => $lineErrors) {
Chris@4 57 foreach ($lineErrors as $column => $colErrors) {
Chris@4 58 foreach ($colErrors as $error) {
Chris@4 59 $out->startElement('testcase');
Chris@4 60 $out->writeAttribute('name', $error['source'].' at '.$report['filename']." ($line:$column)");
Chris@4 61
Chris@4 62 $error['type'] = strtolower($error['type']);
Chris@4 63 if ($phpcsFile->config->encoding !== 'utf-8') {
Chris@4 64 $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']);
Chris@4 65 }
Chris@4 66
Chris@4 67 $out->startElement('failure');
Chris@4 68 $out->writeAttribute('type', $error['type']);
Chris@4 69 $out->writeAttribute('message', $error['message']);
Chris@4 70 $out->endElement();
Chris@4 71
Chris@4 72 $out->endElement();
Chris@4 73 }
Chris@4 74 }
Chris@4 75 }
Chris@4 76 }//end if
Chris@4 77
Chris@4 78 $out->endElement();
Chris@4 79 echo $out->flush();
Chris@4 80 return true;
Chris@4 81
Chris@4 82 }//end generateFileReport()
Chris@4 83
Chris@4 84
Chris@4 85 /**
Chris@4 86 * Prints all violations for processed files, in a proprietary XML format.
Chris@4 87 *
Chris@4 88 * @param string $cachedData Any partial report data that was returned from
Chris@4 89 * generateFileReport during the run.
Chris@4 90 * @param int $totalFiles Total number of files processed during the run.
Chris@4 91 * @param int $totalErrors Total number of errors found during the run.
Chris@4 92 * @param int $totalWarnings Total number of warnings found during the run.
Chris@4 93 * @param int $totalFixable Total number of problems that can be fixed.
Chris@4 94 * @param bool $showSources Show sources?
Chris@4 95 * @param int $width Maximum allowed line width.
Chris@4 96 * @param bool $interactive Are we running in interactive mode?
Chris@4 97 * @param bool $toScreen Is the report being printed to screen?
Chris@4 98 *
Chris@4 99 * @return void
Chris@4 100 */
Chris@4 101 public function generate(
Chris@4 102 $cachedData,
Chris@4 103 $totalFiles,
Chris@4 104 $totalErrors,
Chris@4 105 $totalWarnings,
Chris@4 106 $totalFixable,
Chris@4 107 $showSources=false,
Chris@4 108 $width=80,
Chris@4 109 $interactive=false,
Chris@4 110 $toScreen=true
Chris@4 111 ) {
Chris@4 112 // Figure out the total number of tests.
Chris@4 113 $tests = 0;
Chris@4 114 $matches = [];
Chris@4 115 preg_match_all('/tests="([0-9]+)"/', $cachedData, $matches);
Chris@4 116 if (isset($matches[1]) === true) {
Chris@4 117 foreach ($matches[1] as $match) {
Chris@4 118 $tests += $match;
Chris@4 119 }
Chris@4 120 }
Chris@4 121
Chris@4 122 $failures = ($totalErrors + $totalWarnings);
Chris@4 123 echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
Chris@4 124 echo '<testsuites name="PHP_CodeSniffer '.Config::VERSION.'" errors="0" tests="'.$tests.'" failures="'.$failures.'">'.PHP_EOL;
Chris@4 125 echo $cachedData;
Chris@4 126 echo '</testsuites>'.PHP_EOL;
Chris@4 127
Chris@4 128 }//end generate()
Chris@4 129
Chris@4 130
Chris@4 131 }//end class