Net::getLeaderboard()   C
last analyzed

Complexity

Conditions 7
Paths 24

Size

Total Lines 43
Code Lines 27

Duplication

Lines 4
Ratio 9.3 %

Importance

Changes 0
Metric Value
dl 4
loc 43
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 27
nc 24
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mschmidt
5
 * Date: 8/4/2016
6
 * Time: 1:21 PM
7
 */
8
9
namespace GolfLeague\Tournament;
10
11
use \Tournament;
12
use GolfLeague\Storage\MatchRound\EloquentMatchRoundRepository as MatchRound;
13
use GolfLeague\Services\LeaderboardService as LeaderboardService;
14
15
class Net
16
{
17
    private $tournament;
18
19
    public function __construct(array $tournament)
20
    {
21
        $this->tournament = Tournament::where('id', '=', $tournament['id'])->with('dates')->get();
22
    }
23
24
    function get()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
    {
26
        return 'im a net tournament with an ID of ' . $this->tournament->id;
27
    }
28
29
    function getLeaderboard()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
    {
31
        //Quick and Dirty code for 2016 Tournament only
32
        // Will expand and extract later
33
        $matches = [];
34
        //for each date get the matchid that corresponds
35
        foreach($this->tournament as $tournament){
36
            foreach($tournament->dates as $date){
37
                $match = \Match::where('date', '=', $date->date)->get();
38
                foreach ($match as $m){
39
                    $matchid = $m->id;
40
                }
41
                $matchRound = new MatchRound();
42
                $leaderboardService = new LeaderboardService($matchRound);
43
                $current = $leaderboardService->calculate($matchid,'net');
0 ignored issues
show
Bug introduced by
The variable $matchid 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...
44
                $matches = array_merge($matches, $current);
45
            }
46
        }
47
48
        $leaderboard = [];
49
50
        foreach($matches as $key=>$player){
51
            $inArrayKey =  $this->in_array_r($player['name'], $leaderboard, true);
52
            if($inArrayKey === false){
53
                $leaderboard[$key]['name'] = $player['name'];
54
                $leaderboard[$key]['score'] = $player['score'];
55
            }
56
            else{
57
                $score = $player['score'] + $leaderboard[$inArrayKey]['score'];
58
                $leaderboard[$inArrayKey]['score'] = $score;
59
            }
60
        }
61
62
        $name = [];
63
        $score = [];
64 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...
65
            $name[$key]  = $player['name'];
66
            $score[$key] = $player['score'];
67
        }
68
69
        array_multisort($score, SORT_ASC, $name, SORT_ASC, $leaderboard);
70
        return $leaderboard;
71
    }
72
73
    function in_array_r($needle, $haystack, $strict = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
74
        foreach ($haystack as $key=>$item) {
75
            if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && $this->in_array_r($needle, $item, $strict))) {
76
                return $key;
77
            }
78
        }
79
80
        return false;
81
    }
82
83
84
}