1<?php
2/**
3 * Csv 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 * Csv 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_Csv 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        if ($report['errors'] === 0 && $report['warnings'] === 0) {
57            // Nothing to print.
58            return false;
59        }
60
61        foreach ($report['messages'] as $line => $lineErrors) {
62            foreach ($lineErrors as $column => $colErrors) {
63                foreach ($colErrors as $error) {
64                    $filename = str_replace('"', '\"', $report['filename']);
65                    $message  = str_replace('"', '\"', $error['message']);
66                    $type     = strtolower($error['type']);
67                    $source   = $error['source'];
68                    $severity = $error['severity'];
69                    $fixable  = (int) $error['fixable'];
70                    echo "\"$filename\",$line,$column,$type,\"$message\",$source,$severity,$fixable".PHP_EOL;
71                }
72            }
73        }
74
75        return true;
76
77    }//end generateFileReport()
78
79
80    /**
81     * Generates a csv report.
82     *
83     * @param string  $cachedData    Any partial report data that was returned from
84     *                               generateFileReport during the run.
85     * @param int     $totalFiles    Total number of files processed during the run.
86     * @param int     $totalErrors   Total number of errors found during the run.
87     * @param int     $totalWarnings Total number of warnings found during the run.
88     * @param int     $totalFixable  Total number of problems that can be fixed.
89     * @param boolean $showSources   Show sources?
90     * @param int     $width         Maximum allowed line width.
91     * @param boolean $toScreen      Is the report being printed to screen?
92     *
93     * @return void
94     */
95    public function generate(
96        $cachedData,
97        $totalFiles,
98        $totalErrors,
99        $totalWarnings,
100        $totalFixable,
101        $showSources=false,
102        $width=80,
103        $toScreen=true
104    ) {
105        echo 'File,Line,Column,Type,Message,Source,Severity,Fixable'.PHP_EOL;
106        echo $cachedData;
107
108    }//end generate()
109
110
111}//end class
112