1<?php
2/**
3 * Summary 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 * Summary 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_Summary implements PHP_CodeSniffer_Report
33{
34
35    /**
36     * TRUE if this report needs error messages instead of just totals.
37     *
38     * @var boolean
39     */
40    public $recordErrors = false;
41
42    /**
43     * An array of process files and their error data.
44     *
45     * @var boolean
46     */
47    private $_reportFiles = array();
48
49
50    /**
51     * Generate a partial report for a single processed file.
52     *
53     * Function should return TRUE if it printed or stored data about the file
54     * and FALSE if it ignored the file. Returning TRUE indicates that the file and
55     * its data should be counted in the grand totals.
56     *
57     * @param array                $report      Prepared report data.
58     * @param PHP_CodeSniffer_File $phpcsFile   The file being reported on.
59     * @param boolean              $showSources Show sources?
60     * @param int                  $width       Maximum allowed line width.
61     *
62     * @return boolean
63     */
64    public function generateFileReport(
65        $report,
66        PHP_CodeSniffer_File $phpcsFile,
67        $showSources=false,
68        $width=80
69    ) {
70        if (PHP_CODESNIFFER_VERBOSITY === 0
71            && $report['errors'] === 0
72            && $report['warnings'] === 0
73        ) {
74            // Nothing to print.
75            return false;
76        }
77
78        $this->_reportFiles[$report['filename']] = array(
79                                                    'errors'   => $report['errors'],
80                                                    'warnings' => $report['warnings'],
81                                                    'strlen'   => strlen($report['filename']),
82                                                   );
83
84        return true;
85
86    }//end generateFileReport()
87
88
89    /**
90     * Generates a summary of errors and warnings for each file processed.
91     *
92     * @param string  $cachedData    Any partial report data that was returned from
93     *                               generateFileReport during the run.
94     * @param int     $totalFiles    Total number of files processed during the run.
95     * @param int     $totalErrors   Total number of errors found during the run.
96     * @param int     $totalWarnings Total number of warnings found during the run.
97     * @param int     $totalFixable  Total number of problems that can be fixed.
98     * @param boolean $showSources   Show sources?
99     * @param int     $width         Maximum allowed line width.
100     * @param boolean $toScreen      Is the report being printed to screen?
101     *
102     * @return void
103     */
104    public function generate(
105        $cachedData,
106        $totalFiles,
107        $totalErrors,
108        $totalWarnings,
109        $totalFixable,
110        $showSources=false,
111        $width=80,
112        $toScreen=true
113    ) {
114
115        if (empty($this->_reportFiles) === true) {
116            return;
117        }
118
119        // Make sure the report width isn't too big.
120        $maxLength = 0;
121        foreach ($this->_reportFiles as $file => $data) {
122            $maxLength = max($maxLength, $data['strlen']);
123        }
124
125        $width = min($width, ($maxLength + 21));
126        $width = max($width, 70);
127
128        echo PHP_EOL."\033[1m".'PHP CODE SNIFFER REPORT SUMMARY'."\033[0m".PHP_EOL;
129        echo str_repeat('-', $width).PHP_EOL;
130        echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'ERRORS  WARNINGS'."\033[0m".PHP_EOL;
131        echo str_repeat('-', $width).PHP_EOL;
132
133        foreach ($this->_reportFiles as $file => $data) {
134            $padding = ($width - 18 - $data['strlen']);
135            if ($padding < 0) {
136                $file    = '...'.substr($file, (($padding * -1) + 3));
137                $padding = 0;
138            }
139
140            echo $file.str_repeat(' ', $padding).'  ';
141            if ($data['errors'] !== 0) {
142                echo "\033[31m".$data['errors']."\033[0m";
143                echo str_repeat(' ', (8 - strlen((string) $data['errors'])));
144            } else {
145                echo '0       ';
146            }
147
148            if ($data['warnings'] !== 0) {
149                echo "\033[33m".$data['warnings']."\033[0m";
150            } else {
151                echo '0';
152            }
153
154            echo PHP_EOL;
155        }//end foreach
156
157        echo str_repeat('-', $width).PHP_EOL;
158        echo "\033[1mA TOTAL OF $totalErrors ERROR";
159        if ($totalErrors !== 1) {
160            echo 'S';
161        }
162
163        echo ' AND '.$totalWarnings.' WARNING';
164        if ($totalWarnings !== 1) {
165            echo 'S';
166        }
167
168        echo ' WERE FOUND IN '.$totalFiles.' FILE';
169        if ($totalFiles !== 1) {
170            echo 'S';
171        }
172
173        echo "\033[0m";
174
175        if ($totalFixable > 0) {
176            echo PHP_EOL.str_repeat('-', $width).PHP_EOL;
177            echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m";
178        }
179
180        echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL;
181
182        if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
183            PHP_CodeSniffer_Reporting::printRunTime();
184        }
185
186    }//end generate()
187
188
189}//end class
190