Passed
Pull Request — master (#1821)
by Nico
52:02 queued 25:23
created

AbstractBattleParty   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 45.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 81
ccs 15
cts 33
cp 0.4545
rs 10
c 1
b 0
f 0
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createSingleton() 0 3 1
A isBase() 0 3 1
A isDefeated() 0 3 1
A getActiveMembers() 0 10 6
A getPrivateMessageType() 0 5 2
A getRandomActiveMember() 0 9 2
A getUser() 0 3 1
A getLeader() 0 3 1
A count() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
namespace Stu\Module\Ship\Lib\Battle\Party;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use RuntimeException;
8
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
9
use Stu\Module\Ship\Lib\ShipWrapperInterface;
10
use Stu\Orm\Entity\UserInterface;
11
12
abstract class AbstractBattleParty implements BattlePartyInterface
13
{
14
    private bool $isBase;
15
    private UserInterface $user;
16
17
    /** @var Collection<int, ShipWrapperInterface> $members */
18
    private ?Collection $members = null;
19
20 21
    public function __construct(
21
        protected ShipWrapperInterface $leader
22
    ) {
23 21
        $this->isBase = $leader->get()->isBase();
24 21
        $this->user = $leader->get()->getUser();
25
    }
26
27
    /** @return Collection<int, ShipWrapperInterface> */
28
    protected abstract function initMembers(): Collection;
29
30
    public function getUser(): UserInterface
31
    {
32
        return $this->user;
33
    }
34
35
    public function getLeader(): ShipWrapperInterface
36
    {
37
        return $this->leader;
38
    }
39
40 19
    public function getActiveMembers(bool $canFire = false, bool $filterDisabled = true): Collection
41
    {
42 19
        if ($this->members === null) {
43 19
            $this->members = $this->initMembers();
44
        }
45
46 19
        return $this->members->filter(
0 ignored issues
show
Bug introduced by
The method filter() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        return $this->members->/** @scrutinizer ignore-call */ filter(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47 19
            fn (ShipWrapperInterface $wrapper) => !$wrapper->get()->isDestroyed()
48 17
                && (!$filterDisabled || !$wrapper->get()->isDisabled())
49 17
                && (!$canFire || $wrapper->canFire())
50 19
        );
51
    }
52
53
    public function getRandomActiveMember(): ShipWrapperInterface
54
    {
55
        $activeMembers = $this->getActiveMembers();
56
        $randomActiveMember = $activeMembers->get(array_rand($activeMembers->toArray()));
0 ignored issues
show
Bug introduced by
It seems like array_rand($activeMembers->toArray()) can also be of type array; however, parameter $key of Doctrine\Common\Collecti...adableCollection::get() does only seem to accept integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $randomActiveMember = $activeMembers->get(/** @scrutinizer ignore-type */ array_rand($activeMembers->toArray()));
Loading history...
57
        if ($randomActiveMember === null) {
58
            throw new RuntimeException('isDefeated should be called first');
59
        }
60
61
        return $randomActiveMember;
62
    }
63
64
    public function isDefeated(): bool
65
    {
66
        return $this->getActiveMembers()->isEmpty();
67
    }
68
69
70 2
    public function isBase(): bool
71
    {
72 2
        return $this->isBase;
73
    }
74
75
    /**
76
     * @return Collection<int, ShipWrapperInterface>
77
     */
78 7
    protected function createSingleton(ShipWrapperInterface $wrapper): Collection
79
    {
80 7
        return new ArrayCollection([$wrapper->get()->getId() => $wrapper]);
81
    }
82
83
    public function count(): int
84
    {
85
        return $this->getActiveMembers()->count();
86
    }
87
88
    public function getPrivateMessageType(): int
89
    {
90
        return $this->isBase()
91
            ? PrivateMessageFolderSpecialEnum::PM_SPECIAL_STATION
92
            : PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP;
93
    }
94
}
95