| Conditions | 9 |
| Paths | 6 |
| Total Lines | 20 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 16 |
| CRAP Score | 9 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 16 | 1 | public function getMaxAllowableHoleScore($par, $courseHandicap) |
|
| 17 | { |
||
| 18 | switch(true) { |
||
| 19 | 1 | case ($courseHandicap <= 9): |
|
| 20 | 1 | $maxScore = $par + 2; |
|
| 21 | 1 | break; |
|
| 22 | 1 | case (($courseHandicap > 9) && ($courseHandicap < 20)): //10-19 |
|
| 23 | 1 | $maxScore = 7; |
|
| 24 | 1 | break; |
|
| 25 | 1 | case (($courseHandicap >= 20) && ($courseHandicap < 30))://20-29 |
|
| 26 | 1 | $maxScore = 8; |
|
| 27 | 1 | break; |
|
| 28 | 1 | case (($courseHandicap >= 30) && ($courseHandicap < 40))://30-39 |
|
| 29 | 1 | $maxScore = 9; |
|
| 30 | 1 | break; |
|
| 31 | 1 | case ($courseHandicap >= 40): |
|
| 32 | 1 | $maxScore = 10; |
|
| 33 | } |
||
| 34 | 1 | return $maxScore; |
|
|
|
|||
| 35 | } |
||
| 36 | } |
||
| 37 |
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: