1<?php
2/**
3 * Checkstyle 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 * Checkstyle 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_Checkstyle 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
68        foreach ($report['messages'] as $line => $lineErrors) {
69            foreach ($lineErrors as $column => $colErrors) {
70                foreach ($colErrors as $error) {
71                    $error['type'] = strtolower($error['type']);
72                    if (PHP_CODESNIFFER_ENCODING !== 'utf-8') {
73                        $error['message'] = iconv(PHP_CODESNIFFER_ENCODING, 'utf-8', $error['message']);
74                    }
75
76                    $out->startElement('error');
77                    $out->writeAttribute('line', $line);
78                    $out->writeAttribute('column', $column);
79                    $out->writeAttribute('severity', $error['type']);
80                    $out->writeAttribute('message', $error['message']);
81                    $out->writeAttribute('source', $error['source']);
82                    $out->endElement();
83                }
84            }
85        }//end foreach
86
87        $out->endElement();
88        echo $out->flush();
89
90        return true;
91
92    }//end generateFileReport()
93
94
95    /**
96     * Prints all violations for processed files, in a Checkstyle format.
97     *
98     * @param string  $cachedData    Any partial report data that was returned from
99     *                               generateFileReport during the run.
100     * @param int     $totalFiles    Total number of files processed during the run.
101     * @param int     $totalErrors   Total number of errors found during the run.
102     * @param int     $totalWarnings Total number of warnings found during the run.
103     * @param int     $totalFixable  Total number of problems that can be fixed.
104     * @param boolean $showSources   Show sources?
105     * @param int     $width         Maximum allowed line width.
106     * @param boolean $toScreen      Is the report being printed to screen?
107     *
108     * @return void
109     */
110    public function generate(
111        $cachedData,
112        $totalFiles,
113        $totalErrors,
114        $totalWarnings,
115        $totalFixable,
116        $showSources=false,
117        $width=80,
118        $toScreen=true
119    ) {
120        echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
121        echo '<checkstyle version="'.PHP_CodeSniffer::VERSION.'">'.PHP_EOL;
122        echo $cachedData;
123        echo '</checkstyle>'.PHP_EOL;
124
125    }//end generate()
126
127
128}//end class
129