Conditions | 31 |
Paths | > 20000 |
Total Lines | 180 |
Code Lines | 105 |
Lines | 0 |
Ratio | 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:
1 | <?php |
||
230 | public function generateFailureMessages(PHP_CodeSniffer_File $file) |
||
231 | { |
||
232 | $testFile = $file->getFilename(); |
||
233 | |||
234 | $foundErrors = $file->getErrors(); |
||
235 | $foundWarnings = $file->getWarnings(); |
||
236 | $expectedErrors = $this->getErrorList(basename($testFile)); |
||
237 | $expectedWarnings = $this->getWarningList(basename($testFile)); |
||
238 | |||
239 | if (is_array($expectedErrors) === false) { |
||
240 | throw new PHP_CodeSniffer_Exception('getErrorList() must return an array'); |
||
241 | } |
||
242 | |||
243 | if (is_array($expectedWarnings) === false) { |
||
244 | throw new PHP_CodeSniffer_Exception('getWarningList() must return an array'); |
||
245 | } |
||
246 | |||
247 | /* |
||
248 | We merge errors and warnings together to make it easier |
||
249 | to iterate over them and produce the errors string. In this way, |
||
250 | we can report on errors and warnings in the same line even though |
||
251 | it's not really structured to allow that. |
||
252 | */ |
||
253 | |||
254 | $allProblems = array(); |
||
255 | $failureMessages = array(); |
||
256 | |||
257 | foreach ($foundErrors as $line => $lineErrors) { |
||
258 | foreach ($lineErrors as $column => $errors) { |
||
259 | if (isset($allProblems[$line]) === false) { |
||
260 | $allProblems[$line] = array( |
||
261 | 'expected_errors' => 0, |
||
262 | 'expected_warnings' => 0, |
||
263 | 'found_errors' => array(), |
||
264 | 'found_warnings' => array(), |
||
265 | ); |
||
266 | } |
||
267 | |||
268 | $foundErrorsTemp = array(); |
||
269 | foreach ($allProblems[$line]['found_errors'] as $foundError) { |
||
270 | $foundErrorsTemp[] = $foundError; |
||
271 | } |
||
272 | |||
273 | $errorsTemp = array(); |
||
274 | foreach ($errors as $foundError) { |
||
275 | $errorsTemp[] = $foundError['message'].' ('.$foundError['source'].')'; |
||
276 | } |
||
277 | |||
278 | $allProblems[$line]['found_errors'] = array_merge($foundErrorsTemp, $errorsTemp); |
||
279 | }//end foreach |
||
280 | |||
281 | if (isset($expectedErrors[$line]) === true) { |
||
282 | $allProblems[$line]['expected_errors'] = $expectedErrors[$line]; |
||
283 | } else { |
||
284 | $allProblems[$line]['expected_errors'] = 0; |
||
285 | } |
||
286 | |||
287 | unset($expectedErrors[$line]); |
||
288 | }//end foreach |
||
289 | |||
290 | foreach ($expectedErrors as $line => $numErrors) { |
||
291 | if (isset($allProblems[$line]) === false) { |
||
292 | $allProblems[$line] = array( |
||
293 | 'expected_errors' => 0, |
||
294 | 'expected_warnings' => 0, |
||
295 | 'found_errors' => array(), |
||
296 | 'found_warnings' => array(), |
||
297 | ); |
||
298 | } |
||
299 | |||
300 | $allProblems[$line]['expected_errors'] = $numErrors; |
||
301 | } |
||
302 | |||
303 | foreach ($foundWarnings as $line => $lineWarnings) { |
||
304 | foreach ($lineWarnings as $column => $warnings) { |
||
305 | if (isset($allProblems[$line]) === false) { |
||
306 | $allProblems[$line] = array( |
||
307 | 'expected_errors' => 0, |
||
308 | 'expected_warnings' => 0, |
||
309 | 'found_errors' => array(), |
||
310 | 'found_warnings' => array(), |
||
311 | ); |
||
312 | } |
||
313 | |||
314 | $foundWarningsTemp = array(); |
||
315 | foreach ($allProblems[$line]['found_warnings'] as $foundWarning) { |
||
316 | $foundWarningsTemp[] = $foundWarning; |
||
317 | } |
||
318 | |||
319 | $warningsTemp = array(); |
||
320 | foreach ($warnings as $warning) { |
||
321 | $warningsTemp[] = $warning['message'].' ('.$warning['source'].')'; |
||
322 | } |
||
323 | |||
324 | $allProblems[$line]['found_warnings'] = array_merge($foundWarningsTemp, $warningsTemp); |
||
325 | }//end foreach |
||
326 | |||
327 | if (isset($expectedWarnings[$line]) === true) { |
||
328 | $allProblems[$line]['expected_warnings'] = $expectedWarnings[$line]; |
||
329 | } else { |
||
330 | $allProblems[$line]['expected_warnings'] = 0; |
||
331 | } |
||
332 | |||
333 | unset($expectedWarnings[$line]); |
||
334 | }//end foreach |
||
335 | |||
336 | foreach ($expectedWarnings as $line => $numWarnings) { |
||
337 | if (isset($allProblems[$line]) === false) { |
||
338 | $allProblems[$line] = array( |
||
339 | 'expected_errors' => 0, |
||
340 | 'expected_warnings' => 0, |
||
341 | 'found_errors' => array(), |
||
342 | 'found_warnings' => array(), |
||
343 | ); |
||
344 | } |
||
345 | |||
346 | $allProblems[$line]['expected_warnings'] = $numWarnings; |
||
347 | } |
||
348 | |||
349 | // Order the messages by line number. |
||
350 | ksort($allProblems); |
||
351 | |||
352 | foreach ($allProblems as $line => $problems) { |
||
353 | $numErrors = count($problems['found_errors']); |
||
354 | $numWarnings = count($problems['found_warnings']); |
||
355 | $expectedErrors = $problems['expected_errors']; |
||
356 | $expectedWarnings = $problems['expected_warnings']; |
||
357 | |||
358 | $errors = ''; |
||
359 | $foundString = ''; |
||
360 | |||
361 | if ($expectedErrors !== $numErrors || $expectedWarnings !== $numWarnings) { |
||
362 | $lineMessage = "[LINE $line]"; |
||
363 | $expectedMessage = 'Expected '; |
||
364 | $foundMessage = 'in '.basename($testFile).' but found '; |
||
365 | |||
366 | if ($expectedErrors !== $numErrors) { |
||
367 | $expectedMessage .= "$expectedErrors error(s)"; |
||
368 | $foundMessage .= "$numErrors error(s)"; |
||
369 | if ($numErrors !== 0) { |
||
370 | $foundString .= 'error(s)'; |
||
371 | $errors .= implode(PHP_EOL.' -> ', $problems['found_errors']); |
||
372 | } |
||
373 | |||
374 | if ($expectedWarnings !== $numWarnings) { |
||
375 | $expectedMessage .= ' and '; |
||
376 | $foundMessage .= ' and '; |
||
377 | if ($numWarnings !== 0) { |
||
378 | if ($foundString !== '') { |
||
379 | $foundString .= ' and '; |
||
380 | } |
||
381 | } |
||
382 | } |
||
383 | } |
||
384 | |||
385 | if ($expectedWarnings !== $numWarnings) { |
||
386 | $expectedMessage .= "$expectedWarnings warning(s)"; |
||
387 | $foundMessage .= "$numWarnings warning(s)"; |
||
388 | if ($numWarnings !== 0) { |
||
389 | $foundString .= 'warning(s)'; |
||
390 | if (empty($errors) === false) { |
||
391 | $errors .= PHP_EOL.' -> '; |
||
392 | } |
||
393 | |||
394 | $errors .= implode(PHP_EOL.' -> ', $problems['found_warnings']); |
||
395 | } |
||
396 | } |
||
397 | |||
398 | $fullMessage = "$lineMessage $expectedMessage $foundMessage."; |
||
399 | if ($errors !== '') { |
||
400 | $fullMessage .= " The $foundString found were:".PHP_EOL." -> $errors"; |
||
401 | } |
||
402 | |||
403 | $failureMessages[] = $fullMessage; |
||
404 | }//end if |
||
405 | }//end foreach |
||
406 | |||
407 | return $failureMessages; |
||
408 | |||
409 | }//end generateFailureMessages() |
||
410 | |||
439 |