Completed
Push — master ( 01ad84...898da4 )
by Vincenzo
02:22
created

MatchResult::getWinnerLoser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 4
Metric Value
c 4
b 0
f 4
dl 0
loc 23
rs 9.0856
cc 3
eloc 18
nc 3
nop 0
1
<?php
2
3
namespace App\Lib\DsManager\Models;
4
5
use App\Lib\DsManager\Helpers\Randomizer;
6
use App\Lib\Helpers\Config;
7
8
9
/**
10
 * Class MatchResult
11
 * @package App\Lib\DsManager\Models
12
 */
13
class MatchResult
14
{
15
    /**
16
     * @var
17
     */
18
    private $goalHome;
19
    /**
20
     * @var
21
     */
22
    private $goalAway;
23
    /**
24
     * @var Team
25
     */
26
    private $homeTeam;
27
    /**
28
     * @var Team
29
     */
30
    private $awayTeam;
31
32
    /**
33
     * MatchResult constructor.
34
     * @param $goalHome
35
     * @param $goalAway
36
     * @param Team $home
37
     * @param Team $away
38
     */
39
    public function __construct($goalHome, $goalAway, Team $home, Team $away)
40
    {
41
        $this->goalHome = $goalHome;
42
        $this->goalAway = $goalAway;
43
        $this->homeTeam = $home;
44
        $this->awayTeam = $away;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getWinnerLoser()
51
    {
52
        $isDraw = false;
53
        $winner = null;
1 ignored issue
show
Unused Code introduced by
$winner is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
        $loser = null;
1 ignored issue
show
Unused Code introduced by
$loser is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
        if ($this->goalAway == $this->goalHome) {
56
            $isDraw = true;
57
            $winner = $this->homeTeam;
58
            $loser = $this->awayTeam;
59
        } else if ($this->goalHome > $this->goalAway) {
60
            $winner = $this->homeTeam;
61
            $loser = $this->awayTeam;
62
        } else {
63
            $winner = $this->awayTeam;
64
            $loser = $this->homeTeam;
65
        }
66
67
        return [
68
            'isDraw' => $isDraw,
69
            'winner' => $this->cleanTeam($winner->toArray()),
70
            'loser' => $this->cleanTeam($loser->toArray())
71
        ];
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function toArray()
78
    {
79
        $result = [];
80
        $result["goalHome"] = $this->goalHome;
81
        $result["goalAway"] = $this->goalAway;
82
        $result['info'] = $this->getWinnerLoser();
83
        $result['info']['scorers'] = $this->getScorers();
84
        return $result;
85
    }
86
87
    /**
88
     * @param array $team
89
     * @return array
90
     */
91
    private function cleanTeam(array $team)
92
    {
93
        $fieldsToClean = [
94
            'coach',
95
            'roster',
96
        ];
97
98
        foreach ($fieldsToClean as $field) {
99
            if (array_key_exists($field, $team)) {
100
                unset($team[$field]);
101
            }
102
        }
103
        return $team;
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    private function getScorers()
110
    {
111
        $scorers = [
112
            'home' => [],
113
            'away' => []
114
        ];
115
        for ($i = 0; $i < $this->goalHome; $i++) {
116
            $scorers['home'][] = $this->pickAScorer($this->homeTeam);
117
        }
118
        for ($i = 0; $i < $this->goalAway; $i++) {
119
            $scorers['away'][] = $this->pickAScorer($this->awayTeam);
120
        }
121
        return $scorers;
122
    }
123
124
    /**
125
     * @param Team $team
126
     * @return Player
127
     */
128
    private function pickAScorer(Team $team)
129
    {
130
        $player = null;
1 ignored issue
show
Unused Code introduced by
$player is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
131
        if (Randomizer::boolOnPercentage(70)) {
132
            $roles = Config::get('modules.roles');
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $roles is correct as \App\Lib\Helpers\Config::get('modules.roles') (which targets App\Lib\Helpers\Config::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
133
            $forwards = array_splice($roles, count($roles) / 2);
134
            $pos = array_rand($forwards);
135
            unset($forwards[$pos]);
136
            $player = $team->getBestPlayerForRole($pos);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $player is correct as $team->getBestPlayerForRole($pos) (which targets App\Lib\DsManager\Models...:getBestPlayerForRole()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
137
            while (empty($player)) {
138
                if (!empty($forwards)) {
139
                    $pos = array_rand($forwards);
140
                    unset($forwards[$pos]);
141
                    $player = $team->getBestPlayerForRole($pos);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $player is correct as $team->getBestPlayerForRole($pos) (which targets App\Lib\DsManager\Models...:getBestPlayerForRole()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
142
                } else {
143
                    $player = $team->roster[array_rand($team->roster)];
144
                }
145
            }
146
        } else {
147
            $player = $team->roster[array_rand($team->roster)];
148
        }
149
        return $player;
150
    }
151
152
}