annotate vendor/squizlabs/php_codesniffer/src/Reports/Xml.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 * XML report for PHP_CodeSniffer.
Chris@4 4 *
Chris@4 5 * @author Greg Sherwood <gsherwood@squiz.net>
Chris@4 6 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
Chris@4 7 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
Chris@4 8 */
Chris@4 9
Chris@4 10 namespace PHP_CodeSniffer\Reports;
Chris@4 11
Chris@4 12 use PHP_CodeSniffer\Config;
Chris@4 13 use PHP_CodeSniffer\Files\File;
Chris@4 14
Chris@4 15 class Xml implements Report
Chris@4 16 {
Chris@4 17
Chris@4 18
Chris@4 19 /**
Chris@4 20 * Generate a partial report for a single processed file.
Chris@4 21 *
Chris@4 22 * Function should return TRUE if it printed or stored data about the file
Chris@4 23 * and FALSE if it ignored the file. Returning TRUE indicates that the file and
Chris@4 24 * its data should be counted in the grand totals.
Chris@4 25 *
Chris@4 26 * @param array $report Prepared report data.
Chris@4 27 * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on.
Chris@4 28 * @param bool $showSources Show sources?
Chris@4 29 * @param int $width Maximum allowed line width.
Chris@4 30 *
Chris@4 31 * @return bool
Chris@4 32 */
Chris@4 33 public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
Chris@4 34 {
Chris@4 35 $out = new \XMLWriter;
Chris@4 36 $out->openMemory();
Chris@4 37 $out->setIndent(true);
Chris@4 38 $out->setIndentString(' ');
Chris@4 39 $out->startDocument('1.0', 'UTF-8');
Chris@4 40
Chris@4 41 if ($report['errors'] === 0 && $report['warnings'] === 0) {
Chris@4 42 // Nothing to print.
Chris@4 43 return false;
Chris@4 44 }
Chris@4 45
Chris@4 46 $out->startElement('file');
Chris@4 47 $out->writeAttribute('name', $report['filename']);
Chris@4 48 $out->writeAttribute('errors', $report['errors']);
Chris@4 49 $out->writeAttribute('warnings', $report['warnings']);
Chris@4 50 $out->writeAttribute('fixable', $report['fixable']);
Chris@4 51
Chris@4 52 foreach ($report['messages'] as $line => $lineErrors) {
Chris@4 53 foreach ($lineErrors as $column => $colErrors) {
Chris@4 54 foreach ($colErrors as $error) {
Chris@4 55 $error['type'] = strtolower($error['type']);
Chris@4 56 if ($phpcsFile->config->encoding !== 'utf-8') {
Chris@4 57 $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']);
Chris@4 58 }
Chris@4 59
Chris@4 60 $out->startElement($error['type']);
Chris@4 61 $out->writeAttribute('line', $line);
Chris@4 62 $out->writeAttribute('column', $column);
Chris@4 63 $out->writeAttribute('source', $error['source']);
Chris@4 64 $out->writeAttribute('severity', $error['severity']);
Chris@4 65 $out->writeAttribute('fixable', (int) $error['fixable']);
Chris@4 66 $out->text($error['message']);
Chris@4 67 $out->endElement();
Chris@4 68 }
Chris@4 69 }
Chris@4 70 }//end foreach
Chris@4 71
Chris@4 72 $out->endElement();
Chris@4 73
Chris@4 74 // Remove the start of the document because we will
Chris@4 75 // add that manually later. We only have it in here to
Chris@4 76 // properly set the encoding.
Chris@4 77 $content = $out->flush();
Chris@4 78 $content = substr($content, (strpos($content, PHP_EOL) + strlen(PHP_EOL)));
Chris@4 79 echo $content;
Chris@4 80
Chris@4 81 return true;
Chris@4 82
Chris@4 83 }//end generateFileReport()
Chris@4 84
Chris@4 85
Chris@4 86 /**
Chris@4 87 * Prints all violations for processed files, in a proprietary XML format.
Chris@4 88 *
Chris@4 89 * @param string $cachedData Any partial report data that was returned from
Chris@4 90 * generateFileReport during the run.
Chris@4 91 * @param int $totalFiles Total number of files processed during the run.
Chris@4 92 * @param int $totalErrors Total number of errors found during the run.
Chris@4 93 * @param int $totalWarnings Total number of warnings found during the run.
Chris@4 94 * @param int $totalFixable Total number of problems that can be fixed.
Chris@4 95 * @param bool $showSources Show sources?
Chris@4 96 * @param int $width Maximum allowed line width.
Chris@4 97 * @param bool $interactive Are we running in interactive mode?
Chris@4 98 * @param bool $toScreen Is the report being printed to screen?
Chris@4 99 *
Chris@4 100 * @return void
Chris@4 101 */
Chris@4 102 public function generate(
Chris@4 103 $cachedData,
Chris@4 104 $totalFiles,
Chris@4 105 $totalErrors,
Chris@4 106 $totalWarnings,
Chris@4 107 $totalFixable,
Chris@4 108 $showSources=false,
Chris@4 109 $width=80,
Chris@4 110 $interactive=false,
Chris@4 111 $toScreen=true
Chris@4 112 ) {
Chris@4 113 echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
Chris@4 114 echo '<phpcs version="'.Config::VERSION.'">'.PHP_EOL;
Chris@4 115 echo $cachedData;
Chris@4 116 echo '</phpcs>'.PHP_EOL;
Chris@4 117
Chris@4 118 }//end generate()
Chris@4 119
Chris@4 120
Chris@4 121 }//end class