Passed
Push — master ( 812b31...82efe3 )
by Paweł
02:52
created

TalentCollection::findSecretKnowledge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 3
    public function findWeaponMasteryForWeaponType(WeaponType $weaponType): ?WeaponMastery
17
    {
18 3
        return $this->filter(
19 3
            static fn(Talent $talent): bool =>
20 3
                $talent instanceof WeaponMastery && $talent->getType()->equals($weaponType),
21 3
        )->getIterator()->current();
22
    }
23
24 4
    public function findSecretKnowledge(): ?SecretKnowledge
25
    {
26 4
        return $this->filter(
27 4
            static fn(Talent $talent): bool => $talent instanceof SecretKnowledge,
28 4
        )->getIterator()->current();
29
    }
30
31 3
    public function filterAttacks(): self
32
    {
33 3
        return $this->filter(
34 3
            static fn(Talent $talent): bool => $talent instanceof Attack,
35
        );
36
    }
37
38 3
    public function randomAttack(): Attack
39
    {
40 3
        $attacks = $this->filterAttacks();
41 3
        if ($attacks->isEmpty()) {
42
            throw CollectionException::emptyCollection();
43
        }
44
45 3
        return $attacks->getItems()[array_rand($attacks->getItems())];
46
    }
47
48 8
    protected function getType(): string
49
    {
50 8
        return Talent::class;
51
    }
52
}
53