LeaderboardService::calculate()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 41
Code Lines 25

Duplication

Lines 4
Ratio 9.76 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 4
loc 41
rs 8.439
cc 5
eloc 25
nc 6
nop 2
1
<?php
2
3
namespace GolfLeague\Services;
4
5
use GolfLeague\Storage\MatchRound\MatchRoundRepository as MatchRound;
6
use \Course;
7
8
class LeaderboardService
9
{
10
	public function __construct(MatchRound $matchRoundRepo)
11
    {
12
        $this->matchround = $matchRoundRepo;
0 ignored issues
show
Bug introduced by
The property matchround 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...
13
    }
14
15
	public function calculate($matchId, $type)
16
	{
17
		$rounds = $this->matchround->getByMatch($matchId);
18
		$leaderboard = array();
19
		foreach ($rounds as $round){
20
			$holescores = $round->holescores;
21
				//Filter out NULL scores
22
				$filteredHolecores = $holescores->filter(function($holescore){
23
					if ($holescore->score != null) {
24
						return true;
25
					}
26
				});
27
			//at this point have a collection of filtered holescores
28
			$holescoreCount =  $filteredHolecores->count();
29
			$par = $this->currentParTotal($round->course_id, $holescoreCount);
30
31
			if($type === 'net') {
32
33
				$handicap = round($this->matchround->getMatchPlayerHandicap($matchId, $round->player->id),0);
34
				//$handicap = round($round->player->handicap,0);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
35
				$par = $handicap + $par;
36
            }
37
			$playerScore = $filteredHolecores->sum('score');
38
39
			$player = [
40
                "name" => $round->player->name,
41
                "score" => $playerScore - $par,
42
            ];
43
			array_push($leaderboard, $player);
44
		}
45
		$name = array();
46
		$score = array();
47 View Code Duplication
		foreach ($leaderboard as $key => $player) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
			$name[$key]  = $player['name'];
49
			$score[$key] = $player['score'];
50
		}
51
52
		array_multisort($score, SORT_ASC, $name, SORT_ASC, $leaderboard);
53
		return $leaderboard;
54
55
	}
56
	/*
57
	 * Given a courseID and #of holes played return par total
58
	 * @return int
59
	 */
60
	public function currentParTotal($courseId, $holesPlayed)
61
    {
62
        //find par total for course given number of holes played
63
		$holes = Course::find($courseId)->holes;
64
		$filteredHoles =$holes->slice(0,$holesPlayed);
65
		return $filteredHoles->sum('par'); // Par for given number of holes
66
67
    }
68
69
70
}