Passed
Pull Request — master (#2161)
by Janko
25:16 queued 15:11
created

Module   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 385
Duplicated Lines 0 %

Test Coverage

Coverage 50.96%

Importance

Changes 0
Metric Value
eloc 164
c 0
b 0
f 0
dl 0
loc 385
ccs 53
cts 104
cp 0.5096
rs 9.0399
wmc 42

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