Passed
Push — dev ( 45bf8d...5b8f46 )
by Janko
10:07
created

Module   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 393
Duplicated Lines 0 %

Test Coverage

Coverage 49.53%

Importance

Changes 0
Metric Value
eloc 167
dl 0
loc 393
ccs 53
cts 107
cp 0.4953
rs 8.96
c 0
b 0
f 0
wmc 43

40 Methods

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