Module   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 359
Duplicated Lines 0 %

Test Coverage

Coverage 49.04%

Importance

Changes 0
Metric Value
eloc 126
c 0
b 0
f 0
dl 0
loc 359
ccs 51
cts 104
cp 0.4904
rs 9.0399
wmc 42

39 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getDowngradeFactor() 0 3 1
A getCost() 0 3 1
A getViewable() 0 3 1
A getSpecials() 0 3 1
A setFactionId() 0 5 1
A setType() 0 5 1
A setEcost() 0 5 1
A getCommodity() 0 3 1
A hasSpecial() 0 3 1
A setDefaultFactor() 0 5 1
A setViewable() 0 5 1
A setDowngradeFactor() 0 5 1
A getCrew() 0 3 1
A getCrewByFactionAndRumpLvl() 0 16 4
A getDefaultFactor() 0 3 1
A getCommodityId() 0 3 1
A getType() 0 3 1
A setCommodityId() 0 5 1
A getFaction() 0 3 1
A setUpgradeFactor() 0 5 1
A getName() 0 3 1
A getLevel() 0 3 1
A setResearchId() 0 5 1
A getFactionId() 0 3 1
A setCrew() 0 5 1
A getResearchId() 0 3 1
A getEcost() 0 3 1
A getWeaponShield() 0 3 1
A getWeapon() 0 3 1
A getId() 0 3 1
A getTorpedoHull() 0 3 1
A getDescription() 0 3 1
A getUpgradeFactor() 0 3 1
A setLevel() 0 5 1
A setName() 0 5 1
A getCostSorted() 0 10 1
A getSystemType() 0 3 1
A getShipRumpRoleId() 0 3 1

How to fix   Complexity   

