SpacecraftNfsItem   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Test Coverage

Coverage 79.22%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 159
ccs 61
cts 77
cp 0.7922
rs 9.1199
c 0
b 0
f 0
wmc 41

33 Methods

Rating   Name   Duplication   Size   Complexity  
A isInterceptable() 0 4 1
A getShield() 0 3 1
A isStation() 0 3 1
A isTrumfield() 0 3 1
A getWarpDriveState() 0 3 1
A isWarped() 0 4 2
A getHull() 0 3 1
A isScanPossible() 0 4 2
A getType() 0 3 1
A isTractorbeamPossible() 0 3 1
A getName() 0 3 1
A isShuttle() 0 3 1
A getId() 0 3 1
A __construct() 0 1 1
A getFleetId() 0 3 1
A getMaxHull() 0 3 1
A isBoardingPossible() 0 3 1
A isCloaked() 0 3 1
A isShielded() 0 3 1
A getTransferEntityType() 0 5 2
A getUserId() 0 3 1
A getRumpName() 0 3 1
A isTransferPossible() 0 3 1
A getRump() 0 3 1
A hasCrew() 0 3 1
A getUserName() 0 3 1
A hasUplink() 0 3 1
A getHoldingWebBackgroundStyle() 0 17 5
A canBeTracked() 0 3 1
A isContactable() 0 3 1
A getRumpId() 0 3 1
A isSelectable() 0 4 2
A hasLogBook() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like SpacecraftNfsItem 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 SpacecraftNfsItem, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib;
6
7
use Stu\Component\Game\TimeConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\TimeConstants 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 Stu\Component\Spacecraft\SpacecraftRumpCategoryEnum;
9
use Stu\Component\Spacecraft\SpacecraftTypeEnum;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemModeEnum;
11
use Stu\Component\Spacecraft\System\Type\TractorBeamShipSystem;
12
use Stu\Lib\Transfer\TransferEntityTypeEnum;
13
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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...
14
use Stu\Module\Spacecraft\Lib\Battle\FightLib;
15
use Stu\Module\Spacecraft\Lib\TSpacecraftItemInterface;
16
17
class SpacecraftNfsItem
18
{
19 1
    public function __construct(private TSpacecraftItemInterface $item, private int $userId) {}
20
21 1
    public function getId(): int
22
    {
23 1
        return $this->item->getShipId();
24
    }
25
    public function getFleetId(): ?int
26
    {
27
        return $this->item->getFleetId();
28
    }
29 1
    public function getName(): string
30
    {
31 1
        return $this->item->getShipName();
32
    }
33 1
    public function getHull(): int
34
    {
35 1
        return $this->item->getHull();
36
    }
37 1
    public function getMaxHull(): int
38
    {
39 1
        return $this->item->getMaxHull();
40
    }
41
    public function getShield(): int
42
    {
43
        return $this->item->getShield();
44
    }
45 1
    public function isShielded(): bool
46
    {
47 1
        return $this->item->isShielded() > SpacecraftSystemModeEnum::MODE_OFF->value;
48
    }
49 1
    public function isCloaked(): bool
50
    {
51 1
        return $this->item->isCloaked() > SpacecraftSystemModeEnum::MODE_OFF->value;
52
    }
53 1
    public function getWarpDriveState(): bool
54
    {
55 1
        return $this->item->getWarpDriveState() > SpacecraftSystemModeEnum::MODE_OFF->value;
56
    }
57 1
    public function isWarped(): bool
58
    {
59 1
        return $this->getWarpDriveState()
60 1
            || $this->item->getTractorWarpState() > SpacecraftSystemModeEnum::MODE_OFF->value;
61
    }
62 1
    public function isScanPossible(): bool
63
    {
64 1
        return !$this->isCloaked()
65 1
            && $this->getType() !== SpacecraftTypeEnum::THOLIAN_WEB;
66
    }
67 1
    public function isTractorbeamPossible(): bool
68
    {
69 1
        return TractorBeamShipSystem::isTractorBeamPossible($this);
70
    }
71 1
    public function isBoardingPossible(): bool
72
    {
73 1
        return FightLib::isBoardingPossible($this);
74
    }
75 1
    public function isInterceptable(): bool
76
    {
77
        //TODO can tractored ships be intercepted?!
78 1
        return $this->getWarpDriveState();
79
    }
80 1
    public function getType(): SpacecraftTypeEnum
81
    {
82 1
        return $this->item->getType();
83
    }
84 1
    public function isStation(): bool
85
    {
86 1
        return $this->item->getType() === SpacecraftTypeEnum::STATION;
87
    }
88 1
    public function isTrumfield(): bool
89
    {
90 1
        return false;
91
    }
92 1
    public function isShuttle(): bool
93
    {
94 1
        return $this->item->getRumpCategoryId() === SpacecraftRumpCategoryEnum::SHUTTLE->value;
95
    }
96
    public function canBeTracked(): bool
97
    {
98
        return $this->item->getType() === SpacecraftTypeEnum::SHIP;
99
    }
100 1
    public function getRumpId(): int
101
    {
102 1
        return $this->item->getRumpId();
103
    }
104 1
    public function getHoldingWebBackgroundStyle(): string
105
    {
106 1
        if ($this->item->getWebId() === null) {
107 1
            return '';
108
        }
109
110
        $finishTime = $this->item->getWebFinishTime();
111
112
        if ($finishTime === null || $finishTime < time()) {
113
            $icon =  'web.png';
114
        } else {
115
            $closeTofinish = $finishTime - time() < TimeConstants::ONE_HOUR_IN_SECONDS;
116
117
            $icon = $closeTofinish ? 'web_u.png' : 'web_u2.png';
118
        }
119
120
        return sprintf('background-image: url(assets/buttons/%s); vertical-align: middle; text-align: center;', $icon);
121
    }
122 1
    public function getRumpName(): string
123
    {
124 1
        return $this->item->getRumpName();
125
    }
126 1
    public function getUserId(): int
127
    {
128 1
        return $this->item->getUserId();
129
    }
130
131 1
    public function isContactable(): bool
132
    {
133 1
        return $this->getUserId() != UserConstants::USER_NOONE;
134
    }
135
136 1
    public function getUserName(): string
137
    {
138 1
        return $this->item->getUserName();
139
    }
140 1
    public function isSelectable(): bool
141
    {
142 1
        return $this->userId === $this->getUserId()
143 1
            && $this->getType()->getModuleView() !== null;
144
    }
145
146
    public function hasUplink(): bool
147
    {
148
        return $this->item->getUplinkState() > SpacecraftSystemModeEnum::MODE_OFF->value;
149
    }
150
151
    public function getRump(): mixed
152
    {
153
        return $this;
154
    }
155
156 1
    public function hasLogBook(): bool
157
    {
158 1
        return $this->item->hasLogBook();
159
    }
160
161 1
    public function hasCrew(): bool
162
    {
163 1
        return $this->item->hasCrew();
164
    }
165
166 1
    public function isTransferPossible(): bool
167
    {
168 1
        return $this->getType()->isTransferPossible();
169
    }
170
171 1
    public function getTransferEntityType(): TransferEntityTypeEnum
172
    {
173 1
        return $this->isStation()
174 1
            ? TransferEntityTypeEnum::STATION
175 1
            : TransferEntityTypeEnum::SHIP;
176
    }
177
}
178