EquitableStrokeControl::maxAllowableScore()   B
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 21
rs 7.041
cc 9
eloc 17
nc 6
nop 2
1
<?php
2
3
namespace GolfLeague;
4
use \Holescore as Holescore;
5
use \Hole as Hole;
6
use \Round as Round;
7
use \Player as Player;
8
9
//Determines a net score using Equitable Scoring Control for individual hole
10
/*
11
Course Handicap   				Maximum Score
12
4.5 or less                    Double Bogey(+2)
13
5–9.5                                       7
14
9.6–14.5                                    8
15
14.6–19.5                                   9
16
19.6 or more                               10
17
*/
18
19
class EquitableStrokeControl {
20
21
	public function __construct(Holescore $holescore)
22
	{
23
		$this->holescore= $holescore;
0 ignored issues
show
Bug introduced by
The property holescore does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
	}
25
26
	public function calculate()
27
	{
28
		//holescore
29
		$holescore = $this->holescore->score; // Gross score on hole
30
31
		//Use Round to get player_id
32
		$player = Round::find($this->holescore->round_id)->player_id;
33
34
		//Get player's handicap
35
		$handicap = Player::find($player)->handicap;
36
37
		//Get par for hole
38
		$holePar = Hole::find($this->holescore->hole_id)->par;
39
40
		//Use par and handicap to determine max score for a hole
41
		$maxScore = $this->maxAllowableScore($holePar, $handicap);
42
43
		if($holescore > $maxScore) {
44
			return $maxScore;
45
		}
46
		return $holescore;
47
48
	}
49
50
	private function maxAllowableScore($par, $handicap)
51
	{
52
		switch($handicap) {
53
			case ($handicap < 4.6):
54
				$maxScore = $par + 2;
55
				break;
56
			case (($handicap >= 4.6) && ($handicap < 9.6)):
57
				$maxScore = 7;
58
				break;
59
			case (($handicap >= 9.6) && ($handicap < 14.6)):
60
				$maxScore = 8;
61
				break;
62
			case (($handicap >= 14.6) && ($handicap < 19.6)):
63
				$maxScore = 9;
64
				break;
65
			case ($handicap >= 19.6):
66
				$maxScore = 10;
67
		}
68
69
		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...
70
	}
71
}
72