Passed
Push — master ( f05eff...e96706 )
by Paweł
02:59
created

Player::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 1
eloc 9
nc 1
nop 11
dl 0
loc 22
ccs 0
cts 3
cp 0
crap 2
rs 9.9666
c 4
b 0
f 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\Strength;
11
use AardsGerds\Game\Build\Experience;
12
use AardsGerds\Game\Build\LevelProgress;
13
use AardsGerds\Game\Build\Talent\SecretKnowledge\Ascension;
14
use AardsGerds\Game\Build\Talent\TalentCollection;
15
use AardsGerds\Game\Build\Talent\TalentPoints;
16
use AardsGerds\Game\Entity\Entity;
17
use AardsGerds\Game\Inventory\Inventory;
18
use AardsGerds\Game\Inventory\Weapon\Weapon;
19
20
final class Player extends Entity
21
{
22
    public function __construct(
23
        string $name,
24
        Health $health,
25
        Etherum $etherum,
26
        Strength $strength,
27
        TalentCollection $talentCollection,
28
        Inventory $inventory,
29
        ?Weapon $weapon,
30
        bool $corrupted,
31
        private LevelProgress $levelProgress,
32
        private AttributePoints $attributePoints,
33
        private TalentPoints $talentPoints,
34
    ) {
35
        parent::__construct(
36
            $name,
37
            $health,
38
            $etherum,
39
            $strength,
40
            $talentCollection,
41
            $inventory,
42
            $weapon,
43
            $corrupted,
44
        );
45
    }
46
47
    public function getLevelProgress(): LevelProgress
48
    {
49
        return $this->levelProgress;
50
    }
51
52
    public function increaseExperience(Experience $experience): void
53
    {
54
        $this->levelProgress->increase($experience, $this);
55
    }
56
57
    /**
58
     * @throws PlayerException
59
     */
60
    public function increaseEtherum(Etherum $etherum): void
61
    {
62
        $this->etherum->increaseBy($etherum);
63
64
        if ($this->isCorrupted()) {
65
            return;
66
        }
67
68
        $playerAscension = $this->talentCollection->findSecretKnowledge()?->getAscension()
69
            ?? throw PlayerException::etherumOverdose();
70
71
        $corruptionBoundary = $this->calculateCorruptionBoundary();
72
73
        if (!$this->isCorrupted() && $this->etherum->isGreaterThanOrEqual($corruptionBoundary)) {
74
            if ($playerAscension->isLowerThan(Ascension::sixthAscension())) {
75
                throw PlayerException::etherumOverdose();
76
            }
77
78
            $this->corrupted = true;
79
        }
80
    }
81
82
    public function getAttributePoints(): AttributePoints
83
    {
84
        return $this->attributePoints;
85
    }
86
87
    public function getTalentPoints(): TalentPoints
88
    {
89
        return $this->talentPoints;
90
    }
91
}
92