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