1<?php
2/**
3 * Info report for PHP_CodeSniffer.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
9 * @author    Greg Sherwood <gsherwood@squiz.net>
10 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12 * @link      http://pear.php.net/package/PHP_CodeSniffer
13 */
14
15/**
16 * Info report for PHP_CodeSniffer.
17 *
18 * PHP version 5
19 *
20 * @category  PHP
21 * @package   PHP_CodeSniffer
22 * @author    Greg Sherwood <gsherwood@squiz.net>
23 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
24 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25 * @version   Release: @package_version@
26 * @link      http://pear.php.net/package/PHP_CodeSniffer
27 */
28class PHP_CodeSniffer_Reports_Info implements PHP_CodeSniffer_Report
29{
30
31    /**
32     * TRUE if this report needs error messages instead of just totals.
33     *
34     * @var boolean
35     */
36    public $recordErrors = false;
37
38    /**
39     * A cache of metrics collected during the run.
40     *
41     * @var array
42     */
43    private $_metricCache = array();
44
45
46    /**
47     * Generate a partial report for a single processed file.
48     *
49     * Function should return TRUE if it printed or stored data about the file
50     * and FALSE if it ignored the file. Returning TRUE indicates that the file and
51     * its data should be counted in the grand totals.
52     *
53     * @param array                $report      Prepared report data.
54     * @param PHP_CodeSniffer_File $phpcsFile   The file being reported on.
55     * @param boolean              $showSources Show sources?
56     * @param int                  $width       Maximum allowed line width.
57     *
58     * @return boolean
59     */
60    public function generateFileReport(
61        $report,
62        PHP_CodeSniffer_File $phpcsFile,
63        $showSources=false,
64        $width=80
65    ) {
66        $metrics = $phpcsFile->getMetrics();
67        foreach ($metrics as $metric => $data) {
68            if (isset($this->_metricCache[$metric]) === false) {
69                $this->_metricCache[$metric] = array();
70            }
71
72            foreach ($data['values'] as $value => $locations) {
73                $locations = array_unique($locations);
74                $count     = count($locations);
75
76                if (isset($this->_metricCache[$metric][$value]) === false) {
77                    $this->_metricCache[$metric][$value] = $count;
78                } else {
79                    $this->_metricCache[$metric][$value] += $count;
80                }
81            }
82        }//end foreach
83
84        return true;
85
86    }//end generateFileReport()
87
88
89    /**
90     * Prints the source of all errors and warnings.
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        if (empty($this->_metricCache) === true) {
115            // Nothing to show.
116            return;
117        }
118
119        ksort($this->_metricCache);
120
121        echo PHP_EOL."\033[1m".'PHP CODE SNIFFER INFORMATION REPORT'."\033[0m".PHP_EOL;
122        echo str_repeat('-', 70).PHP_EOL;
123
124        foreach ($this->_metricCache as $metric => $values) {
125            $winner      = '';
126            $winnerCount = 0;
127            $totalCount  = 0;
128            foreach ($values as $value => $count) {
129                $totalCount += $count;
130                if ($count > $winnerCount) {
131                    $winner      = $value;
132                    $winnerCount = $count;
133                }
134            }
135
136            $winPercent = round(($winnerCount / $totalCount * 100), 2);
137            echo "$metric: \033[4m$winner\033[0m [$winnerCount/$totalCount, $winPercent%]".PHP_EOL;
138
139            asort($values);
140            $values = array_reverse($values, true);
141            foreach ($values as $value => $count) {
142                if ($value === $winner) {
143                    continue;
144                }
145
146                $percent = round(($count / $totalCount * 100), 2);
147                echo "\t$value => $count ($percent%)".PHP_EOL;
148            }
149
150            echo PHP_EOL;
151        }//end foreach
152
153        echo str_repeat('-', 70).PHP_EOL;
154
155        if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
156            PHP_CodeSniffer_Reporting::printRunTime();
157        }
158
159    }//end generate()
160
161
162}//end class
163