Player::__construct()   A
last analyzed

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\Corruption;
22
use AardsGerds\Game\Entity\Entity;
23
use AardsGerds\Game\Event\Story\FirstChapter\MercenaryCamp\MercenaryCampVisitEvent;
24
use AardsGerds\Game\Event\VisitEvent;
25
use AardsGerds\Game\Inventory\Alchemy\Potion\HealthPotion;
26
use AardsGerds\Game\Inventory\Inventory;
27
use AardsGerds\Game\Inventory\Weapon\ShortSword\RustyShortSword;
28
use AardsGerds\Game\Inventory\Weapon\Weapon;
29
use AardsGerds\Game\Shared\IntegerValueException;
30
31
final class Player extends Entity
32
{
33 4
    public function __construct(
34
        string $name,
35
        Health $health,
36
        Etherum $etherum,
37
        Strength $strength,
38
        Initiative $initiative,
39
        TalentCollection $talentCollection,
40
        Inventory $inventory,
41
        ?Weapon $weapon,
42
        ?Corruption $corruption,
43
        private LevelProgress $levelProgress,
44
        private Health $maximumHealth,
45
        private AttributePoints $attributePoints,
46
        private TalentPoints $talentPoints,
47
        private VisitEvent $checkpoint,
48
    ) {
49 4
        parent::__construct(
50 4
            $name,
51
            $health,
52
            $etherum,
53
            $strength,
54
            $initiative,
55
            $talentCollection,
56
            $inventory,
57
            $weapon,
58
            $corruption,
59
        );
60 4
    }
61
62 3
    public static function new(string $name): self
63
    {
64 3
        return new self(
65 3
            $name,
66 3
            new Health(100),
67 3
            new Etherum(1),
68 3
            new Strength(5),
69 3
            new Initiative(10),
70 3
            new TalentCollection([
71 3
                WeaponMastery::shortSword(WeaponMasteryLevel::novice()),
72 3
                new Slash(),
73
            ]),
74 3
            new Inventory([new HealthPotion(), new HealthPotion()]),
75 3
            new RustyShortSword(),
76 3
            null,
77 3
            new LevelProgress(
78 3
                new Level(1),
79 3
                new Experience(0),
80
            ),
81 3
            new Health(100),
82 3
            new AttributePoints(0),
83 3
            new TalentPoints(0),
84 3
            new MercenaryCampVisitEvent(),
85
        );
86
    }
87
88 1
    public function getLevelProgress(): LevelProgress
89
    {
90 1
        return $this->levelProgress;
91
    }
92
93
    public function increaseExperience(Experience $experience, PlayerAction $playerAction): void
94
    {
95
        $this->levelProgress->increase($experience, $this, $playerAction);
96
    }
97
98 1
    public function getMaximumHealth(): Health
99
    {
100 1
        return $this->maximumHealth;
101
    }
102
103 1
    public function getAttributePoints(): AttributePoints
104
    {
105 1
        return $this->attributePoints;
106
    }
107
108 1
    public function getTalentPoints(): TalentPoints
109
    {
110 1
        return $this->talentPoints;
111
    }
112
113 1
    public function getCheckpoint(): VisitEvent
114
    {
115 1
        return $this->checkpoint;
116
    }
117
118
    public function setCheckpoint(VisitEvent $checkpoint): void
119
    {
120
        $this->checkpoint = $checkpoint;
121
    }
122
123 1
    public function heal(Health $health): void
124
    {
125 1
        $this->health->increaseBy($health);
126
127 1
        if ($this->health->isGreaterThan($this->maximumHealth)) {
128 1
            $this->healCompletely();
129
        }
130 1
    }
131
132 1
    public function healCompletely(): void
133
    {
134 1
        $this->health->replaceWith($this->maximumHealth);
135 1
    }
136
137
    /**
138
     * @throws PlayerException
139
     */
140 4
    public function increaseEtherum(Etherum $etherum): void
141
    {
142 4
        $this->etherum->increaseBy($etherum);
143
144 4
        $playerAscension = $this->talentCollection->findSecretKnowledge()?->getAscension()
145 1
            ?? throw PlayerException::etherumOverdose();
146
147 3
        $corruptionBoundary = $this->calculateCorruptionBoundary();
148
149 3
        if ($this->etherum->isGreaterThanOrEqual($corruptionBoundary)) {
150 3
            if ($this->corruption !== null) {
151
                try {
152
                    $this->corruption->increment();
153
                } catch (IntegerValueException) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
154
            }
155
156 3
            if ($playerAscension->isLowerThan(Ascension::sixthAscension())) {
157 1
                throw PlayerException::etherumOverdose();
158
            }
159
160 2
            $this->corruption = Corruption::low();
161
        }
162 2
    }
163
}
164