Completed
Branch v1.x-dev (5c2708)
by Benjamin
04:14
created

AbstractTeamRuleTrait::getMaxTeamCost()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
cc 4
nc 3
nop 1
crap 4.25
1
<?php
2
3
namespace Obblm\Core\Helper\Rule\Traits;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Obblm\Core\Contracts\PositionInterface;
7
use Obblm\Core\Contracts\RosterInterface;
8
use Obblm\Core\Entity\PlayerVersion;
9
use Obblm\Core\Entity\Team;
10
use Obblm\Core\Entity\TeamVersion;
11
use Obblm\Core\Exception\InvalidArgumentException;
12
use Obblm\Core\Helper\PlayerHelper;
13
use Obblm\Core\Helper\Rule\Roster\Roster;
14
use Obblm\Core\Validator\Constraints\Team\AdditionalSkills;
15
use Obblm\Core\Validator\Constraints\Team\Value;
16
17
/****************************
18
 * TEAM INFORMATION METHODS
19
 ***************************/
20
trait AbstractTeamRuleTrait
21
{
22
    abstract public function getRoster(Team $team):RosterInterface;
23
24
    /**
25
     * @return int
26
     */
27 1
    public function getMaxTeamCost(Team $team = null):int
28
    {
29 1
        if ($team && $team->getCreationOption('max_team_cost')) {
30
            return $team->getCreationOption('max_team_cost');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $team->getCreationOption('max_team_cost') could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
31
        }
32
33 1
        return ($this->rule['max_team_cost']) ? $this->rule['max_team_cost'] : Value::LIMIT;
34
    }
35
36
    /**
37
     * @param Team $team
38
     * @return int
39
     */
40 1
    public function getRerollCost(Team $team):int
41
    {
42
        /** @var Roster $roster */
43 1
        $roster = $this->getRosters()->get($team->getRoster());
44 1
        return (int) $roster->getRerollCost();
45
    }
46
47
    /**
48
     * @param Team $team
49
     * @return int
50
     */
51 1
    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

51
    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...
52
    {
53 1
        return (int) $this->rule['sidelines_cost']['apothecary'];
54
    }
55
56
    /**
57
     * @param Team $team
58
     * @return int
59
     */
60 1
    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

60
    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...
61
    {
62 1
        return (int) $this->rule['sidelines_cost']['cheerleaders'];
63
    }
64
65
    /**
66
     * @param Team $team
67
     * @return int
68
     */
69 1
    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

69
    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...
70
    {
71 1
        return (int) $this->rule['sidelines_cost']['assistants'];
72
    }
73
74
    /**
75
     * @param Team $team
76
     * @return int
77
     */
78 1
    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

78
    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...
79
    {
80 1
        return (int) $this->rule['sidelines_cost']['popularity'];
81
    }
82
83
    /**
84
     * @param Team $team
85
     * @return bool
86
     * @throws \Exception
87
     */
88 1
    public function couldHaveApothecary(Team $team):bool
89
    {
90
        /** @var Roster $roster */
91 1
        $roster = $this->getRosters()->get($team->getRoster());
92 1
        return (bool) $roster->canHaveApothecary();
93
    }
94
95
    public function calculateTeamRate(TeamVersion $version):?int
96
    {
97
        return $this->calculateTeamValue($version) / 10000;
98
    }
99
100
    public function calculateInducementsCost(array $inducements):int
101
    {
102
        $cost = 0;
103
        foreach ($inducements as $key) {
104
            $cost += $this->getInducement($key)->getValue();
0 ignored issues
show
Bug introduced by
It seems like getInducement() 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

104
            $cost += $this->/** @scrutinizer ignore-call */ getInducement($key)->getValue();
Loading history...
105
        }
106
        return $cost;
107
    }
108
109
    public function updatePlayerVersionCost(PlayerVersion $playerVersion)
110
    {
111
        $position = $this->getPlayerPosition($playerVersion->getPlayer());
0 ignored issues
show
Bug introduced by
It seems like getPlayerPosition() 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

111
        /** @scrutinizer ignore-call */ 
112
        $position = $this->getPlayerPosition($playerVersion->getPlayer());
Loading history...
112
        if ($playerVersion->getPlayer()->getTeam()->getCreationOption('skills_allowed') && $playerVersion->getPlayer()->getTeam()->getCreationOption('skills_allowed.choice') == AdditionalSkills::NOT_FREE) {
113
            if (!$playerVersion->isHiredStarPlayer() && !$playerVersion->getPlayer()->isStarPlayer()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $playerVersion->getPlayer()->isStarPlayer() of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
114
                $extra = $this->getPlayerVersionExtraCosts($playerVersion);
0 ignored issues
show
Bug introduced by
It seems like getPlayerVersionExtraCosts() 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

114
                /** @scrutinizer ignore-call */ 
115
                $extra = $this->getPlayerVersionExtraCosts($playerVersion);
Loading history...
115
                $playerVersion->setValue($position->getCost() + $extra);
116
            }
117
        }
118
    }
119
120 2
    public function calculateTeamValue(TeamVersion $version, bool $excludeDisposable = false):int
121
    {
122 2
        if (!$version->getTeam()) {
123 1
            throw new InvalidArgumentException();
124
        }
125 1
        $value = 0;
126
        // Players
127 1
        $roster = $this->getRoster($version->getTeam());
0 ignored issues
show
Unused Code introduced by
The assignment to $roster is dead and can be removed.
Loading history...
128 1
        foreach ($version->getNotDeadPlayerVersions() as $playerVersion) {
129
            $player = $playerVersion->getPlayer();
130
            if ($player->getPosition()) {
131
                $playerVersion = PlayerHelper::getLastVersion($player);
132
                //$this->updatePlayerVersionCost($playerVersion);
133
                if (!$playerVersion->isMissingNextGame() && !($this->playerIsDisposable($playerVersion) && $excludeDisposable)) {
134
                    $value += $playerVersion->getValue();
135
                }
136
            }
137
        }
138
        // Sidelines
139 1
        $value += $version->getRerolls() * $this->getRerollCost($version->getTeam());
140 1
        $value += $version->getAssistants() * $this->getAssistantsCost($version->getTeam());
141 1
        $value += $version->getCheerleaders() * $this->getCheerleadersCost($version->getTeam());
142 1
        $value += $version->getPopularity() * $this->getPopularityCost($version->getTeam());
143 1
        $value += ($version->getApothecary()) ? $this->getApothecaryCost($version->getTeam()) : 0;
144
145 1
        return $value;
146
    }
147
148
    abstract public function setPlayerDefaultValues(PlayerVersion $version, PositionInterface $position): ?PlayerVersion;
149
    abstract public function playerIsDisposable(PlayerVersion $version):bool;
150
    /** @return RosterInterface[]|ArrayCollection */
151
    abstract public function getRosters():ArrayCollection;
152
153
    /**
154
     * @return array
155
     */
156
    public function getInjuriesTable():array
157
    {
158
        return (array) $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

158
        return (array) $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...
159
    }
160
161 2
    public function getMaxPlayersByType($rosterKey, $typeKey): int
162
    {
163
        /** @var PositionInterface $position */
164 2
        $position = $this->getRosters()->get($rosterKey)->getPosition($typeKey);
165 1
        return (int) $position->getMax();
166
    }
167
168
    public function applyTeamExtraCosts(TeamVersion $version, $creationPhase = false)
169
    {
170
        if ($creationPhase &&
171
            $version->getTeam()->getCreationOption('skills_allowed') &&
172
            $version->getTeam()->getCreationOption('skills_allowed.choice') == AdditionalSkills::NOT_FREE) {
173
            foreach ($version->getNotDeadPlayerVersions() as $playerVersion) {
174
                if (!$playerVersion->isHiredStarPlayer() && !$playerVersion->getPlayer()->isStarPlayer()) {
175
                    $extra = $this->getPlayerVersionExtraCosts($playerVersion);
176
                    $position = $this->getPlayerPosition($playerVersion->getPlayer());
177
                    $playerVersion->setValue($position->getCost() + $extra);
178
                }
179
            }
180
        }
181
    }
182
}
183