Passed
Push — master ( ef7bc0...8d062d )
by Nico
18:32 queued 09:24
created

Module   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 402
Duplicated Lines 0 %

Test Coverage

Coverage 47.32%

Importance

Changes 0
Metric Value
eloc 172
dl 0
loc 402
ccs 53
cts 112
cp 0.4732
rs 8.8798
c 0
b 0
f 0
wmc 44

40 Methods

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

371
        return in_array($special_id, /** @scrutinizer ignore-type */ $this->specialAbilities);
Loading history...
372
    }
373
374 3
    #[Override]
375
    public function getSpecials(): Collection
376
    {
377 3
        return $this->moduleSpecials;
378
    }
379
380 1
    #[Override]
381
    public function getCost(): Collection
382
    {
383 1
        return $this->buildingCosts;
384
    }
385
386 1
    #[Override]
387
    public function getCostSorted(): array
388
    {
389 1
        $array = $this->getCost()->getValues();
390
391 1
        usort(
392 1
            $array,
393 1
            fn(ModuleCostInterface $a, ModuleCostInterface $b): int => $a->getCommodity()->getSort() <=> $b->getCommodity()->getSort()
394 1
        );
395
396 1
        return $array;
397
    }
398
399 1
    #[Override]
400
    public function getCommodity(): CommodityInterface
401
    {
402 1
        return $this->commodity;
403
    }
404
405 3
    #[Override]
406
    public function getDescription(): string
407
    {
408 3
        return $this->getType()->getDescription();
409
    }
410
411 3
    #[Override]
412
    public function getTorpedoHull(): Collection
413
    {
414 3
        return $this->torpedoHull;
415
    }
416
417 3
    #[Override]
418
    public function getWeaponShield(): Collection
419
    {
420 3
        return $this->weaponShield;
421
    }
422
423 3
    #[Override]
424
    public function getFaction(): ?FactionInterface
425
    {
426 3
        return $this->faction;
427
    }
428
}
429