Complex Class

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

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\Entity;
11
use Doctrine\ORM\Mapping\GeneratedValue;
12
use Doctrine\ORM\Mapping\Id;
13
use Doctrine\ORM\Mapping\Index;
14
use Doctrine\ORM\Mapping\JoinColumn;
15
use Doctrine\ORM\Mapping\ManyToOne;
16
use Doctrine\ORM\Mapping\OneToMany;
17
use Doctrine\ORM\Mapping\OneToOne;
18
use Doctrine\ORM\Mapping\OrderBy;
19
use Doctrine\ORM\Mapping\Table;
20
use Stu\Component\Faction\FactionEnum;
21
use Stu\Component\Spacecraft\ModuleSpecialAbilityEnum;
22
use Stu\Component\Spacecraft\SpacecraftModuleTypeEnum;
23
use Stu\Component\Spacecraft\SpacecraftRumpRoleEnum;
24
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
25
use Stu\Orm\Repository\ModuleRepository;
26
27
#[Table(name: 'stu_modules')]
28
#[Index(name: 'ship_rump_role_type_idx', columns: ['rumps_role_id', 'type'])]
29
#[Entity(repositoryClass: ModuleRepository::class)]
30
class Module
31
{
32
    #[Id]
33
    #[Column(type: 'integer')]
34
    #[GeneratedValue(strategy: 'IDENTITY')]
35
    private int $id;
36
37
    #[Column(type: 'string')]
38
    private string $name = '';
39
40
    #[Column(type: 'smallint')]
41
    private int $level = 0;
42
43
    #[Column(type: 'smallint')]
44
    private int $upgrade_factor = 0;
45
46
    #[Column(type: 'smallint')]
47
    private int $default_factor = 0;
48
49
    #[Column(type: 'smallint')]
50
    private int $downgrade_factor = 0;
51
52
    #[Column(type: 'smallint')]
53
    private int $crew = 0;
54
55
    #[Column(type: 'integer', enumType: SpacecraftModuleTypeEnum::class)]
56
    private SpacecraftModuleTypeEnum $type = SpacecraftModuleTypeEnum::HULL;
57
58
    #[Column(type: 'integer', nullable: true)]
59
    private ?int $research_id = 0;
60
61
    #[Column(type: 'integer')]
62
    private int $commodity_id = 0;
63
64
    #[Column(type: 'boolean')]
65
    private bool $viewable = false;
66
67
    #[Column(type: 'integer', enumType: SpacecraftRumpRoleEnum::class, nullable: true)]
68
    private ?SpacecraftRumpRoleEnum $rumps_role_id = null;
69
70
    #[Column(type: 'smallint')]
71
    private int $ecost = 0;
72
73
    #[Column(type: 'integer', nullable: true, enumType: FactionEnum::class)]
74
    private ?FactionEnum $faction_id = null;
75
76
    #[Column(type: 'integer', enumType: SpacecraftSystemTypeEnum::class, nullable: true)]
77
    private ?SpacecraftSystemTypeEnum $system_type = null;
78
79
    /**
80
     * @var Research
81
     */
82
    #[ManyToOne(targetEntity: Research::class)]
83
    #[JoinColumn(name: 'research_id', nullable: false, referencedColumnName: 'id')]
84
    private $research;
0 ignored issues
show
introduced by
The private property $research is not used, and could be removed.
Loading history...
85
86
    #[ManyToOne(targetEntity: Commodity::class)]
87
    #[JoinColumn(name: 'commodity_id', nullable: false, referencedColumnName: 'id', onDelete: 'CASCADE')]
88
    private Commodity $commodity;
89
90
    /**
91
     * @var Faction
92
     */
93
    #[ManyToOne(targetEntity: Faction::class)]
94
    #[JoinColumn(name: 'faction_id', nullable: false, referencedColumnName: 'id')]
95
    private $faction;
96
97
    #[ManyToOne(targetEntity: ShipRumpRole::class)]
98
    #[JoinColumn(name: 'rumps_role_id', nullable: false, referencedColumnName: 'id')]
99
    private ShipRumpRole $shipRumpRole;
0 ignored issues
show
introduced by
The private property $shipRumpRole is not used, and could be removed.
Loading history...
100
101
    /**
102
     * @var ArrayCollection<int, ModuleSpecial>
103
     */
104
    #[OneToMany(targetEntity: ModuleSpecial::class, mappedBy: 'module', indexBy: 'special_id', fetch: 'EXTRA_LAZY')]
105
    #[OrderBy(['special_id' => 'ASC'])]
106
    private Collection $moduleSpecials;
107
108
    /**
109
     * @var ArrayCollection<int, ModuleCost>
110
     */
111
    #[OneToMany(targetEntity: ModuleCost::class, mappedBy: 'module')]
112
    private Collection $buildingCosts;
113
114
    /**
115
     * @var ArrayCollection<int, TorpedoHull>
116
     */
117
    #[OneToMany(targetEntity: TorpedoHull::class, mappedBy: 'module', indexBy: 'torpedo_type')]
118
    #[OrderBy(['torpedo_type' => 'ASC'])]
119
    private Collection $torpedoHull;
120
121
    /**
122
     * @var ArrayCollection<int, WeaponShield>
123
     */
124
    #[OneToMany(targetEntity: WeaponShield::class, mappedBy: 'module', indexBy: 'weapon_id')]
125
    #[OrderBy(['weapon_id' => 'ASC'])]
126
    private Collection $weaponShield;
127
128
    #[OneToOne(targetEntity: Weapon::class, mappedBy: 'module')]
129
    private ?Weapon $weapon = null;
130
131
    public function __construct()
132
    {
133
        $this->moduleSpecials = new ArrayCollection();
134
        $this->buildingCosts = new ArrayCollection();
135
        $this->torpedoHull = new ArrayCollection();
136
        $this->weaponShield = new ArrayCollection();
137
    }
138
139 5
    public function getId(): int
140
    {
141 5
        return $this->id;
142
    }
143
144 6
    public function getName(): string
145
    {
146 6
        return $this->name;
147
    }
148
149
    public function setName(string $name): Module
150
    {
151
        $this->name = $name;
152
153
        return $this;
154
    }
155
156 4
    public function getLevel(): int
157
    {
158 4
        return $this->level;
159
    }
160
161
    public function setLevel(int $level): Module
162
    {
163
        $this->level = $level;
164
165
        return $this;
166
    }
167
168 3
    public function getUpgradeFactor(): int
169
    {
170 3
        return $this->upgrade_factor;
171
    }
172
173
    public function setUpgradeFactor(int $upgradeFactor): Module
174
    {
175
        $this->upgrade_factor = $upgradeFactor;
176
177
        return $this;
178
    }
179
180 3
    public function getDefaultFactor(): int
181
    {
182 3
        return $this->default_factor;
183
    }
184
185
    public function setDefaultFactor(int $defaultFactor): Module
186
    {
187
        $this->default_factor = $defaultFactor;
188
189
        return $this;
190
    }
191
192 3
    public function getDowngradeFactor(): int
193
    {
194 3
        return $this->downgrade_factor;
195
    }
196
197
    public function setDowngradeFactor(int $downgradeFactor): Module
198
    {
199
        $this->downgrade_factor = $downgradeFactor;
200
201
        return $this;
202
    }
203
204 3
    public function getCrew(): int
205
    {
206 3
        return $this->crew;
207
    }
208
209 3
    public function getCrewByFactionAndRumpLvl(Faction $faction, SpacecraftRump $rump): int
210
    {
211 3
        $result = $this->getCrew();
212
213
        if (
214 3
            $this->getFaction() !== null
215 3
            && $this->getFaction() !== $faction
216
        ) {
217 3
            $result += 1;
218
        }
219
220 3
        if ($this->getLevel() > $rump->getBaseValues()->getModuleLevel()) {
221 3
            $result += 1;
222
        }
223
224 3
        return $result;
225
    }
226
227
    public function setCrew(int $crew): Module
228
    {
229
        $this->crew = $crew;
230
231
        return $this;
232
    }
233
234 6
    public function getType(): SpacecraftModuleTypeEnum
235
    {
236 6
        return $this->type;
237
    }
238
239
    public function setType(SpacecraftModuleTypeEnum $type): Module
240
    {
241
        $this->type = $type;
242
243
        return $this;
244
    }
245
246
    public function getResearchId(): ?int
247
    {
248
        return $this->research_id;
249
    }
250
251
    public function setResearchId(int $researchId): Module
252
    {
253
        $this->research_id = $researchId;
254
255
        return $this;
256
    }
257
258 7
    public function getCommodityId(): int
259
    {
260 7
        return $this->commodity_id;
261
    }
262
263
    public function setCommodityId(int $commodityId): Module
264
    {
265
        $this->commodity_id = $commodityId;
266
267
        return $this;
268
    }
269
270
    public function getViewable(): bool
271
    {
272
        return $this->viewable;
273
    }
274
275
    public function setViewable(bool $viewable): Module
276
    {
277
        $this->viewable = $viewable;
278
279
        return $this;
280
    }
281
282
    public function getShipRumpRoleId(): ?SpacecraftRumpRoleEnum
283
    {
284
        return $this->rumps_role_id;
285
    }
286
287
    public function getWeapon(): ?Weapon
288
    {
289
        return $this->weapon;
290
    }
291
292 1
    public function getEcost(): int
293
    {
294 1
        return $this->ecost;
295
    }
296
297
    public function setEcost(int $energyCosts): Module
298
    {
299
        $this->ecost = $energyCosts;
300
301
        return $this;
302
    }
303
304
    public function getFactionId(): ?FactionEnum
305
    {
306
        return $this->faction_id;
307
    }
308
309
    public function setFactionId(FactionEnum $factionId): ?Module
310
    {
311
        $this->faction_id = $factionId;
312
313
        return $this;
314
    }
315
316 3
    public function getSystemType(): ?SpacecraftSystemTypeEnum
317
    {
318 3
        return $this->system_type;
319
    }
320
321
    /**
322
     * @return Collection<int, ModuleSpecial>
323
     */
324 3
    public function getSpecials(): Collection
325
    {
326 3
        return $this->moduleSpecials;
327
    }
328
329
    public function hasSpecial(ModuleSpecialAbilityEnum $ability): bool
330
    {
331
        return $this->moduleSpecials->containsKey($ability->value);
332
    }
333
334
    /**
335
     * @return Collection<int, ModuleCost>
336
     */
337 1
    public function getCost(): Collection
338
    {
339 1
        return $this->buildingCosts;
340
    }
341
342
    /**
343
     * @return array<int, ModuleCost>
344
     */
345 1
    public function getCostSorted(): array
346
    {
347 1
        $array = $this->getCost()->getValues();
348
349 1
        usort(
350 1
            $array,
351 1
            fn(ModuleCost $a, ModuleCost $b): int => $a->getCommodity()->getSort() <=> $b->getCommodity()->getSort()
352 1
        );
353
354 1
        return $array;
355
    }
356
357 1
    public function getCommodity(): Commodity
358
    {
359 1
        return $this->commodity;
360
    }
361
362 3
    public function getDescription(): string
363
    {
364 3
        return $this->getType()->getDescription();
365
    }
366
367
    /**
368
     * @return Collection<int, TorpedoHull>
369
     */
370 3
    public function getTorpedoHull(): Collection
371
    {
372 3
        return $this->torpedoHull;
373
    }
374
375
    /**
376
     * @return Collection<int, WeaponShield>
377
     */
378 3
    public function getWeaponShield(): Collection
379
    {
380 3
        return $this->weaponShield;
381
    }
382
383 3
    public function getFaction(): ?Faction
384
    {
385 3
        return $this->faction;
386
    }
387
}
388