1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Version control report base class for Symplify\PHP7_CodeSniffer. |
4
|
|
|
* |
5
|
|
|
* @author Ben Selby <[email protected]> |
6
|
|
|
* @author Greg Sherwood <[email protected]> |
7
|
|
|
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
8
|
|
|
* @license https://github.com/squizlabs/Symplify\PHP7_CodeSniffer/blob/master/licence.txt BSD Licence |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Symplify\PHP7_CodeSniffer\Reports; |
12
|
|
|
|
13
|
|
|
use Symplify\PHP7_CodeSniffer\Files\File; |
14
|
|
|
use Symplify\PHP7_CodeSniffer\Util\Timing; |
15
|
|
|
|
16
|
|
|
abstract class VersionControl implements Report |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The name of the report we want in the output. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $reportName = 'VERSION CONTROL'; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Generate a partial report for a single processed file. |
29
|
|
|
* |
30
|
|
|
* Function should return TRUE if it printed or stored data about the file |
31
|
|
|
* and FALSE if it ignored the file. Returning TRUE indicates that the file and |
32
|
|
|
* its data should be counted in the grand totals. |
33
|
|
|
* |
34
|
|
|
* @param array $report Prepared report data. |
35
|
|
|
* @param \Symplify\PHP7_CodeSniffer\File $phpcsFile The file being reported on. |
36
|
|
|
* @param bool $showSources Show sources? |
37
|
|
|
* @param int $width Maximum allowed line width. |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
42
|
|
|
{ |
43
|
|
|
$blames = $this->getBlameContent($report['filename']); |
44
|
|
|
|
45
|
|
|
$authorCache = array(); |
46
|
|
|
$praiseCache = array(); |
47
|
|
|
$sourceCache = array(); |
48
|
|
|
|
49
|
|
|
foreach ($report['messages'] as $line => $lineErrors) { |
50
|
|
|
$author = 'Unknown'; |
51
|
|
|
if (isset($blames[($line - 1)]) === true) { |
52
|
|
|
$blameAuthor = $this->getAuthor($blames[($line - 1)]); |
53
|
|
|
if ($blameAuthor !== false) { |
54
|
|
|
$author = $blameAuthor; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
if (isset($authorCache[$author]) === false) { |
|
|
|
|
59
|
|
|
$authorCache[$author] = 0; |
60
|
|
|
$praiseCache[$author] = array( |
61
|
|
|
'good' => 0, |
62
|
|
|
'bad' => 0, |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$praiseCache[$author]['bad']++; |
67
|
|
|
|
68
|
|
|
foreach ($lineErrors as $column => $colErrors) { |
69
|
|
|
foreach ($colErrors as $error) { |
70
|
|
|
$authorCache[$author]++; |
71
|
|
|
|
72
|
|
|
if ($showSources === true) { |
73
|
|
|
$source = $error['source']; |
74
|
|
|
if (isset($sourceCache[$author][$source]) === false) { |
75
|
|
|
$sourceCache[$author][$source] = 1; |
76
|
|
|
} else { |
77
|
|
|
$sourceCache[$author][$source]++; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
unset($blames[($line - 1)]); |
84
|
|
|
}//end foreach |
85
|
|
|
|
86
|
|
|
// Now go through and give the authors some credit for |
87
|
|
|
// all the lines that do not have errors. |
88
|
|
|
foreach ($blames as $line) { |
89
|
|
|
$author = $this->getAuthor($line); |
90
|
|
|
if ($author === false) { |
91
|
|
|
$author = 'Unknown'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
if (isset($authorCache[$author]) === false) { |
|
|
|
|
95
|
|
|
// This author doesn't have any errors. |
96
|
|
|
if (PHP_CodeSniffer_VERBOSITY === 0) { |
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$authorCache[$author] = 0; |
101
|
|
|
$praiseCache[$author] = array( |
102
|
|
|
'good' => 0, |
103
|
|
|
'bad' => 0, |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$praiseCache[$author]['good']++; |
108
|
|
|
}//end foreach |
109
|
|
|
|
110
|
|
|
foreach ($authorCache as $author => $errors) { |
111
|
|
|
echo "AUTHOR>>$author>>$errors".PHP_EOL; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ($praiseCache as $author => $praise) { |
115
|
|
|
echo "PRAISE>>$author>>".$praise['good'].'>>'.$praise['bad'].PHP_EOL; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($sourceCache as $author => $sources) { |
119
|
|
|
foreach ($sources as $source => $errors) { |
120
|
|
|
echo "SOURCE>>$author>>$source>>$errors".PHP_EOL; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return true; |
125
|
|
|
|
126
|
|
|
}//end generateFileReport() |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Prints the author of all errors and warnings, as given by "version control blame". |
131
|
|
|
* |
132
|
|
|
* @param string $cachedData Any partial report data that was returned from |
133
|
|
|
* generateFileReport during the run. |
134
|
|
|
* @param int $totalFiles Total number of files processed during the run. |
135
|
|
|
* @param int $totalErrors Total number of errors found during the run. |
136
|
|
|
* @param int $totalWarnings Total number of warnings found during the run. |
137
|
|
|
* @param int $totalFixable Total number of problems that can be fixed. |
138
|
|
|
* @param bool $showSources Show sources? |
139
|
|
|
* @param int $width Maximum allowed line width. |
140
|
|
|
* @param bool $interactive Are we running in interactive mode? |
141
|
|
|
* @param bool $toScreen Is the report being printed to screen? |
142
|
|
|
* |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
|
public function generate( |
146
|
|
|
$cachedData, |
147
|
|
|
$totalFiles, |
148
|
|
|
$totalErrors, |
149
|
|
|
$totalWarnings, |
150
|
|
|
$totalFixable, |
151
|
|
|
$showSources=false, |
152
|
|
|
$width=80, |
153
|
|
|
$interactive=false, |
154
|
|
|
$toScreen=true |
155
|
|
|
) { |
156
|
|
|
$errorsShown = ($totalErrors + $totalWarnings); |
157
|
|
|
if ($errorsShown === 0) { |
158
|
|
|
// Nothing to show. |
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$lines = explode(PHP_EOL, $cachedData); |
163
|
|
|
array_pop($lines); |
164
|
|
|
|
165
|
|
|
if (empty($lines) === true) { |
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$authorCache = array(); |
170
|
|
|
$praiseCache = array(); |
171
|
|
|
$sourceCache = array(); |
172
|
|
|
|
173
|
|
|
foreach ($lines as $line) { |
174
|
|
|
$parts = explode('>>', $line); |
175
|
|
|
switch ($parts[0]) { |
176
|
|
|
case 'AUTHOR': |
177
|
|
|
if (isset($authorCache[$parts[1]]) === false) { |
178
|
|
|
$authorCache[$parts[1]] = $parts[2]; |
179
|
|
|
} else { |
180
|
|
|
$authorCache[$parts[1]] += $parts[2]; |
181
|
|
|
} |
182
|
|
|
break; |
183
|
|
|
case 'PRAISE': |
184
|
|
|
if (isset($praiseCache[$parts[1]]) === false) { |
185
|
|
|
$praiseCache[$parts[1]] = array( |
186
|
|
|
'good' => $parts[2], |
187
|
|
|
'bad' => $parts[3], |
188
|
|
|
); |
189
|
|
|
} else { |
190
|
|
|
$praiseCache[$parts[1]]['good'] += $parts[2]; |
191
|
|
|
$praiseCache[$parts[1]]['bad'] += $parts[3]; |
192
|
|
|
} |
193
|
|
|
break; |
194
|
|
|
case 'SOURCE': |
195
|
|
|
if (isset($praiseCache[$parts[1]]) === false) { |
196
|
|
|
$praiseCache[$parts[1]] = array(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if (isset($sourceCache[$parts[1]][$parts[2]]) === false) { |
200
|
|
|
$sourceCache[$parts[1]][$parts[2]] = $parts[3]; |
201
|
|
|
} else { |
202
|
|
|
$sourceCache[$parts[1]][$parts[2]] += $parts[3]; |
203
|
|
|
} |
204
|
|
|
break; |
205
|
|
|
default: |
206
|
|
|
break; |
207
|
|
|
}//end switch |
208
|
|
|
}//end foreach |
209
|
|
|
|
210
|
|
|
// Make sure the report width isn't too big. |
211
|
|
|
$maxLength = 0; |
212
|
|
|
foreach ($authorCache as $author => $count) { |
213
|
|
|
$maxLength = max($maxLength, strlen($author)); |
214
|
|
|
if ($showSources === true && isset($sourceCache[$author]) === true) { |
215
|
|
|
foreach ($sourceCache[$author] as $source => $count) { |
216
|
|
|
if ($source === 'count') { |
217
|
|
|
continue; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$maxLength = max($maxLength, (strlen($source) + 9)); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$width = min($width, ($maxLength + 30)); |
226
|
|
|
$width = max($width, 70); |
227
|
|
|
arsort($authorCache); |
228
|
|
|
|
229
|
|
|
echo PHP_EOL."\033[1m".'PHP CODE SNIFFER '.$this->reportName.' BLAME SUMMARY'."\033[0m".PHP_EOL; |
230
|
|
|
echo str_repeat('-', $width).PHP_EOL."\033[1m"; |
231
|
|
|
if ($showSources === true) { |
232
|
|
|
echo 'AUTHOR SOURCE'.str_repeat(' ', ($width - 43)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
233
|
|
|
echo str_repeat('-', $width).PHP_EOL; |
234
|
|
|
} else { |
235
|
|
|
echo 'AUTHOR'.str_repeat(' ', ($width - 34)).'(Author %) (Overall %) COUNT'.PHP_EOL; |
236
|
|
|
echo str_repeat('-', $width).PHP_EOL; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
echo "\033[0m"; |
240
|
|
|
|
241
|
|
|
foreach ($authorCache as $author => $count) { |
242
|
|
|
if ($praiseCache[$author]['good'] === 0) { |
243
|
|
|
$percent = 0; |
244
|
|
|
} else { |
245
|
|
|
$total = ($praiseCache[$author]['bad'] + $praiseCache[$author]['good']); |
246
|
|
|
$percent = round(($praiseCache[$author]['bad'] / $total * 100), 2); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$overallPercent = '('.round((($count / $errorsShown) * 100), 2).')'; |
250
|
|
|
$authorPercent = '('.$percent.')'; |
251
|
|
|
$line = str_repeat(' ', (6 - strlen($count))).$count; |
252
|
|
|
$line = str_repeat(' ', (12 - strlen($overallPercent))).$overallPercent.$line; |
253
|
|
|
$line = str_repeat(' ', (11 - strlen($authorPercent))).$authorPercent.$line; |
254
|
|
|
$line = $author.str_repeat(' ', ($width - strlen($author) - strlen($line))).$line; |
255
|
|
|
|
256
|
|
|
if ($showSources === true) { |
257
|
|
|
$line = "\033[1m$line\033[0m"; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
echo $line.PHP_EOL; |
261
|
|
|
|
262
|
|
|
if ($showSources === true && isset($sourceCache[$author]) === true) { |
263
|
|
|
$errors = $sourceCache[$author]; |
264
|
|
|
asort($errors); |
265
|
|
|
$errors = array_reverse($errors); |
266
|
|
|
|
267
|
|
|
foreach ($errors as $source => $count) { |
268
|
|
|
if ($source === 'count') { |
269
|
|
|
continue; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$line = str_repeat(' ', (5 - strlen($count))).$count; |
273
|
|
|
echo ' '.$source.str_repeat(' ', ($width - 14 - strlen($source))).$line.PHP_EOL; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
}//end foreach |
277
|
|
|
|
278
|
|
|
echo str_repeat('-', $width).PHP_EOL; |
279
|
|
|
echo "\033[1m".'A TOTAL OF '.$errorsShown.' SNIFF VIOLATION'; |
280
|
|
|
if ($errorsShown !== 1) { |
281
|
|
|
echo 'S'; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
echo ' WERE COMMITTED BY '.count($authorCache).' AUTHOR'; |
285
|
|
|
if (count($authorCache) !== 1) { |
286
|
|
|
echo 'S'; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
echo "\033[0m"; |
290
|
|
|
|
291
|
|
View Code Duplication |
if ($totalFixable > 0) { |
|
|
|
|
292
|
|
|
echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
293
|
|
|
echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m"; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
297
|
|
|
|
298
|
|
|
if ($toScreen === true && $interactive === false) { |
299
|
|
|
Timing::printRunTime(); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
}//end generate() |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Extract the author from a blame line. |
307
|
|
|
* |
308
|
|
|
* @param string $line Line to parse. |
309
|
|
|
* |
310
|
|
|
* @return mixed string or false if impossible to recover. |
311
|
|
|
*/ |
312
|
|
|
abstract protected function getAuthor($line); |
313
|
|
|
|
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Gets the blame output. |
317
|
|
|
* |
318
|
|
|
* @param string $filename File to blame. |
319
|
|
|
* |
320
|
|
|
* @return array |
321
|
|
|
*/ |
322
|
|
|
abstract protected function getBlameContent($filename); |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
}//end class |
326
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.