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