Passed
Push — master ( 82efe3...775557 )
by Paweł
03:11
created

Player::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 10
c 4
b 0
f 1
dl 0
loc 26
ccs 3
cts 3
cp 1
rs 9.9332
cc 1
nc 1
nop 14
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Player;
6
7
use AardsGerds\Game\Build\Attribute\AttributePoints;
8
use AardsGerds\Game\Build\Attribute\Etherum;
9
use AardsGerds\Game\Build\Attribute\Health;
10
use AardsGerds\Game\Build\Attribute\Initiative;
11
use AardsGerds\Game\Build\Attribute\Strength;
12
use AardsGerds\Game\Build\Experience;
13
use AardsGerds\Game\Build\Level;
14
use AardsGerds\Game\Build\LevelProgress;
15
use AardsGerds\Game\Build\Talent\SecretKnowledge\Ascension;
16
use AardsGerds\Game\Build\Talent\TalentCollection;
17
use AardsGerds\Game\Build\Talent\TalentPoints;
18
use AardsGerds\Game\Build\Talent\WeaponMastery\ShortSword\Novice\Slash;
19
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMastery;
20
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMasteryLevel;
21
use AardsGerds\Game\Entity\Entity;
22
use AardsGerds\Game\Event\Story\FirstChapter\MercenaryCamp\MercenaryCampVisitEvent;
23
use AardsGerds\Game\Event\VisitEvent;
24
use AardsGerds\Game\Inventory\Alchemy\Potion\HealthPotion;
25
use AardsGerds\Game\Inventory\Inventory;
26
use AardsGerds\Game\Inventory\Weapon\ShortSword\RustyShortSword;
27
use AardsGerds\Game\Inventory\Weapon\Weapon;
28
use AardsGerds\Game\Shared\IntegerValueException;
29
30
final class Player extends Entity
31
{
32 4
    public function __construct(
33
        string $name,
34
        Health $health,
35
        Etherum $etherum,
36
        Strength $strength,
37
        Initiative $initiative,
38
        TalentCollection $talentCollection,
39
        Inventory $inventory,
40
        ?Weapon $weapon,
41
        bool $corrupted,
42
        private LevelProgress $levelProgress,
43
        private Health $maximumHealth,
44
        private AttributePoints $attributePoints,
45
        private TalentPoints $talentPoints,
46
        private VisitEvent $checkpoint,
47
    ) {
48 4
        parent::__construct(
49 4
            $name,
50
            $health,
51
            $etherum,
52
            $strength,
53
            $initiative,
54
            $talentCollection,
55
            $inventory,
56
            $weapon,
57
            $corrupted,
58
        );
59 4
    }
60
61 3
    public static function new(string $name): self
62
    {
63 3
        return new self(
64 3
            $name,
65 3
            new Health(100),
66 3
            new Etherum(1),
67 3
            new Strength(5),
68 3
            new Initiative(10),
69 3
            new TalentCollection([
70 3
                WeaponMastery::shortSword(WeaponMasteryLevel::novice()),
71 3
                new Slash(),
72
            ]),
73 3
            new Inventory([new HealthPotion(), new HealthPotion()]),
74 3
            new RustyShortSword(),
75 3
            false,
76 3
            new LevelProgress(
77 3
                new Level(1),
78 3
                new Experience(0),
79
            ),
80 3
            new Health(100),
81 3
            new AttributePoints(0),
82 3
            new TalentPoints(0),
83 3
            new MercenaryCampVisitEvent(),
84
        );
85
    }
86
87 1
    public function getLevelProgress(): LevelProgress
88
    {
89 1
        return $this->levelProgress;
90
    }
91
92
    public function increaseExperience(Experience $experience, PlayerAction $playerAction): void
93
    {
94
        $this->levelProgress->increase($experience, $this, $playerAction);
95
    }
96
97 1
    public function getMaximumHealth(): Health
98
    {
99 1
        return $this->maximumHealth;
100
    }
101
102 1
    public function getAttributePoints(): AttributePoints
103
    {
104 1
        return $this->attributePoints;
105
    }
106
107 1
    public function getTalentPoints(): TalentPoints
108
    {
109 1
        return $this->talentPoints;
110
    }
111
112 1
    public function getCheckpoint(): VisitEvent
113
    {
114 1
        return $this->checkpoint;
115
    }
116
117
    public function setCheckpoint(VisitEvent $checkpoint): void
118
    {
119
        $this->checkpoint = $checkpoint;
120
    }
121
122 1
    public function heal(Health $health): void
123
    {
124 1
        $this->health->increaseBy($health);
125
126 1
        if ($this->health->isGreaterThan($this->maximumHealth)) {
127 1
            $this->healCompletely();
128
        }
129 1
    }
130
131 1
    public function healCompletely(): void
132
    {
133 1
        $this->health->replaceWith($this->maximumHealth);
134 1
    }
135
136
    /**
137
     * @throws PlayerException
138
     */
139 4
    public function increaseEtherum(Etherum $etherum): void
140
    {
141 4
        $this->etherum->increaseBy($etherum);
142
143 4
        if ($this->isCorrupted()) {
144
            return;
145
        }
146
147 4
        $playerAscension = $this->talentCollection->findSecretKnowledge()?->getAscension()
148 1
            ?? throw PlayerException::etherumOverdose();
149
150 3
        $corruptionBoundary = $this->calculateCorruptionBoundary();
151
152 3
        if ($this->etherum->isGreaterThanOrEqual($corruptionBoundary)) {
153 3
            if ($playerAscension->isLowerThan(Ascension::sixthAscension())) {
154 1
                throw PlayerException::etherumOverdose();
155
            }
156
157 2
            $this->corrupted = true;
158
        }
159 2
    }
160
161 3
    private function calculateCorruptionBoundary(): Etherum
162
    {
163 3
        $ascension = $this->talentCollection->findSecretKnowledge()?->getAscension();
164 3
        if ($ascension === null) {
165
            return new Etherum(2);
166
        }
167
168
        try {
169
170 3
            $nextAscensionEtherum = (new Ascension($ascension->get()))->increment()->getRequiredEtherum();
171 1
        } catch (IntegerValueException) {
172
            // entity has 8th ascension
173 1
            $nextAscensionEtherum = new Etherum(Ascension::eighthAscension()->getRequiredEtherum()->get() * 2);
174
        }
175
176
        // etherum required by next ascension + 0.5 x etherum required by next ascension
177 3
        return $nextAscensionEtherum->increaseBy(
178 3
            new Etherum((int) ($nextAscensionEtherum->get() * 0.5)),
179
        );
180
    }
181
}
182