1<?php
2/**
3 * Json report for PHP_CodeSniffer.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Jeffrey Fisher <jeffslofish@gmail.com>
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 * Json report for PHP_CodeSniffer.
18 *
19 * PHP version 5
20 *
21 * @category  PHP
22 * @package   PHP_CodeSniffer
23 * @author    Jeffrey Fisher <jeffslofish@gmail.com>
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_Json implements PHP_CodeSniffer_Report
31{
32
33
34    /**
35     * Generate a partial report for a single processed file.
36     *
37     * Function should return TRUE if it printed or stored data about the file
38     * and FALSE if it ignored the file. Returning TRUE indicates that the file and
39     * its data should be counted in the grand totals.
40     *
41     * @param array                $report      Prepared report data.
42     * @param PHP_CodeSniffer_File $phpcsFile   The file being reported on.
43     * @param boolean              $showSources Show sources?
44     * @param int                  $width       Maximum allowed line width.
45     *
46     * @return boolean
47     */
48    public function generateFileReport(
49        $report,
50        PHP_CodeSniffer_File $phpcsFile,
51        $showSources=false,
52        $width=80
53    ) {
54        $filename = str_replace('\\', '\\\\', $report['filename']);
55        $filename = str_replace('"', '\"', $filename);
56        $filename = str_replace('/', '\/', $filename);
57        echo '"'.$filename.'":{';
58        echo '"errors":'.$report['errors'].',"warnings":'.$report['warnings'].',"messages":[';
59
60        $messages = '';
61        foreach ($report['messages'] as $line => $lineErrors) {
62            foreach ($lineErrors as $column => $colErrors) {
63                foreach ($colErrors as $error) {
64                    $error['message'] = str_replace('\\', '\\\\', $error['message']);
65                    $error['message'] = str_replace('"', '\"', $error['message']);
66                    $error['message'] = str_replace('/', '\/', $error['message']);
67                    $error['message'] = str_replace("\n", '\n', $error['message']);
68                    $error['message'] = str_replace("\r", '\r', $error['message']);
69                    $error['message'] = str_replace("\t", '\t', $error['message']);
70
71                    $fixable = 'false';
72                    if ($error['fixable'] === true) {
73                        $fixable = 'true';
74                    }
75
76                    $messages .= '{"message":"'.$error['message'].'",';
77                    $messages .= '"source":"'.$error['source'].'",';
78                    $messages .= '"severity":'.$error['severity'].',';
79                    $messages .= '"type":"'.$error['type'].'",';
80                    $messages .= '"line":'.$line.',';
81                    $messages .= '"column":'.$column.',';
82                    $messages .= '"fixable":'.$fixable;
83                    $messages .= '},';
84                }//end foreach
85            }//end foreach
86        }//end foreach
87
88        echo rtrim($messages, ',');
89        echo ']},';
90
91        return true;
92
93    }//end generateFileReport()
94
95
96    /**
97     * Generates a JSON report.
98     *
99     * @param string  $cachedData    Any partial report data that was returned from
100     *                               generateFileReport during the run.
101     * @param int     $totalFiles    Total number of files processed during the run.
102     * @param int     $totalErrors   Total number of errors found during the run.
103     * @param int     $totalWarnings Total number of warnings found during the run.
104     * @param int     $totalFixable  Total number of problems that can be fixed.
105     * @param boolean $showSources   Show sources?
106     * @param int     $width         Maximum allowed line width.
107     * @param boolean $toScreen      Is the report being printed to screen?
108     *
109     * @return void
110     */
111    public function generate(
112        $cachedData,
113        $totalFiles,
114        $totalErrors,
115        $totalWarnings,
116        $totalFixable,
117        $showSources=false,
118        $width=80,
119        $toScreen=true
120    ) {
121        echo '{"totals":{"errors":'.$totalErrors.',"warnings":'.$totalWarnings.',"fixable":'.$totalFixable.'},"files":{';
122        echo rtrim($cachedData, ',');
123        echo "}}";
124
125    }//end generate()
126
127
128}//end class
129