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