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 |
||
43 | 2 | public function getMaxAllowableHoleScore($par, $courseHandicap) |
|
44 | { |
||
45 | switch(true) { |
||
46 | 2 | case ($courseHandicap <= 9): |
|
47 | 2 | $maxScore = $par + 2; |
|
48 | 2 | break; |
|
49 | 1 | case (($courseHandicap > 9) && ($courseHandicap < 20)): //10-19 |
|
50 | 1 | $maxScore = 7; |
|
51 | 1 | break; |
|
52 | 1 | case (($courseHandicap >= 20) && ($courseHandicap < 30))://20-29 |
|
53 | 1 | $maxScore = 8; |
|
54 | 1 | break; |
|
55 | 1 | case (($courseHandicap >= 30) && ($courseHandicap < 40))://30-39 |
|
56 | 1 | $maxScore = 9; |
|
57 | 1 | break; |
|
58 | 1 | case ($courseHandicap >= 40): |
|
59 | 1 | $maxScore = 10; |
|
60 | } |
||
61 | 2 | return $maxScore; |
|
|
|||
62 | } |
||
63 | } |
||
64 |
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: