3
* Version control report base class for PHP_CodeSniffer.
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
17
* Version control report base class for PHP_CodeSniffer.
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
30
abstract class PHP_CodeSniffer_Reports_VersionControl implements PHP_CodeSniffer_Report
34
* The name of the report we want in the output.
38
protected $reportName = 'VERSION CONTROL';
41
* A cache of author stats collected during the run.
45
private $_authorCache = array();
48
* A cache of blame stats collected during the run.
52
private $_praiseCache = array();
55
* A cache of source stats collected during the run.
59
private $_sourceCache = array();
63
* Generate a partial report for a single processed file.
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.
69
* @param array $report Prepared report data.
70
* @param boolean $showSources Show sources?
71
* @param int $width Maximum allowed line width.
75
public function generateFileReport(
80
$blames = $this->getBlameContent($report['filename']);
82
foreach ($report['messages'] as $line => $lineErrors) {
84
if (isset($blames[($line - 1)]) === true) {
85
$blameAuthor = $this->getAuthor($blames[($line - 1)]);
86
if ($blameAuthor !== false) {
87
$author = $blameAuthor;
91
if (isset($this->_authorCache[$author]) === false) {
92
$this->_authorCache[$author] = 0;
93
$this->_praiseCache[$author] = array(
99
$this->_praiseCache[$author]['bad']++;
101
foreach ($lineErrors as $column => $colErrors) {
102
foreach ($colErrors as $error) {
103
$this->_authorCache[$author]++;
105
if ($showSources === true) {
106
$source = $error['source'];
107
if (isset($this->_sourceCache[$author][$source]) === false) {
108
$this->_sourceCache[$author][$source] = 1;
110
$this->_sourceCache[$author][$source]++;
116
unset($blames[($line - 1)]);
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) {
127
if (isset($this->_authorCache[$author]) === false) {
128
// This author doesn't have any errors.
129
if (PHP_CODESNIFFER_VERBOSITY === 0) {
133
$this->_authorCache[$author] = 0;
134
$this->_praiseCache[$author] = array(
140
$this->_praiseCache[$author]['good']++;
145
}//end generateFileReport()
149
* Prints the author of all errors and warnings, as given by "version control blame".
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?
162
public function generate(
171
$errorsShown = ($totalErrors + $totalWarnings);
172
if ($errorsShown === 0) {
177
$width = max($width, 70);
178
arsort($this->_authorCache);
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;
186
echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL;
187
echo str_repeat('-', $width).PHP_EOL;
190
foreach ($this->_authorCache as $author => $count) {
191
if ($this->_praiseCache[$author]['good'] === 0) {
194
$total = ($this->_praiseCache[$author]['bad'] + $this->_praiseCache[$author]['good']);
195
$percent = round(($this->_praiseCache[$author]['bad'] / $total * 100), 2);
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;
207
if ($showSources === true && isset($this->_sourceCache[$author]) === true) {
208
$errors = $this->_sourceCache[$author];
210
$errors = array_reverse($errors);
212
foreach ($errors as $source => $count) {
213
if ($source === 'count') {
217
$line = str_repeat(' ', (5 - strlen($count))).$count;
218
echo ' '.$source.str_repeat(' ', ($width - 14 - strlen($source))).$line.PHP_EOL;
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;
228
if ($toScreen === true
229
&& PHP_CODESNIFFER_INTERACTIVE === false
230
&& class_exists('PHP_Timer', false) === true
232
echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
239
* Extract the author from a blame line.
241
* @param string $line Line to parse.
243
* @return mixed string or false if impossible to recover.
245
abstract protected function getAuthor($line);
249
* Gets the blame output.
251
* @param string $filename File to blame.
255
abstract protected function getBlameContent($filename);