Match::simulate()   F
last analyzed

Complexity

Conditions 15
Paths 2700

Size

Total Lines 62
Code Lines 46

Duplication

Lines 8
Ratio 12.9 %

Importance

Changes 8
Bugs 1 Features 4
Metric Value
c 8
b 1
f 4
dl 8
loc 62
rs 2.9275
cc 15
eloc 46
nc 2700
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Lib\DsManager\Models;
4
5
use App\Lib\DsManager\Helpers\Randomizer;
6
use App\Lib\DsManager\Models\Common\DsManagerModel;
7
8
/**
9
 * Class Match
10
 * @package App\Lib\DsManager\Models
11
 */
12
class Match extends DsManagerModel
13
{
14
    /**
15
     * @var Team
16
     */
17
    private $homeTeam;
18
    /**
19
     * @var Team
20
     */
21
    private $awayTeam;
22
23
    /**
24
     * Match constructor.
25
     * @param Team $home
26
     * @param Team $away
27
     */
28
    public function __construct(Team $home, Team $away)
29
    {
30
        $this->homeTeam = $home;
31
        $this->awayTeam = $away;
32
    }
33
34
    /**
35
     * @return MatchResult
36
     */
37
    public function simulate()
38
    {
39
        $homePoints = $this->homeTeam->getAvgSkill();
40
        $awayPoints = $this->awayTeam->getAvgSkill();
41
42
        $homePoints += $this->malusModule(
43
            $this->homeTeam->coach->favouriteModule,
44
            $this->homeTeam->playersPerRoleArray()
45
        );
46
        $awayPoints += $this->malusModule(
47
            $this->awayTeam->coach->favouriteModule,
48
            $this->awayTeam->playersPerRoleArray()
49
        );
50
51
        $goalHome = 0;
52
        $goalAway = 0;
53
54
        if (Randomizer::boolOnPercentage(80)) {
55
            if (($homePoints - $awayPoints) < 0) {
56
                $goalAway = ($awayPoints - $homePoints) % 6;
57
                $goalHome += $this->chance();
58
                $goalAway += $this->chance();
59
                $goalHome += $this->bonusHome();
60
            } else {
61
                $goalHome = ($homePoints - $awayPoints) % 6;
62
                $goalAway += $this->chance();
63
                $goalHome += $this->bonusHome();
64
            }
65
        } else {
66
            $goalHome += $this->chance();
67
            $goalAway += $this->chance();
68
            $goalHome += $this->bonusHome();
69
        }
70
        $goalHome += $this->bonusAge($this->homeTeam);
71
        $goalAway += $this->bonusAge($this->awayTeam);
72
73
        //Bonus on Good GoalKeeper
74
        $goalies = $this->homeTeam->getBestPlayerForRole("GK");
75
        $goalAway -= $this->bonusGoalKeeper($goalies);
76
        $goalies = $this->awayTeam->getBestPlayerForRole("GK");
77
        $goalHome -= $this->bonusGoalKeeper($goalies);
78
        //
79
        $homeModule = new Module($this->homeTeam->coach->favouriteModule);
80
        $awayModule = new Module($this->awayTeam->coach->favouriteModule);
81 View Code Duplication
        if ($homeModule->isOffensive()) {
82
            $goalHome += Randomizer::boolOnPercentage(50) ? rand(1, 2) : 0;
83
            $goalAway += Randomizer::boolOnPercentage(20) ? 1 : 0;
84
        }
85 View Code Duplication
        if ($awayModule->isOffensive()) {
86
            $goalAway += Randomizer::boolOnPercentage(50) ? rand(1, 2) : 0;
87
            $goalHome += Randomizer::boolOnPercentage(20) ? 1 : 0;
88
        }
89
        if ($awayModule->isDefensive()) {
90
            $goalHome -= Randomizer::boolOnPercentage(50) ? 1 : 0;
91
        }
92
        if ($homeModule->isDefensive()) {
93
            $goalAway -= Randomizer::boolOnPercentage(50) ? 1 : 0;
94
        }
95
        $goalHome = $goalHome < 0 ? 0 : $goalHome;
96
        $goalAway = $goalAway < 0 ? 0 : $goalAway;
97
        return new MatchResult($goalHome, $goalAway, $this->homeTeam, $this->awayTeam);
98
    }
99
100
    /**
101
     * @param Team $team
102
     * @return int
103
     */
104
    private function bonusAge(Team $team)
105
    {
106
        if ($team->getAvgAge() > 29 || $team->getAvgAge() < 24) {
107
            return $this->chance();
108
        }
109
        return 0;
110
    }
111
112
    /**
113
     * @param $goalkeeper
114
     * @return int
115
     */
116
    private function bonusGoalKeeper($goalkeeper)
117
    {
118
        $skillGoalkeeper = empty($goalkeeper) ? 1 : $goalkeeper->skillAvg;
119
        return (Randomizer::boolOnPercentage($skillGoalkeeper) ? 1 : 0);
120
    }
121
122
    /**
123
     * @return int
124
     */
125
    private function chance()
126
    {
127
        return rand(0, 3);
128
    }
129
130
    /**
131
     * @return int
132
     */
133
    private function bonusHome()
134
    {
135
        return Randomizer::boolOnPercentage(66) ? 1 : 0;
136
    }
137
138
    /**
139
     * @param $moduleString
140
     * @param $playersRoleArray
141
     * @return int
142
     */
143
    private function malusModule($moduleString, $playersRoleArray)
144
    {
145
        $module = new Module($moduleString);
146
        if ($module->isApplicableToArray($playersRoleArray)) {
147
            return rand(1, 10);
148
        } else {
149
            return (-1) * rand(1, 10);
150
        }
151
    }
152
153
    /**
154
     * @param array $ormMatchArray
155
     * @return Match
156
     */
157
    public static function fromArray($ormMatchArray = [])
158
    {
159
        $homeTeam = Team::fromArray($ormMatchArray['home_team']);
160
        $awayTeam = Team::fromArray($ormMatchArray['away_team']);
161
        return new Match($homeTeam, $awayTeam);
162
    }
163
164
    /**
165
     * @return mixed
166
     */
167
    function toArray()
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...
168
    {
169
        $result = [];
170
        $result['home_team_id'] = $this->homeTeam->id;
171
        $result['away_team_id'] = $this->awayTeam->id;
172
        return $result;
173
    }
174
}