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