TransferInformation   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 147
ccs 60
cts 64
cp 0.9375
rs 9.1199
c 0
b 0
f 0
wmc 41

20 Methods

Rating   Name   Duplication   Size   Complexity  
B isTorpedoTransferPossible() 0 15 11
A __construct() 0 9 1
A isCommodityTransferPossible() 0 4 2
A isCrewTransferPossible() 0 17 5
A getTargetWrapper() 0 3 1
A isOtherGoodTransferPossible() 0 5 3
A getSourceType() 0 3 1
A getTargetId() 0 3 1
A getBeamFactor() 0 3 1
A isFriend() 0 3 2
A getTargetType() 0 3 1
A isUpIcon() 0 3 1
A getSourceId() 0 3 1
A getTarget() 0 3 1
A isSourceAbove() 0 3 1
A getSource() 0 3 1
A getSourceWrapper() 0 3 1
A getTransferType() 0 3 1
A showFleetleaderActions() 0 12 4
A isUnload() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like TransferInformation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TransferInformation, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Lib\Transfer;
6
7
use Stu\Lib\Transfer\Wrapper\StorageEntityWrapperInterface;
8
use Stu\Orm\Entity\Colony;
9
use Stu\Orm\Entity\Ship;
10
use Stu\Orm\Entity\Spacecraft;
11
12
class TransferInformation
13
{
14
    private EntityWithStorageInterface $source;
15
    private EntityWithStorageInterface $target;
16
17 13
    public function __construct(
18
        private TransferTypeEnum $currentType,
19
        private StorageEntityWrapperInterface $sourceWrapper,
20
        private StorageEntityWrapperInterface $targetWrapper,
21
        private bool $isUnload,
22
        private bool $isFriend
23
    ) {
24 13
        $this->source = $sourceWrapper->get();
25 13
        $this->target = $targetWrapper->get();
26
    }
27
28 13
    public function isCommodityTransferPossible(bool $isOtherTypeRequired = true): bool
29
    {
30 13
        return !($isOtherTypeRequired
31 13
            && $this->currentType === TransferTypeEnum::COMMODITIES);
32
    }
33
34 13
    public function isCrewTransferPossible(bool $isOtherTypeRequired = true): bool
35
    {
36
        if (
37 13
            $isOtherTypeRequired
38 13
            && $this->currentType === TransferTypeEnum::CREW
39
        ) {
40 4
            return false;
41
        }
42
43
        if (
44 9
            $this->target instanceof Spacecraft
45 9
            && $this->target->hasUplink()
0 ignored issues
show
Bug introduced by
The method hasUplink() does not exist on Stu\Lib\Transfer\EntityWithStorageInterface. ( Ignorable by Annotation )

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

45
            && $this->target->/** @scrutinizer ignore-call */ hasUplink()

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...
46
        ) {
47
            return true;
48
        }
49
50 9
        return $this->target->getUser() === $this->source->getUser();
51
    }
52
53 13
    public function isTorpedoTransferPossible(bool $isOtherTypeRequired = true): bool
54
    {
55
        if (
56 13
            $isOtherTypeRequired
57 13
            && $this->currentType === TransferTypeEnum::TORPEDOS
58
        ) {
59
            return false;
60
        }
61
62 13
        return $this->source instanceof Spacecraft
63 13
            && $this->target instanceof Spacecraft
64 13
            && $this->source->isTorpedoStorageHealthy()
0 ignored issues
show
Bug introduced by
The method isTorpedoStorageHealthy() does not exist on Stu\Lib\Transfer\EntityWithStorageInterface. ( Ignorable by Annotation )

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

64
            && $this->source->/** @scrutinizer ignore-call */ isTorpedoStorageHealthy()

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...
65 13
            && (!$this->isUnload() || $this->source->getTorpedoCount() > 0)
0 ignored issues
show
Bug introduced by
The method getTorpedoCount() does not exist on Stu\Lib\Transfer\EntityWithStorageInterface. ( Ignorable by Annotation )

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

65
            && (!$this->isUnload() || $this->source->/** @scrutinizer ignore-call */ getTorpedoCount() > 0)

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...
66 13
            && ($this->target->hasTorpedo() || $this->target->isTorpedoStorageHealthy())
0 ignored issues
show
Bug introduced by
The method hasTorpedo() does not exist on Stu\Lib\Transfer\EntityWithStorageInterface. ( Ignorable by Annotation )

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

66
            && ($this->target->/** @scrutinizer ignore-call */ hasTorpedo() || $this->target->isTorpedoStorageHealthy())

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...
67 13
            && ($this->isUnload() || $this->target->getTorpedoCount() > 0);
68
    }
69
70
    public function isFriend(): bool
71
    {
72
        return $this->isFriend || $this->source->getUser() === $this->target->getUser();
73
    }
74
75 13
    public function isOtherGoodTransferPossible(): bool
76
    {
77 13
        return $this->isCommodityTransferPossible()
78 13
            || $this->isCrewTransferPossible()
79 13
            || $this->isTorpedoTransferPossible();
80
    }
81
82 13
    public function getTransferType(): TransferTypeEnum
83
    {
84 13
        return $this->currentType;
85
    }
86
87 13
    public function getSourceType(): TransferEntityTypeEnum
88
    {
89 13
        return $this->source->getTransferEntityType();
90
    }
91
92 13
    public function getTargetType(): TransferEntityTypeEnum
93
    {
94 13
        return $this->target->getTransferEntityType();
95
    }
96
97 13
    public function isUnload(): bool
98
    {
99 13
        return $this->isUnload;
100
    }
101
102 13
    public function getSourceId(): int
103
    {
104 13
        return $this->source->getId();
105
    }
106
107 13
    public function getTargetId(): int
108
    {
109 13
        return $this->target->getId();
110
    }
111
112 13
    public function getSource(): EntityWithStorageInterface
113
    {
114 13
        return $this->source;
115
    }
116
117 13
    public function getTarget(): EntityWithStorageInterface
118
    {
119 13
        return $this->target;
120
    }
121
122 13
    public function getSourceWrapper(): StorageEntityWrapperInterface
123
    {
124 13
        return $this->sourceWrapper;
125
    }
126
127 13
    public function getTargetWrapper(): StorageEntityWrapperInterface
128
    {
129 13
        return $this->targetWrapper;
130
    }
131
132 13
    public function getBeamFactor(): int
133
    {
134 13
        return $this->sourceWrapper->getBeamFactor();
135
    }
136
137 13
    public function isSourceAbove(): bool
138
    {
139 13
        return $this->getSourceType() !== TransferEntityTypeEnum::COLONY;
140
    }
141
142 13
    public function isUpIcon(): bool
143
    {
144 13
        return ($this->getSource() instanceof Colony) === $this->isUnload();
145
    }
146
147 9
    public function showFleetleaderActions(): bool
148
    {
149 9
        $source = $this->getSource();
150 9
        if (!$source instanceof Ship) {
151 5
            return false;
152
        }
153
154 4
        $fleet = $source->getFleet();
155
156 4
        return $fleet !== null
157 4
            && $source->isFleetLeader()
158 4
            && $fleet->getShipCount() > 1;
159
    }
160
}
161