Completed
Branch develop (598d0f)
by Benjamin
03:23
created

AbstractTeamRuleTrait   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 134
rs 10
wmc 22

13 Methods

Rating   Name   Duplication   Size   Complexity  
A calculateTeamRate() 0 3 1
B calculateTeamValue() 0 27 8
A getMaxTeamCost() 0 3 2
A getRerollCost() 0 5 1
A getMaxPlayersByType() 0 8 2
A couldHaveApothecary() 0 5 1
A getAvailableRosters() 0 3 1
A getCheerleadersCost() 0 3 1
A getTeamAvailablePlayerTypes() 0 3 1
A getAssistantsCost() 0 3 1
A getApothecaryCost() 0 3 1
A getPopularityCost() 0 3 1
A getInjuriesTable() 0 3 1
1
<?php
2
3
namespace Obblm\Core\Helper\Rule\Traits;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Obblm\Core\Entity\PlayerVersion;
7
use Obblm\Core\Entity\Team;
8
use Obblm\Core\Entity\TeamVersion;
9
use Obblm\Core\Exception\NotFoundRuleKeyExcepion;
10
use Obblm\Core\Exception\NoVersionException;
11
use Obblm\Core\Helper\PlayerHelper;
12
use Obblm\Core\Helper\Rule\Roster\Roster;
13
use Obblm\Core\Validator\Constraints\TeamValue;
14
15
/****************************
16
 * TEAM INFORMATION METHODS
17
 ***************************/
18
trait AbstractTeamRuleTrait
19
{
20
    public function getAvailableRosters(): ArrayCollection
21
    {
22
        return $this->getRosters();
0 ignored issues
show
Bug introduced by
It seems like getRosters() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
        return $this->/** @scrutinizer ignore-call */ getRosters();
Loading history...
23
    }
24
25
    /**
26
     * @return int
27
     */
28
    public function getMaxTeamCost():int
29
    {
30
        return ($this->rule['max_team_cost']) ? $this->rule['max_team_cost'] : TeamValue::LIMIT;
31
    }
32
33
    /**
34
     * @param Team $team
35
     * @return array
36
     */
37
    public function getTeamAvailablePlayerTypes(Team $team)
38
    {
39
        return $this->getAvailablePlayerTypes($team->getRoster());
0 ignored issues
show
Bug introduced by
The method getAvailablePlayerTypes() does not exist on Obblm\Core\Helper\Rule\T...s\AbstractTeamRuleTrait. Did you maybe mean getAvailableRosters()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        return $this->/** @scrutinizer ignore-call */ getAvailablePlayerTypes($team->getRoster());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
    }
41
42
    /**
43
     * @param Team $team
44
     * @return int
45
     * @throws \Exception
46
     */
47
    public function getRerollCost(Team $team):int
48
    {
49
        /** @var Roster $roster */
50
        $roster = $this->getRosters()->get($team->getRoster());
51
        return (int) $roster->getRerollCost();
52
    }
53
54
    /**
55
     * @param Team $team
56
     * @return int
57
     */
58
    public function getApothecaryCost(Team $team):int
0 ignored issues
show
Unused Code introduced by
The parameter $team is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
    public function getApothecaryCost(/** @scrutinizer ignore-unused */ Team $team):int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
        return (int) $this->rule['sidelines_cost']['apothecary'];
61
    }
62
63
    /**
64
     * @param Team $team
65
     * @return int
66
     */
67
    public function getCheerleadersCost(Team $team):int
0 ignored issues
show
Unused Code introduced by
The parameter $team is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

67
    public function getCheerleadersCost(/** @scrutinizer ignore-unused */ Team $team):int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
    {
69
        return (int) $this->rule['sidelines_cost']['cheerleaders'];
70
    }
71
72
    /**
73
     * @param Team $team
74
     * @return int
75
     */
76
    public function getAssistantsCost(Team $team):int
0 ignored issues
show
Unused Code introduced by
The parameter $team is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
    public function getAssistantsCost(/** @scrutinizer ignore-unused */ Team $team):int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        return (int) $this->rule['sidelines_cost']['assistants'];
79
    }
80
81
    /**
82
     * @param Team $team
83
     * @return int
84
     */
85
    public function getPopularityCost(Team $team):int
0 ignored issues
show
Unused Code introduced by
The parameter $team is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

85
    public function getPopularityCost(/** @scrutinizer ignore-unused */ Team $team):int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
        return (int) $this->rule['sidelines_cost']['popularity'];
88
    }
89
90
    /**
91
     * @param Team $team
92
     * @return bool
93
     * @throws \Exception
94
     */
95
    public function couldHaveApothecary(Team $team):bool
96
    {
97
        /** @var Roster $roster */
98
        $roster = $this->getRosters()->get($team->getRoster());
99
        return (bool) $roster->canHaveApothecary();
100
    }
101
102
    public function calculateTeamRate(TeamVersion $version):?int
103
    {
104
        return $this->calculateTeamValue($version) / 10000;
105
    }
