Completed
Push — master ( a0c851...1cf6f1 )
by Michael
03:37
created

EquitableScoreControl   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getMaxAllowableHoleScore() 0 20 9
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: michaelschmidt
5
 * Date: 8/17/16
6
 * Time: 9:44 PM
7
 */
8
9
namespace GLsoftware\Golf;
10
11
12
class EquitableScoreControl
13
{
14
15
    // Using a players course handicap which is a whole number
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;
0 ignored issues
show
Bug introduced by
The variable $maxScore does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
35
    }
36
}
37