Completed
Push — master ( a5c576...7c819e )
by Vincenzo
02:22
created

Match::simulate()   F

Complexity

Conditions 15
Paths 2700

Size

Total Lines 70
Code Lines 46

Duplication

Lines 8
Ratio 11.43 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 8
loc 70
rs 2.4837
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
7
/**
8
 * Class Match
9
 * @package App\Lib\DsManager\Models
10
 */
11
class Match
12
{
13
    /**
14
     * @var Team
15
     */
16
    private $homeTeam;
17
    /**
18
     * @var Team
19
     */
20
    private $awayTeam;
21
22
    /**
23
     * Match constructor.
24
     * @param Team $home
25
     * @param Team $away
26
     */
27
    public function __construct(Team $home, Team $away)
28
    {
29
        $this->homeTeam = $home;
30
        $this->awayTeam = $away;
31
    }
32
33
    /**
34
     * @return MatchResult
35
     */
36
    public function simulate()
37
    {
38
        $homePoints = $this->homeTeam->getAvgSkill();
39
        $awayPoints = $this->awayTeam->getAvgSkill();
40
41
        $homePoints += $this->malusModule(
42
            $this->homeTeam->coach->favouriteModule,
43
            $this->homeTeam->playersPerRoleArray()
44
        );
45
        $awayPoints += $this->malusModule(
46
            $this->awayTeam->coach->favouriteModule,
47
            $this->awayTeam->playersPerRoleArray()
48
        );
49
50
        $goalHome = 0;
51
        $goalAway = 0;
52
53
        if (Randomizer::boolOnPercentage(80)) {
54
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
66
        } else {
67
            $goalHome += $this->chance();
68
            $goalAway += $this->chance();
69
            $goalHome += $this->bonusHome();
70
        }
71
72
        $goalHome += $this->bonusAge($this->homeTeam);
73
        $goalAway += $this->bonusAge($this->awayTeam);
74
75
76
        //Bonus on Good GoalKeeper
77
        $goalies = $this->homeTeam->getBestPlayerForRole("GK");
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $goalies is correct as $this->homeTeam->getBestPlayerForRole('GK') (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...
78
        $goalAway -= $this->bonusGoalkeeper($goalies);
79
        $goalies = $this->awayTeam->getBestPlayerForRole("GK");
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $goalies is correct as $this->awayTeam->getBestPlayerForRole('GK') (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...
80
        $goalHome -= $this->bonusGoalkeeper($goalies);
81
        //
82
83
        $homeModule = new Module($this->homeTeam->coach->favouriteModule);
84
        $awayModule = new Module($this->awayTeam->coach->favouriteModule);
85
86 View Code Duplication
        if ($homeModule->isOffensive()) {
1 ignored issue
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...
87
            $goalHome += Randomizer::boolOnPercentage(50) ? rand(1, 2) : 0;
88
            $goalAway += Randomizer::boolOnPercentage(20) ? 1 : 0;
89
        }
90 View Code Duplication
        if ($awayModule->isOffensive()) {
1 ignored issue
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...
91
            $goalAway += Randomizer::boolOnPercentage(50) ? rand(1, 2) : 0;
92
            $goalHome += Randomizer::boolOnPercentage(20) ? 1 : 0;
93
        }
94
95
        if ($awayModule->isDefensive()) {
96
            $goalHome -= Randomizer::boolOnPercentage(50) ? 1 : 0;
97
        }
98
        if ($homeModule->isDefensive()) {
99
            $goalAway -= Randomizer::boolOnPercentage(50) ? 1 : 0;
100
        }
101
102
        $goalHome = $goalHome < 0 ? 0 : $goalHome;
103
        $goalAway = $goalAway < 0 ? 0 : $goalAway;
104
        return new MatchResult($goalHome, $goalAway, $this->homeTeam, $this->awayTeam);
105
    }
106
107
    private function bonusAge(Team $team)
108
    {
109
        if ($team->getAvgAge() > 29 || $team->getAvgAge() < 24) {
110
            return $this->chance();
111
        }
112
        return 0;
113
    }
114
115
    /**
116
     * @param $goalkeeper
117
     * @return int
118
     */
119
    private function bonusGoalKeeper($goalkeeper)
120
    {
121
122
        $skillGoalkeeper = empty($goalkeeper) ? 1 : $goalkeeper->skillAvg;
123
        return (Randomizer::boolOnPercentage($skillGoalkeeper) ? 1 : 0);
124
125
    }
126
127
    /**
128
     * @return int
129
     */
130
    private function chance()
131
    {
132
        return rand(0, 3);
133
    }
134
135
    /**
136
     * @return int
137
     */
138
    private function bonusHome()
139
    {
140
        return Randomizer::boolOnPercentage(66) ? 1 : 0;
141
    }
142
143
    /**
144
     * @param $moduleString
145
     * @param $playersRoleArray
146
     * @return int
147
     */
148
    private function malusModule($moduleString, $playersRoleArray)
149
    {
150
        $module = new Module($moduleString);
151
        if ($module->isApplicableToArray($playersRoleArray)) {
152
            return rand(1, 10);
153
        } else {
154
            return (-1) * rand(1, 10);
155
        }
156
    }
157
158
}