Mercurial > hg > cmmr2012-drupal-site
comparison vendor/squizlabs/php_codesniffer/src/Reports/Full.php @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:307d7a7fd348 | 4:a9cd425dd02b |
---|---|
1 <?php | |
2 /** | |
3 * Full report for PHP_CodeSniffer. | |
4 * | |
5 * @author Greg Sherwood <gsherwood@squiz.net> | |
6 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) | |
7 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | |
8 */ | |
9 | |
10 namespace PHP_CodeSniffer\Reports; | |
11 | |
12 use PHP_CodeSniffer\Files\File; | |
13 use PHP_CodeSniffer\Util; | |
14 | |
15 class Full 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 \PHP_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 ($report['errors'] === 0 && $report['warnings'] === 0) { | |
36 // Nothing to print. | |
37 return false; | |
38 } | |
39 | |
40 // The length of the word ERROR or WARNING; used for padding. | |
41 if ($report['warnings'] > 0) { | |
42 $typeLength = 7; | |
43 } else { | |
44 $typeLength = 5; | |
45 } | |
46 | |
47 // Work out the max line number length for formatting. | |
48 $maxLineNumLength = max(array_map('strlen', array_keys($report['messages']))); | |
49 | |
50 // The padding that all lines will require that are | |
51 // printing an error message overflow. | |
52 $paddingLine2 = str_repeat(' ', ($maxLineNumLength + 1)); | |
53 $paddingLine2 .= ' | '; | |
54 $paddingLine2 .= str_repeat(' ', $typeLength); | |
55 $paddingLine2 .= ' | '; | |
56 if ($report['fixable'] > 0) { | |
57 $paddingLine2 .= ' '; | |
58 } | |
59 | |
60 $paddingLength = strlen($paddingLine2); | |
61 | |
62 // Make sure the report width isn't too big. | |
63 $maxErrorLength = 0; | |
64 foreach ($report['messages'] as $line => $lineErrors) { | |
65 foreach ($lineErrors as $column => $colErrors) { | |
66 foreach ($colErrors as $error) { | |
67 $length = strlen($error['message']); | |
68 if ($showSources === true) { | |
69 $length += (strlen($error['source']) + 3); | |
70 } | |
71 | |
72 $maxErrorLength = max($maxErrorLength, ($length + 1)); | |
73 } | |
74 } | |
75 } | |
76 | |
77 $file = $report['filename']; | |
78 $fileLength = strlen($file); | |
79 $maxWidth = max(($fileLength + 6), ($maxErrorLength + $paddingLength)); | |
80 $width = min($width, $maxWidth); | |
81 if ($width < 70) { | |
82 $width = 70; | |
83 } | |
84 | |
85 echo PHP_EOL."\033[1mFILE: "; | |
86 if ($fileLength <= ($width - 6)) { | |
87 echo $file; | |
88 } else { | |
89 echo '...'.substr($file, ($fileLength - ($width - 6))); | |
90 } | |
91 | |
92 echo "\033[0m".PHP_EOL; | |
93 echo str_repeat('-', $width).PHP_EOL; | |
94 | |
95 echo "\033[1m".'FOUND '.$report['errors'].' ERROR'; | |
96 if ($report['errors'] !== 1) { | |
97 echo 'S'; | |
98 } | |
99 | |
100 if ($report['warnings'] > 0) { | |
101 echo ' AND '.$report['warnings'].' WARNING'; | |
102 if ($report['warnings'] !== 1) { | |
103 echo 'S'; | |
104 } | |
105 } | |
106 | |
107 echo ' AFFECTING '.count($report['messages']).' LINE'; | |
108 if (count($report['messages']) !== 1) { | |
109 echo 'S'; | |
110 } | |
111 | |
112 echo "\033[0m".PHP_EOL; | |
113 echo str_repeat('-', $width).PHP_EOL; | |
114 | |
115 // The maximum amount of space an error message can use. | |
116 $maxErrorSpace = ($width - $paddingLength - 1); | |
117 | |
118 foreach ($report['messages'] as $line => $lineErrors) { | |
119 foreach ($lineErrors as $column => $colErrors) { | |
120 foreach ($colErrors as $error) { | |
121 $message = $error['message']; | |
122 $msgLines = [$message]; | |
123 if (strpos($message, "\n") !== false) { | |
124 $msgLines = explode("\n", $message); | |
125 } | |
126 | |
127 $errorMsg = ''; | |
128 $lastLine = (count($msgLines) - 1); | |
129 foreach ($msgLines as $k => $msgLine) { | |
130 if ($k === 0) { | |
131 if ($showSources === true) { | |
132 $errorMsg .= "\033[1m"; | |
133 } | |
134 } else { | |
135 $errorMsg .= PHP_EOL.$paddingLine2; | |
136 } | |
137 | |
138 if ($k === $lastLine && $showSources === true) { | |
139 $msgLine .= "\033[0m".' ('.$error['source'].')'; | |
140 } | |
141 | |
142 $errorMsg .= wordwrap( | |
143 $msgLine, | |
144 $maxErrorSpace, | |
145 PHP_EOL.$paddingLine2 | |
146 ); | |
147 } | |
148 | |
149 // The padding that goes on the front of the line. | |
150 $padding = ($maxLineNumLength - strlen($line)); | |
151 | |
152 echo ' '.str_repeat(' ', $padding).$line.' | '; | |
153 if ($error['type'] === 'ERROR') { | |
154 echo "\033[31mERROR\033[0m"; | |
155 if ($report['warnings'] > 0) { | |
156 echo ' '; | |
157 } | |
158 } else { | |
159 echo "\033[33mWARNING\033[0m"; | |
160 } | |
161 | |
162 echo ' | '; | |
163 if ($report['fixable'] > 0) { | |
164 echo '['; | |
165 if ($error['fixable'] === true) { | |
166 echo 'x'; | |
167 } else { | |
168 echo ' '; | |
169 } | |
170 | |
171 echo '] '; | |
172 } | |
173 | |
174 echo $errorMsg.PHP_EOL; | |
175 }//end foreach | |
176 }//end foreach | |
177 }//end foreach | |
178 | |
179 echo str_repeat('-', $width).PHP_EOL; | |
180 if ($report['fixable'] > 0) { | |
181 echo "\033[1m".'PHPCBF CAN FIX THE '.$report['fixable'].' MARKED SNIFF VIOLATIONS AUTOMATICALLY'."\033[0m".PHP_EOL; | |
182 echo str_repeat('-', $width).PHP_EOL; | |
183 } | |
184 | |
185 echo PHP_EOL; | |
186 return true; | |
187 | |
188 }//end generateFileReport() | |
189 | |
190 | |
191 /** | |
192 * Prints all errors and warnings for each file processed. | |
193 * | |
194 * @param string $cachedData Any partial report data that was returned from | |
195 * generateFileReport during the run. | |
196 * @param int $totalFiles Total number of files processed during the run. | |
197 * @param int $totalErrors Total number of errors found during the run. | |
198 * @param int $totalWarnings Total number of warnings found during the run. | |
199 * @param int $totalFixable Total number of problems that can be fixed. | |
200 * @param bool $showSources Show sources? | |
201 * @param int $width Maximum allowed line width. | |
202 * @param bool $interactive Are we running in interactive mode? | |
203 * @param bool $toScreen Is the report being printed to screen? | |
204 * | |
205 * @return void | |
206 */ | |
207 public function generate( | |
208 $cachedData, | |
209 $totalFiles, | |
210 $totalErrors, | |
211 $totalWarnings, | |
212 $totalFixable, | |
213 $showSources=false, | |
214 $width=80, | |
215 $interactive=false, | |
216 $toScreen=true | |
217 ) { | |
218 if ($cachedData === '') { | |
219 return; | |
220 } | |
221 | |
222 echo $cachedData; | |
223 | |
224 if ($toScreen === true && $interactive === false) { | |
225 Util\Timing::printRunTime(); | |
226 } | |
227 | |
228 }//end generate() | |
229 | |
230 | |
231 }//end class |