1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Summary report for Symplify\PHP7_CodeSniffer. |
4
|
|
|
* |
5
|
|
|
* @author Greg Sherwood <[email protected]> |
6
|
|
|
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) |
7
|
|
|
* @license https://github.com/squizlabs/Symplify\PHP7_CodeSniffer/blob/master/licence.txt BSD Licence |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Symplify\PHP7_CodeSniffer\Reports; |
11
|
|
|
|
12
|
|
|
use Symplify\PHP7_CodeSniffer\Files\File; |
13
|
|
|
use Symplify\PHP7_CodeSniffer\Util; |
14
|
|
|
|
15
|
|
|
class Summary implements Report |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Generate a partial report for a single processed file. |
21
|
|
|
* |
22
|
|
|
* Function should return TRUE if it printed or stored data about the file |
23
|
|
|
* and FALSE if it ignored the file. Returning TRUE indicates that the file and |
24
|
|
|
* its data should be counted in the grand totals. |
25
|
|
|
* |
26
|
|
|
* @param array $report Prepared report data. |
27
|
|
|
* @param \Symplify\PHP7_CodeSniffer\File $phpcsFile The file being reported on. |
28
|
|
|
* @param bool $showSources Show sources? |
29
|
|
|
* @param int $width Maximum allowed line width. |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80) |
34
|
|
|
{ |
35
|
|
|
if (PHP_CodeSniffer_VERBOSITY === 0 |
36
|
|
|
&& $report['errors'] === 0 |
37
|
|
|
&& $report['warnings'] === 0 |
38
|
|
|
) { |
39
|
|
|
// Nothing to print. |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
echo $report['filename'].'>>'.$report['errors'].'>>'.$report['warnings'].PHP_EOL; |
44
|
|
|
return true; |
45
|
|
|
|
46
|
|
|
}//end generateFileReport() |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Generates a summary of errors and warnings for each file processed. |
51
|
|
|
* |
52
|
|
|
* @param string $cachedData Any partial report data that was returned from |
53
|
|
|
* generateFileReport during the run. |
54
|
|
|
* @param int $totalFiles Total number of files processed during the run. |
55
|
|
|
* @param int $totalErrors Total number of errors found during the run. |
56
|
|
|
* @param int $totalWarnings Total number of warnings found during the run. |
57
|
|
|
* @param int $totalFixable Total number of problems that can be fixed. |
58
|
|
|
* @param bool $showSources Show sources? |
59
|
|
|
* @param int $width Maximum allowed line width. |
60
|
|
|
* @param bool $interactive Are we running in interactive mode? |
61
|
|
|
* @param bool $toScreen Is the report being printed to screen? |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function generate( |
66
|
|
|
$cachedData, |
67
|
|
|
$totalFiles, |
68
|
|
|
$totalErrors, |
69
|
|
|
$totalWarnings, |
70
|
|
|
$totalFixable, |
71
|
|
|
$showSources=false, |
72
|
|
|
$width=80, |
73
|
|
|
$interactive=false, |
74
|
|
|
$toScreen=true |
75
|
|
|
) { |
76
|
|
|
$lines = explode(PHP_EOL, $cachedData); |
77
|
|
|
array_pop($lines); |
78
|
|
|
|
79
|
|
|
if (empty($lines) === true) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$reportFiles = array(); |
84
|
|
|
$maxLength = 0; |
85
|
|
|
|
86
|
|
|
foreach ($lines as $line) { |
87
|
|
|
$parts = explode('>>', $line); |
88
|
|
|
$fileLen = strlen($parts[0]); |
89
|
|
|
$reportFiles[$parts[0]] = array( |
90
|
|
|
'errors' => $parts[1], |
91
|
|
|
'warnings' => $parts[2], |
92
|
|
|
'strlen' => $fileLen, |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$maxLength = max($maxLength, $fileLen); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$width = min($width, ($maxLength + 21)); |
99
|
|
|
$width = max($width, 70); |
100
|
|
|
|
101
|
|
|
echo PHP_EOL."\033[1m".'PHP CODE SNIFFER REPORT SUMMARY'."\033[0m".PHP_EOL; |
102
|
|
|
echo str_repeat('-', $width).PHP_EOL; |
103
|
|
|
echo "\033[1m".'FILE'.str_repeat(' ', ($width - 20)).'ERRORS WARNINGS'."\033[0m".PHP_EOL; |
104
|
|
|
echo str_repeat('-', $width).PHP_EOL; |
105
|
|
|
|
106
|
|
|
foreach ($reportFiles as $file => $data) { |
107
|
|
|
$padding = ($width - 18 - $data['strlen']); |
108
|
|
|
if ($padding < 0) { |
109
|
|
|
$file = '...'.substr($file, (($padding * -1) + 3)); |
110
|
|
|
$padding = 0; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
echo $file.str_repeat(' ', $padding).' '; |
114
|
|
|
if ($data['errors'] !== 0) { |
115
|
|
|
echo "\033[31m".$data['errors']."\033[0m"; |
116
|
|
|
echo str_repeat(' ', (8 - strlen((string) $data['errors']))); |
117
|
|
|
} else { |
118
|
|
|
echo '0 '; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($data['warnings'] !== 0) { |
122
|
|
|
echo "\033[33m".$data['warnings']."\033[0m"; |
123
|
|
|
} else { |
124
|
|
|
echo '0'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
echo PHP_EOL; |
128
|
|
|
}//end foreach |
129
|
|
|
|
130
|
|
|
echo str_repeat('-', $width).PHP_EOL; |
131
|
|
|
echo "\033[1mA TOTAL OF $totalErrors ERROR"; |
132
|
|
|
if ($totalErrors !== 1) { |
133
|
|
|
echo 'S'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
echo ' AND '.$totalWarnings.' WARNING'; |
137
|
|
|
if ($totalWarnings !== 1) { |
138
|
|
|
echo 'S'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
echo ' WERE FOUND IN '.$totalFiles.' FILE'; |
142
|
|
|
if ($totalFiles !== 1) { |
143
|
|
|
echo 'S'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
echo "\033[0m"; |
147
|
|
|
|
148
|
|
View Code Duplication |
if ($totalFixable > 0) { |
|
|
|
|
149
|
|
|
echo PHP_EOL.str_repeat('-', $width).PHP_EOL; |
150
|
|
|
echo "\033[1mPHPCBF CAN FIX $totalFixable OF THESE SNIFF VIOLATIONS AUTOMATICALLY\033[0m"; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
echo PHP_EOL.str_repeat('-', $width).PHP_EOL.PHP_EOL; |
154
|
|
|
|
155
|
|
|
if ($toScreen === true && $interactive === false) { |
156
|
|
|
Util\Timing::printRunTime(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
}//end generate() |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
}//end class |
163
|
|
|
|
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.