Passed
Push — master ( c87c14...8387eb )
by Janko
09:00
created

AbstractBattleParty::isActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib\Battle\Party;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use RuntimeException;
9
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
10
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
11
use Stu\Orm\Entity\User;
12
13
abstract class AbstractBattleParty implements BattlePartyInterface
14
{
15
    private bool $isStation;
16
    private User $user;
17
18
    /** @var Collection<int, covariant SpacecraftWrapperInterface> $members */
19
    private ?Collection $members = null;
20
21 21
    public function __construct(
22
        protected SpacecraftWrapperInterface $leader,
23
        private bool $isAttackingShieldsOnly = false
24
    ) {
25 21
        $this->isStation = $leader->get()->isStation();
26 21
        $this->user = $leader->get()->getUser();
27
    }
28
29
    /** 
30
     * @return Collection<int, covariant SpacecraftWrapperInterface> 
31
     */
32
    abstract protected function initMembers(): Collection;
33
34
    #[Override]
35
    public function getUser(): User
36
    {
37
        return $this->user;
38
    }
39
40
    #[Override]
41
    public function getLeader(): SpacecraftWrapperInterface
42
    {
43
        return $this->leader;
44
    }
45
46 19
    #[Override]
47
    public function getActiveMembers(bool $canFire = false, bool $filterDisabled = true): Collection
48
    {
49 19
        if ($this->members === null) {
50 19
            $this->members = $this->initMembers();
51
        }
52
53 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

53
        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...
54 19
            fn(SpacecraftWrapperInterface $wrapper): bool => !$wrapper->get()->getCondition()->isDestroyed()
55 17
                && (!$filterDisabled || !$wrapper->get()->getCondition()->isDisabled())
56 17
                && (!$canFire || $wrapper->canFire())
57 19
        );
58
    }
59
60
    #[Override]
61
    public function getRandomActiveMember(): SpacecraftWrapperInterface
62
    {
63
        $activeMembers = $this->getActiveMembers();
64
        $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

64
        $randomActiveMember = $activeMembers->get(/** @scrutinizer ignore-type */ array_rand($activeMembers->toArray()));
Loading history...
65
        if ($randomActiveMember === null) {
66
            throw new RuntimeException('isDefeated should be called first');
67
        }
68
69
        return $randomActiveMember;
70
    }
71
72
    #[Override]
73
    public function isDefeated(): bool
74
    {
75
        return $this->getActiveMembers()->isEmpty();
76
    }
77
78
79 2
    #[Override]
80
    public function isStation(): bool
81
    {
82 2
        return $this->isStation;
83
    }
84
85 2
    #[Override]
86
    public function isAttackingShieldsOnly(): bool
87
    {
88 2
        return $this->isAttackingShieldsOnly;
89
    }
90
91
    #[Override]
92
    public function isActive(): bool
93
    {
94
        return $this->getActiveMembers()
95
            ->exists(fn(int $key, SpacecraftWrapperInterface $wrapper): bool => $wrapper->get()->hasEnoughCrew());
96
    }
97
98
    /**
99
     * @return Collection<int, SpacecraftWrapperInterface>
100
     */
101 7
    protected function createSingleton(SpacecraftWrapperInterface $wrapper): Collection
102
    {
103 7
        return new ArrayCollection([$wrapper->get()->getId() => $wrapper]);
104
    }
105
106
    #[Override]
107
    public function count(): int
108
    {
109
        return $this->getActiveMembers()->count();
110
    }
111
112
    #[Override]
113
    public function getPrivateMessageType(): PrivateMessageFolderTypeEnum
114
    {
115
        return $this->isStation()
116
            ? PrivateMessageFolderTypeEnum::SPECIAL_STATION
117
            : PrivateMessageFolderTypeEnum::SPECIAL_SHIP;
118
    }
119
}
120