Completed
Push — master ( 236403...70f3fa )
by Michael
02:53
created

EquitableScoreControl::adjustScores()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 3
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
    private $escResultArray;
15
16
    /**
17
     *
18
     * @param array $holeScores
19
     * @param $courseHandicap
20
     * @return array
21
     */
22 1
    public function adjustScores(array $holeScores, $courseHandicap)
23
    {
24 1
        foreach($holeScores as $key => $holeScore){
25 1
            $maxHoleScore = $this->getMaxAllowableHoleScore($holeScore['par'],$courseHandicap);
26
27 1
            if($holeScore['score'] >= $maxHoleScore ){
28 1
                $this->escResultArray[] = $maxHoleScore;
29
            } else {
30 1
                $this->escResultArray[] = $holeScore['score'];
31
            }
32
        }
33
34 1
        return $this->escResultArray;
35
    }
36
37
    // Using a players course handicap which is a whole number
38
    /**
39
     * @param $par
40
     * @param $courseHandicap
41
     * @return int
42
     */
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;
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...
62
    }
63
}
64