106
107
    public function calculateTeamValue(TeamVersion $version, bool $excludeDisposable = false):int
108
    {
109
        $team_cost = 0;
110
        // Players
111
        foreach ($version->getTeam()->getAvailablePlayers() as $basePlayer) {
112
            if ($basePlayer->getType()) {
113
                $player = (new PlayerVersion());
0 ignored issues
show
Unused Code introduced by
The assignment to $player is dead and can be removed.
Loading history...
114
                try {
115
                    $player = PlayerHelper::getLastVersion($basePlayer);
116
                } catch (NoVersionException $e) { // It's a new player !
117
                    $basePlayer->addVersion($player);
118
                    $version->addPlayerVersion($player);
119
                    $this->setPlayerDefaultValues($player);
0 ignored issues
show
Bug introduced by
It seems like setPlayerDefaultValues() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
                    $this->/** @scrutinizer ignore-call */ 
120
                           setPlayerDefaultValues($player);
Loading history...
120
                }
121
                if (!$player->isMissingNextGame() && !($this->playerIsDisposable($player) && $excludeDisposable)) {
0 ignored issues
show
Bug introduced by
It seems like playerIsDisposable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
                if (!$player->isMissingNextGame() && !($this->/** @scrutinizer ignore-call */ playerIsDisposable($player) && $excludeDisposable)) {
Loading history...
122
                    $team_cost += $player->getValue();
123
                }
124
            }
125
        }
126
        // Sidelines
127
        $team_cost += $version->getRerolls() * $this->getRerollCost($version->getTeam());
0 ignored issues
show
Bug introduced by
It seems like $version->getTeam() can also be of type null; however, parameter $team of Obblm\Core\Helper\Rule\T...eTrait::getRerollCost() does only seem to accept Obblm\Core\Entity\Team, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
        $team_cost += $version->getRerolls() * $this->getRerollCost(/** @scrutinizer ignore-type */ $version->getTeam());
Loading history...
128
        $team_cost += $version->getAssistants() * $this->getAssistantsCost($version->getTeam());
0 ignored issues
show
Bug introduced by
It seems like $version->getTeam() can also be of type null; however, parameter $team of Obblm\Core\Helper\Rule\T...it::getAssistantsCost() does only seem to accept Obblm\Core\Entity\Team, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
        $team_cost += $version->getAssistants() * $this->getAssistantsCost(/** @scrutinizer ignore-type */ $version->getTeam());
Loading history...
129
        $team_cost += $version->getCheerleaders() * $this->getCheerleadersCost($version->getTeam());
0 ignored issues
show
Bug introduced by
It seems like $version->getTeam() can also be of type null; however, parameter $team of Obblm\Core\Helper\Rule\T...::getCheerleadersCost() does only seem to accept Obblm\Core\Entity\Team, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
        $team_cost += $version->getCheerleaders() * $this->getCheerleadersCost(/** @scrutinizer ignore-type */ $version->getTeam());
Loading history...
130
        $team_cost += $version->getPopularity() * $this->getPopularityCost($version->getTeam());
0 ignored issues
show
Bug introduced by
It seems like $version->getTeam() can also be of type null; however, parameter $team of Obblm\Core\Helper\Rule\T...it::getPopularityCost() does only seem to accept Obblm\Core\Entity\Team, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
        $team_cost += $version->getPopularity() * $this->getPopularityCost(/** @scrutinizer ignore-type */ $version->getTeam());
Loading history...
131
        $team_cost += ($version->getApothecary()) ? $this->getApothecaryCost($version->getTeam()) : 0;
0 ignored issues
show
Bug introduced by
It seems like $version->getTeam() can also be of type null; however, parameter $team of Obblm\Core\Helper\Rule\T...it::getApothecaryCost() does only seem to accept Obblm\Core\Entity\Team, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
        $team_cost += ($version->getApothecary()) ? $this->getApothecaryCost(/** @scrutinizer ignore-type */ $version->getTeam()) : 0;
Loading history...
132
133
        return $team_cost;
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function getInjuriesTable():array
140
    {
141
        return $this->getInjuries();
0 ignored issues
show
Bug introduced by
The method getInjuries() does not exist on Obblm\Core\Helper\Rule\T...s\AbstractTeamRuleTrait. Did you maybe mean getInjuriesTable()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        return $this->/** @scrutinizer ignore-call */ getInjuries();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
    }
143
144
    public function getMaxPlayersByType($roster_key, $type_key): int
145
    {
146
        /** @var Roster $roster */
147
        $roster = $this->getRosters()->get($roster_key);
148
        if (!$type = $roster->getPlayerTypes()[$type_key]) {
149
            throw new NotFoundRuleKeyExcepion($type_key, 'toto');
0 ignored issues
show
Bug introduced by
'toto' of type string is incompatible with the type integer expected by parameter $code of Obblm\Core\Exception\Not...Excepion::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
            throw new NotFoundRuleKeyExcepion($type_key, /** @scrutinizer ignore-type */ 'toto');
Loading history...
150
        }
151
        return (int) $type['max'];
152
    }
153
}
154