~ubuntu-branches/ubuntu/vivid/php-codesniffer/vivid

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.5/CodeSniffer/Reports/VersionControl.php

  • Committer: Package Import Robot
  • Author(s): David Prévot, Greg Sherwood, Alexey, Emily, David Prévot
  • Date: 2014-09-26 13:44:35 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140926134435-wvjq16miqq4d60y0
Tags: 1.5.5-1
[ Greg Sherwood ]
* Improved closure support in Generic ScopeIndentSniff
* Improved indented PHP tag support in Generic ScopeIndentSniff
* Standards can now be located within hidden directories
 (further fix for bug #20323)
* Fixed bug #20373 : Inline comment sniff tab handling way
* Fixed bug #20378 : Report appended to existing file if no errors
  found in run
* Fixed bug #20381 : Invalid "Comment closer must be on a new line"
* PHP tokenizer no longer converts class/function names to special
  tokens types
* Fixed bug #20386 : Squiz.Commenting.ClassComment.SpacingBefore
  thrown if first block comment
* Squiz and PEAR FunctionCommentSnif now support _()
* PEAR ValidFunctionNameSniff no longer throws an error for _()
* Fixed bug #248 : FunctionCommentSniff expects ampersand on param name
* Fixed bug #248 in Squiz sniff as well
* Fixed bug #265 : False positives with type hints in ForbiddenFunctionsSniff
* Prepare for 1.5.5 release

[ Alexey ]
* Allowed single undersored methods and functions

[ Emily ]
* Added var_dump to discouraged functions sniff

[ David Prévot ]
* Revert "Add XS-Testsuite still needed for ci.d.n"
* Add self to uploaders
* Bump standards version to 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Version control report base class for PHP_CodeSniffer.
 
4
 *
 
5
 * PHP version 5
 
6
 *
 
7
 * @category  PHP
 
8
 * @package   PHP_CodeSniffer
 
9
 * @author    Ben Selby <benmatselby@gmail.com>
 
10
 * @copyright 2009-2014 SQLI <www.sqli.com>
 
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
 * Version control report base class for PHP_CodeSniffer.
 
18
 *
 
19
 * PHP version 5
 
20
 *
 
21
 * @category  PHP
 
22
 * @package   PHP_CodeSniffer
 
23
 * @author    Ben Selby <benmatselby@gmail.com>
 
24
 * @copyright 2009-2014 SQLI <www.sqli.com>
 
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: 1.2.2
 
28
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
29
 */
 
30
abstract class PHP_CodeSniffer_Reports_VersionControl implements PHP_CodeSniffer_Report
 
31
{
 
32
 
 
33
    /**
 
34
     * The name of the report we want in the output.
 
35
     *
 
36
     * @var string
 
37
     */
 
38
    protected $reportName = 'VERSION CONTROL';
 
39
 
 
40
    /**
 
41
     * A cache of author stats collected during the run.
 
42
     *
 
43
     * @var array
 
44
     */
 
45
    private $_authorCache = array();
 
46
 
 
47
    /**
 
48
     * A cache of blame stats collected during the run.
 
49
     *
 
50
     * @var array
 
51
     */
 
52
    private $_praiseCache = array();
 
53
 
 
54
    /**
 
55
     * A cache of source stats collected during the run.
 
56
     *
 
57
     * @var array
 
58
     */
 
59
    private $_sourceCache = array();
 
60
 
 
61
 
 
62
    /**
 
63
     * Generate a partial report for a single processed file.
 
64
     *
 
65
     * Function should return TRUE if it printed or stored data about the file
 
66
     * and FALSE if it ignored the file. Returning TRUE indicates that the file and
 
67
     * its data should be counted in the grand totals.
 
68
     *
 
69
     * @param array   $report      Prepared report data.
 
70
     * @param boolean $showSources Show sources?
 
71
     * @param int     $width       Maximum allowed line width.
 
72
     *
 
73
     * @return boolean
 
74
     */
 
75
    public function generateFileReport(
 
76
        $report,
 
77
        $showSources=false,
 
78
        $width=80
 
79
    ) {
 
80
        $blames = $this->getBlameContent($report['filename']);
 
81
 
 
82
        foreach ($report['messages'] as $line => $lineErrors) {
 
83
            $author = 'Unknown';
 
84
            if (isset($blames[($line - 1)]) === true) {
 
85
                $blameAuthor = $this->getAuthor($blames[($line - 1)]);
 
86
                if ($blameAuthor !== false) {
 
87
                    $author = $blameAuthor;
 
88
                }
 
89
            }
 
90
 
 
91
            if (isset($this->_authorCache[$author]) === false) {
 
92
                $this->_authorCache[$author] = 0;
 
93
                $this->_praiseCache[$author] = array(
 
94
                                                'good' => 0,
 
95
                                                'bad'  => 0,
 
96
                                               );
 
97
            }
 
98
 
 
99
            $this->_praiseCache[$author]['bad']++;
 
100
 
 
101
            foreach ($lineErrors as $column => $colErrors) {
 
102
                foreach ($colErrors as $error) {
 
103
                    $this->_authorCache[$author]++;
 
104
 
 
105
                    if ($showSources === true) {
 
106
                        $source = $error['source'];
 
107
                        if (isset($this->_sourceCache[$author][$source]) === false) {
 
108
                            $this->_sourceCache[$author][$source] = 1;
 
109
                        } else {
 
110
                            $this->_sourceCache[$author][$source]++;
 
111
                        }
 
112
                    }
 
113
                }
 
114
            }
 
115
 
 
116
            unset($blames[($line - 1)]);
 
117
        }//end foreach
 
118
 
 
119
        // No go through and give the authors some credit for
 
120
        // all the lines that do not have errors.
 
121
        foreach ($blames as $line) {
 
122
            $author = $this->getAuthor($line);
 
123
            if ($author === false) {
 
124
                $author = 'Unknown';
 
125
            }
 
126
 
 
127
            if (isset($this->_authorCache[$author]) === false) {
 
128
                // This author doesn't have any errors.
 
129
                if (PHP_CODESNIFFER_VERBOSITY === 0) {
 
130
                    continue;
 
131
                }
 
132
 
 
133
                $this->_authorCache[$author] = 0;
 
134
                $this->_praiseCache[$author] = array(
 
135
                                                'good' => 0,
 
136
                                                'bad'  => 0,
 
137
                                               );
 
138
            }
 
139
 
 
140
            $this->_praiseCache[$author]['good']++;
 
141
        }//end foreach
 
142
 
 
143
        return true;
 
144
 
 
145
    }//end generateFileReport()
 
146
 
 
147
 
 
148
    /**
 
149
     * Prints the author of all errors and warnings, as given by "version control blame".
 
150
     *
 
151
     * @param string  $cachedData    Any partial report data that was returned from
 
152
     *                               generateFileReport during the run.
 
153
     * @param int     $totalFiles    Total number of files processed during the run.
 
154
     * @param int     $totalErrors   Total number of errors found during the run.
 
155
     * @param int     $totalWarnings Total number of warnings found during the run.
 
156
     * @param boolean $showSources   Show sources?
 
157
     * @param int     $width         Maximum allowed line width.
 
158
     * @param boolean $toScreen      Is the report being printed to screen?
 
159
     *
 
160
     * @return void
 
161
     */
 
162
    public function generate(
 
163
        $cachedData,
 
164
        $totalFiles,
 
165
        $totalErrors,
 
166
        $totalWarnings,
 
167
        $showSources=false,
 
168
        $width=80,
 
169
        $toScreen=true
 
170
    ) {
 
171
        $errorsShown = ($totalErrors + $totalWarnings);
 
172
        if ($errorsShown === 0) {
 
173
            // Nothing to show.
 
174
            return;
 
175
        }
 
176
 
 
177
        $width = max($width, 70);
 
178
        arsort($this->_authorCache);
 
179
 
 
180
        echo PHP_EOL.'PHP CODE SNIFFER '.$this->reportName.' BLAME SUMMARY'.PHP_EOL;
 
181
        echo str_repeat('-', $width).PHP_EOL;
 
182
        if ($showSources === true) {
 
183
            echo 'AUTHOR   SOURCE'.str_repeat(' ', ($width - 43)).'(Author %) (Overall %) COUNT'.PHP_EOL;
 
184
            echo str_repeat('-', $width).PHP_EOL;
 
185
        } else {
 
186
            echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL;
 
187
            echo str_repeat('-', $width).PHP_EOL;
 
188
        }
 
189
 
 
190
        foreach ($this->_authorCache as $author => $count) {
 
191
            if ($this->_praiseCache[$author]['good'] === 0) {
 
192
                $percent = 0;
 
193
            } else {
 
194
                $total   = ($this->_praiseCache[$author]['bad'] + $this->_praiseCache[$author]['good']);
 
195
                $percent = round(($this->_praiseCache[$author]['bad'] / $total * 100), 2);
 
196
            }
 
197
 
 
198
            $overallPercent = '('.round((($count / $errorsShown) * 100), 2).')';
 
199
            $authorPercent  = '('.$percent.')';
 
200
            $line = str_repeat(' ', (6 - strlen($count))).$count;
 
201
            $line = str_repeat(' ', (12 - strlen($overallPercent))).$overallPercent.$line;
 
202
            $line = str_repeat(' ', (11 - strlen($authorPercent))).$authorPercent.$line;
 
203
            $line = $author.str_repeat(' ', ($width - strlen($author) - strlen($line))).$line;
 
204
 
 
205
            echo $line.PHP_EOL;
 
206
 
 
207
            if ($showSources === true && isset($this->_sourceCache[$author]) === true) {
 
208
                $errors = $this->_sourceCache[$author];
 
209
                asort($errors);
 
210
                $errors = array_reverse($errors);
 
211
 
 
212
                foreach ($errors as $source => $count) {
 
213
                    if ($source === 'count') {
 
214
                        continue;
 
215
                    }
 
216
 
 
217
                    $line = str_repeat(' ', (5 - strlen($count))).$count;
 
218
                    echo '         '.$source.str_repeat(' ', ($width - 14 - strlen($source))).$line.PHP_EOL;
 
219
                }
 
220
            }
 
221
        }//end foreach
 
222
 
 
223
        echo str_repeat('-', $width).PHP_EOL;
 
224
        echo 'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION(S) ';
 
225
        echo 'WERE COMMITTED BY '.count($this->_authorCache).' AUTHOR(S)'.PHP_EOL;
 
226
        echo str_repeat('-', $width).PHP_EOL.PHP_EOL;
 
227
 
 
228
        if ($toScreen === true
 
229
            && PHP_CODESNIFFER_INTERACTIVE === false
 
230
            && class_exists('PHP_Timer', false) === true
 
231
        ) {
 
232
            echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
 
233
        }
 
234
 
 
235
    }//end generate()
 
236
 
 
237
 
 
238
    /**
 
239
     * Extract the author from a blame line.
 
240
     *
 
241
     * @param string $line Line to parse.
 
242
     *
 
243
     * @return mixed string or false if impossible to recover.
 
244
     */
 
245
    abstract protected function getAuthor($line);
 
246
 
 
247
 
 
248
    /**
 
249
     * Gets the blame output.
 
250
     *
 
251
     * @param string $filename File to blame.
 
252
     *
 
253
     * @return array
 
254
     */
 
255
    abstract protected function getBlameContent($filename);
 
256
 
 
257
 
 
258
}//end class
 
259
 
 
260
?>