Passed
Push — master ( 9137c7...d6bc1c )
by Paweł
03:29
created

Player::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 1
eloc 9
c 4
b 0
f 1
nc 1
nop 11
dl 0
loc 22
ccs 3
cts 3
cp 1
crap 1
rs 9.9666

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\Strength;
11
use AardsGerds\Game\Build\Experience;
12
use AardsGerds\Game\Build\Level;
13
use AardsGerds\Game\Build\LevelProgress;
14
use AardsGerds\Game\Build\Talent\SecretKnowledge\Ascension;
15
use AardsGerds\Game\Build\Talent\TalentCollection;
16
use AardsGerds\Game\Build\Talent\TalentPoints;
17
use AardsGerds\Game\Entity\Entity;
18
use AardsGerds\Game\Inventory\Inventory;
19
use AardsGerds\Game\Inventory\Weapon\Weapon;
20
use AardsGerds\Game\Shared\IntegerValueException;
21
22
final class Player extends Entity
23
{
24 1
    public function __construct(
25
        string $name,
26
        Health $health,
27
        Etherum $etherum,
28
        Strength $strength,
29
        TalentCollection $talentCollection,
30
        Inventory $inventory,
31
        ?Weapon $weapon,
32
        bool $corrupted,
33
        private LevelProgress $levelProgress,
34
        private AttributePoints $attributePoints,
35
        private TalentPoints $talentPoints,
36
    ) {
37 1
        parent::__construct(
38 1
            $name,
39
            $health,
40
            $etherum,
41
            $strength,
42
            $talentCollection,
43
            $inventory,
44
            $weapon,
45
            $corrupted,
46
        );
47 1
    }
48
49
    public static function new(string $name): self
50
    {
51
        return new self(
52
            $name,
53
            new Health(100),
54
            new Etherum(1),
55
            new Strength(5),
56
            new TalentCollection([]),
57
            new Inventory([]),
58
            null,
59
            false,
60
            new LevelProgress(
61
                new Level(1),
62
                new Experience(0),
63
            ),
64
            new AttributePoints(0),
65
            new TalentPoints(0),
66
        );
67
    }
68
69 1
    public function getLevelProgress(): LevelProgress
70
    {
71 1
        return $this->levelProgress;
72
    }
73
74
    public function increaseExperience(Experience $experience): void
75
    {
76
        $this->levelProgress->increase($experience, $this);
77
    }
78
79
    /**
80
     * @throws PlayerException
81
     */
82
    public function increaseEtherum(Etherum $etherum): void
83
    {
84
        $this->etherum->increaseBy($etherum);
85
86
        if ($this->isCorrupted()) {
87
            return;
88
        }
89
90
        $playerAscension = $this->talentCollection->findSecretKnowledge()?->getAscension()
91
            ?? throw PlayerException::etherumOverdose();
92
93
        $corruptionBoundary = $this->calculateCorruptionBoundary();
94
95
        if ($this->etherum->isGreaterThanOrEqual($corruptionBoundary)) {
96
            if ($playerAscension->isLowerThan(Ascension::sixthAscension())) {
97
                throw PlayerException::etherumOverdose();
98
            }
99
100
            $this->corrupted = true;
101
        }
102
    }
103
104 1
    public function getAttributePoints(): AttributePoints
105
    {
106 1
        return $this->attributePoints;
107
    }
108
109 1
    public function getTalentPoints(): TalentPoints
110
    {
111 1
        return $this->talentPoints;
112
    }
113
114
    private function calculateCorruptionBoundary(): Etherum
115
    {
116
        $ascension = $this->talentCollection->findSecretKnowledge()?->getAscension();
117
        if ($ascension === null) {
118
            return new Etherum(2);
119
        }
120
121
        try {
122
            $nextAscensionEtherum = $ascension->increment()->getRequiredEtherum();
123
        } catch (IntegerValueException $exception) {
124
            // entity has 8th ascension
125
            $nextAscensionEtherum = new Etherum($ascension->getRequiredEtherum()->get() * 2);
126
        }
127
128
        // etherum required by next ascension + 0.5 x etherum required by next ascension
129
        return $nextAscensionEtherum->increaseBy(
130
            new Etherum((int) ($nextAscensionEtherum->get() * 0.5)),
131
        );
132
    }
133
}
134