Conditions | 29 |
Paths | > 20000 |
Total Lines | 158 |
Code Lines | 106 |
Lines | 4 |
Ratio | 2.53 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
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 | |||
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.