Completed
Branch v1.x-dev (59bcf7)
by Benjamin
05:08
created

AbstractTeamRuleTrait::calculateTeamValue()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9.2055

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 33
ccs 19
cts 22
cp 0.8636
rs 8.0555
cc 9
nc 13
nop 2
crap 9.2055
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\Exception\NoVersionException;
13
use Obblm\Core\Helper\PlayerHelper;
14
use Obblm\Core\Helper\Rule\Roster\Roster;
15
16
/****************************
17
 * TEAM INFORMATION METHODS
18
 ***************************/
19
trait AbstractTeamRuleTrait
20
{
21
    abstract public function getRoster(Team $team):RosterInterface;
22
23
    /**
24
     * @return int
25
     */
26 1
    public function getMaxTeamCost():int
27
    {
28 1
        return ($this->rule['max_team_cost']) ? $this->rule['max_team_cost'] : TeamValue::LIMIT;
0 ignored issues
show
Bug introduced by
The type Obblm\Core\Helper\Rule\Traits\TeamValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
    }
30
31
    /**
32
     * @param Team $team
33
     * @return int
34
     */
35 1
    public function getRerollCost(Team $team):int
36
    {
37
        /** @var Roster $roster */
38 1
        $roster = $this->getRosters()->get($team->getRoster());
39 1
        return (int) $roster->getRerollCost();
40
    }
41
42
    /**
43
     * @param Team $team
44
     * @return int
45
     */
46 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

46
    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...
47
    {
48 1
        return (int) $this->rule['sidelines_cost']['apothecary'];
49
    }
50
51
    /**
52
     * @param Team $team
53
     * @return int
54
     */
55 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

55
    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...
56
    {
57 1
        return (int) $this->rule['sidelines_cost']['cheerleaders'];
58
    }
59
60
    /**
61
     * @param Team $team
62
     * @return int
63
     */
64 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

64
    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...
65
    {
66 1
        return (int) $this->rule['sidelines_cost']['assistants'];
67
    }
68
69
    /**
70
     * @param Team $team
71
     * @return int
72
     */
73 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

73
    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...
74
    {
75 1
        return (int) $this->rule['sidelines_cost']['popularity'];
76
    }
77
78
    /**
79
     * @param Team $team
80
     * @return bool
81
     * @throws \Exception
82
     */
83 1
    public function couldHaveApothecary(Team $team):bool
84
    {
85
        /** @var Roster $roster */
86 1
        $roster = $this->getRosters()->get($team->getRoster());
87 1
        return (bool) $roster->canHaveApothecary();
88
    }
89
90
    public function calculateTeamRate(TeamVersion $version):?int
91
    {
92
        return $this->calculateTeamValue($version) / 10000;
93
    }
94
95 2
    public function calculateTeamValue(TeamVersion $version, bool $excludeDisposable = false):int
96
    {
97 2
        if (!$version->getTeam()) {
98 1
            throw new InvalidArgumentException();
99
        }
100 1
        $value = 0;
101
        // Players
102 1
        $roster = $this->getRoster($version->getTeam());
103
        // TODO: Bug => players are not version's one (because of dead players)
104 1
        foreach ($version->getTeam()->getAvailablePlayers() as $player) {
105 1
            if ($player->getPosition()) {
106
                try {
107 1
                    $playerVersion = PlayerHelper::getLastVersion($player);
108 1
                } catch (NoVersionException $e) { // It's a new player !
109 1
                    $playerVersion = (new PlayerVersion());
110 1
                    $player->addVersion($playerVersion);
111 1
                    $version->addPlayerVersion($playerVersion);
112 1
                    $position = $roster->getPosition($player->getPosition());
113
                    $this->setPlayerDefaultValues($playerVersion, $position);
0 ignored issues
show
Bug introduced by
It seems like $position can also be of type null; however, parameter $position of Obblm\Core\Helper\Rule\T...etPlayerDefaultValues() does only seem to accept Obblm\Core\Contracts\PositionInterface, 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

113
                    $this->setPlayerDefaultValues($playerVersion, /** @scrutinizer ignore-type */ $position);
Loading history...
114
                }
115
                if (!$playerVersion->isMissingNextGame() && !($this->playerIsDisposable($playerVersion) && $excludeDisposable)) {
116
                    $value += $playerVersion->getValue();
117
                }
118
            }
119
        }
120
        // Sidelines
121 1
        $value += $version->getRerolls() * $this->getRerollCost($version->getTeam());
122 1
        $value += $version->getAssistants() * $this->getAssistantsCost($version->getTeam());
123 1
        $value += $version->getCheerleaders() * $this->getCheerleadersCost($version->getTeam());
124 1
        $value += $version->getPopularity() * $this->getPopularityCost($version->getTeam());
125 1
        $value += ($version->getApothecary()) ? $this->getApothecaryCost($version->getTeam()) : 0;
126
127 1
        return $value;
128
    }
129
130
    abstract public function setPlayerDefaultValues(PlayerVersion $version, PositionInterface $position): ?PlayerVersion;
131
    abstract public function playerIsDisposable(PlayerVersion $version):bool;
132
    /** @return RosterInterface[]|ArrayCollection */
133
    abstract public function getRosters():ArrayCollection;
134
135
    /**
136
     * @return array
137
     */
138
    public function getInjuriesTable():array
139
    {
140
        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

140
        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...
141
    }
142
143 2
    public function getMaxPlayersByType($rosterKey, $typeKey): int
144
    {
145
        /** @var PositionInterface $position */
146 2
        $position = $this->getRosters()->get($rosterKey)->getPosition($typeKey);
147 1
        return (int) $position->getMax();
148
    }
149
}
150