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