Passed
Push — master ( f2c7f4...294cf3 )
by Paweł
10:56
created

TalentCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 10.53%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 15
c 2
b 0
f 0
dl 0
loc 37
ccs 2
cts 19
cp 0.1053
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A filterAttacks() 0 4 1
A getType() 0 3 1
A findSecretKnowledge() 0 5 1
A randomAttack() 0 8 2
A findWeaponMasteryForWeaponType() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Build\Talent;
6
7
use AardsGerds\Game\Build\Talent\SecretKnowledge\SecretKnowledge;
8
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMastery;
9
use AardsGerds\Game\Fight\Attack;
10
use AardsGerds\Game\Inventory\Weapon\WeaponType;
11
use AardsGerds\Game\Shared\Collection;
12
use AardsGerds\Game\Shared\CollectionException;
13
14
final class TalentCollection extends Collection
15
{
16
    public function findWeaponMasteryForWeaponType(WeaponType $weaponType): ?WeaponMastery
17
    {
18
        return $this->filter(
19
            static fn(Talent $talent): bool =>
20
                $talent instanceof WeaponMastery && $talent->getType()->equals($weaponType),
21
        )->getIterator()->current();
22
    }
23
24
    public function findSecretKnowledge(): ?SecretKnowledge
25
    {
26
        return $this->filter(
27
            static fn(Talent $talent): bool => $talent instanceof SecretKnowledge,
28
        )->getIterator()->current();
29
    }
30
31
    public function filterAttacks(): self
32
    {
33
        return $this->filter(
34
            static fn(Talent $talent): bool => $talent instanceof Attack,
35
        );
36
    }
37
38
    public function randomAttack(): Attack
39
    {
40
        $attacks = $this->filterAttacks();
41
        if ($attacks->isEmpty()) {
42
            throw CollectionException::emptyCollection();
43
        }
44
45
        return $attacks->getItems()[array_rand($attacks->getItems())];
46
    }
47
48 1
    protected function getType(): string
49
    {
50 1
        return Talent::class;
51
    }
52
}
53