Colony   C
last analyzed

Complexity

Total Complexity 54

Size/Duplication

Total Lines 404
Duplicated Lines 0 %

Test Coverage

Coverage 54.81%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 136
dl 0
loc 404
ccs 74
cts 135
cp 0.5481
rs 6.4799
c 1
b 0
f 0
wmc 54

54 Methods

Rating   Name   Duplication   Size   Complexity  
A getSurfaceWidth() 0 3 1
A setColonyClass() 0 4 1
A getTransferEntityType() 0 3 1
A getCrewAssignmentAmount() 0 3 1
A getSx() 0 3 1
A getBeamFactor() 0 3 1
A setPlanetName() 0 4 1
A getHostType() 0 3 1
A getId() 0 3 1
A isFree() 0 3 1
A getChangeable() 0 3 1
A getMask() 0 3 1
A getSy() 0 3 1
A getPlanetFields() 0 3 1
A getWorkless() 0 3 1
A isColony() 0 3 1
A setUser() 0 4 1
A getComponentParameters() 0 6 1
A getColonyClass() 0 3 1
A getSectorString() 0 3 1
A getDatabaseId() 0 3 1
A getBeamableStorage() 0 3 1
A getBlockers() 0 3 1
A getPlanetName() 0 3 1
A isMenuAllowed() 0 3 1
A getLocation() 0 3 1
A setName() 0 4 1
A setDatabaseEntry() 0 4 1
A getStorage() 0 3 1
A __construct() 0 8 1
A getMaxEps() 0 3 1
A getCrewTrainingAmount() 0 3 1
A getSystem() 0 3 1
A setChangeable() 0 5 1
A getCrewAssignments() 0 3 1
A getUserId() 0 3 1
A getStarsystemMap() 0 3 1
A setMask() 0 4 1
A isBlocked() 0 3 1
A getStorageSum() 0 6 1
A getMaxBev() 0 3 1
A getWorkers() 0 3 1
A getDefenders() 0 3 1
A getName() 0 3 1
A getMaxStorage() 0 3 1
A getHref() 0 7 1
A isDefended() 0 3 1
A setRotationFactor() 0 5 1
A getUser() 0 3 1
A getRotationFactor() 0 3 1
A setStarsystemMap() 0 5 1
A getPopulation() 0 3 1
A getDefaultViewIdentifier() 0 3 1
A setSurfaceWidth() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Colony 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 Colony, 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\JoinColumn;
14
use Doctrine\ORM\Mapping\ManyToOne;
15
use Doctrine\ORM\Mapping\OneToMany;
16
use Doctrine\ORM\Mapping\OneToOne;
17
use Doctrine\ORM\Mapping\OrderBy;
18
use Doctrine\ORM\Mapping\Table;
19
use LogicException;
20
use Stu\Component\Colony\ColonyMenuEnum;
21
use Stu\Component\Colony\Trait\ColonyRotationTrait;
22
use Stu\Component\Game\ModuleEnum;
23
use Stu\Lib\Colony\PlanetFieldHostInterface;
24
use Stu\Lib\Colony\PlanetFieldHostTypeEnum;
25
use Stu\Lib\Interaction\EntityWithInteractionCheckInterface;
26
use Stu\Lib\Map\EntityWithLocationInterface;
27
use Stu\Lib\Transfer\CommodityTransfer;
28
use Stu\Lib\Transfer\EntityWithStorageInterface;
29
use Stu\Lib\Transfer\TransferEntityTypeEnum;
30
use Stu\Module\Colony\View\ShowColony\ShowColony;
31
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...
32
use Stu\Module\Spacecraft\Lib\Crew\EntityWithCrewAssignmentsInterface;
33
use Stu\Orm\Repository\ColonyRepository;
34
35
#[Table(name: 'stu_colony')]
36
#[Entity(repositoryClass: ColonyRepository::class)]
37
class Colony implements
38
    PlanetFieldHostInterface,
39
    EntityWithStorageInterface,
40
    EntityWithLocationInterface,
41
    EntityWithCrewAssignmentsInterface,
42
    EntityWithInteractionCheckInterface
43
{
44
    use ColonyRotationTrait;
45
46
    #[Id]
47
    #[Column(type: 'integer')]
48
    #[GeneratedValue(strategy: 'IDENTITY')]
49
    private int $id;
50
51
    #[OneToOne(targetEntity: ColonyChangeable::class, mappedBy: 'colony', fetch: 'EAGER', cascade: ['all'])]
52
    private ?ColonyChangeable $changeable;
53
54
    #[Column(type: 'integer')]
55
    private int $colonies_classes_id = 0;
0 ignored issues
show
introduced by
The private property $colonies_classes_id is not used, and could be removed.
Loading history...
56
57
    #[Column(type: 'integer')]
58
    private int $user_id = 0;
59
60
    #[Column(type: 'string')]
61
    private string $name = '';
62
63
    #[Column(type: 'string', length: 100)]
64
    private string $planet_name = '';
65
66
    #[Column(type: 'text', nullable: true)]
67
    private ?string $mask = null;
68
69
    #[Column(type: 'integer', nullable: true)]
70
    private ?int $database_id = null;
71
72
    #[Column(type: 'integer', length: 3)]
73
    private int $rotation_factor = 1;
74
75
    #[Column(type: 'integer', length: 2)]
76
    private int $surface_width = 0;
77
78
    #[ManyToOne(targetEntity: ColonyClass::class)]
79
    #[JoinColumn(name: 'colonies_classes_id', nullable: false, referencedColumnName: 'id')]
80
    private ColonyClass $colonyClass;
81
82
    #[OneToOne(targetEntity: StarSystemMap::class, inversedBy: 'colony')]
83
    #[JoinColumn(name: 'starsystem_map_id', nullable: false, referencedColumnName: 'id', onDelete: 'CASCADE')]
84
    private StarSystemMap $starsystem_map;
85
86
    #[ManyToOne(targetEntity: User::class)]
87
    #[JoinColumn(name: 'user_id', nullable: false, referencedColumnName: 'id')]
88
    private User $user;
89
90
    /**
91
     * @var ArrayCollection<int, PlanetField>
92
     */
93
    #[OneToMany(targetEntity: PlanetField::class, mappedBy: 'colony', indexBy: 'field_id', fetch: 'EXTRA_LAZY')]
94
    #[OrderBy(['field_id' => 'ASC'])]
95
    private Collection $planetFields;
96
97
    /**
98
     * @var ArrayCollection<int, Storage>
99
     */
100
    #[OneToMany(targetEntity: Storage::class, mappedBy: 'colony', indexBy: 'commodity_id')]
101
    #[OrderBy(['commodity_id' => 'ASC'])]
102
    private Collection $storage;
103
104
    #[OneToOne(targetEntity: DatabaseEntry::class)]
105
    #[JoinColumn(name: 'database_id', referencedColumnName: 'id')]
106
    private ?DatabaseEntry $databaseEntry;
107
108
    /**
109
     * @var ArrayCollection<int, Fleet>
110
     */
111
    #[OneToMany(targetEntity: Fleet::class, mappedBy: 'defendedColony')]
112
    private Collection $defenders;
113
114
    /**
115
     * @var ArrayCollection<int, Fleet>
116
     */
117
    #[OneToMany(targetEntity: Fleet::class, mappedBy: 'blockedColony')]
118
    private Collection $blockers;
119
120
    /**
121
     * @var ArrayCollection<int, CrewAssignment>
122
     */
123
    #[OneToMany(targetEntity: CrewAssignment::class, mappedBy: 'colony')]
124
    private Collection $crewAssignments;
125
126
    /**
127
     * @var ArrayCollection<int, CrewTraining>
128
     */
129
    #[OneToMany(targetEntity: CrewTraining::class, mappedBy: 'colony')]
130
    private Collection $crewTrainings;
131
132
    /** @var array<int, int> */
133
    private array $twilightZones = [];
0 ignored issues
show
introduced by
The private property $twilightZones is not used, and could be removed.
Loading history...
134
135
    public function __construct()
136
    {
137
        $this->planetFields = new ArrayCollection();
138
        $this->storage = new ArrayCollection();
139
        $this->defenders = new ArrayCollection();
140
        $this->blockers = new ArrayCollection();
141
        $this->crewAssignments = new ArrayCollection();
142
        $this->crewTrainings = new ArrayCollection();
143
    }
144
145 43
    public function getId(): int
146
    {
147 43
        return $this->id;
148
    }
149
150 21
    public function getChangeable(): ColonyChangeable
151
    {
152 21
        return $this->changeable ?? throw new LogicException('Colony has no changeable');
153
    }
154
155
    public function setChangeable(ColonyChangeable $changeable): Colony
156
    {
157
        $this->changeable = $changeable;
158
159
        return $this;
160
    }
161
162 39
    public function getUserId(): int
163
    {
164 39
        return $this->user_id;
165
    }
166
167 8
    public function getSx(): int
168
    {
169 8
        return $this->getStarsystemMap()->getSx();
170
    }
171
172 8
    public function getSy(): int
173
    {
174 8
        return $this->getStarsystemMap()->getSy();
175
    }
176
177 17
    public function getName(): string
178
    {
179 17
        return $this->name;
180
    }
181
182
    public function setName(string $name): Colony
183
    {
184
        $this->name = $name;
185
        return $this;
186
    }
187
188 4
    public function getPlanetName(): string
189
    {
190 4
        return $this->planet_name;
191
    }
192
193
    public function setPlanetName(string $planet_name): Colony
194
    {
195
        $this->planet_name = $planet_name;
196
        return $this;
197
    }
198
199
    public function getMask(): ?string
200
    {
201
        return $this->mask;
202
    }
203
204
    public function setMask(?string $mask): Colony
205
    {
206
        $this->mask = $mask;
207
        return $this;
208
    }
209
210
    public function getDatabaseId(): ?int
211
    {
212
        return $this->database_id;
213
    }
214
215
    public function setDatabaseEntry(?DatabaseEntry $entry): Colony
216
    {
217
        $this->databaseEntry = $entry;
218
        return $this;
219
    }
220
221 4
    public function getRotationFactor(): int
222
    {
223 4
        return $this->rotation_factor;
224
    }
225
226
    public function setRotationFactor(int $rotationFactor): Colony
227
    {
228
        $this->rotation_factor = $rotationFactor;
229
230
        return $this;
231
    }
232
233 6
    public function getSurfaceWidth(): int
234
    {
235 6
        return $this->surface_width;
236
    }
237
238
    public function setSurfaceWidth(int $surfaceWidth): Colony
239
    {
240
        $this->surface_width = $surfaceWidth;
241
        return $this;
242
    }
243
244 24
    public function getColonyClass(): ColonyClass
245
    {
246 24
        return $this->colonyClass;
247
    }
248
249
    public function setColonyClass(ColonyClass $colonyClass): Colony
250
    {
251
        $this->colonyClass = $colonyClass;
252
        return $this;
253
    }
254
255 11
    public function getStorageSum(): int
256
    {
257 11
        return array_reduce(
258 11
            $this->getStorage()->getValues(),
259 11
            fn(int $sum, Storage $storage): int => $sum + $storage->getAmount(),
260 11
            0
261 11
        );
262
    }
263
264 17
    public function getStarsystemMap(): StarSystemMap
265
    {
266 17
        return $this->starsystem_map;
267
    }
268
269 10
    public function getLocation(): Map|StarSystemMap
270
    {
271 10
        return $this->getStarsystemMap();
272
    }
273
274
    public function setStarsystemMap(StarSystemMap $systemMap): Colony
275
    {
276
        $this->starsystem_map = $systemMap;
277
278
        return $this;
279
    }
280
281 8
    public function getSystem(): StarSystem
282
    {
283 8
        return $this->getStarsystemMap()->getSystem();
284
    }
285
286 2
    public function getBeamFactor(): int
287
    {
288 2
        return 10;
289
    }
290
291 5
    public function getPlanetFields(): Collection
292
    {
293 5
        return $this->planetFields;
294
    }
295
296 2
    public function getBeamableStorage(): Collection
297
    {
298 2
        return CommodityTransfer::excludeNonBeamable($this->storage);
299
    }
300
301 18
    public function getStorage(): Collection
302
    {
303 18
        return $this->storage;
304
    }
305
306 1
    public function isDefended(): bool
307
    {
308 1
        return !$this->getDefenders()->isEmpty();
309
    }
310
311
    /**
312
     * @return Collection<int, Fleet>
313
     */
314 1
    public function getDefenders(): Collection
315
    {
316 1
        return $this->defenders;
317
    }
318
319 1
    public function isBlocked(): bool
320
    {
321 1
        return !$this->getBlockers()->isEmpty();
322
    }
323
324
    /**
325
     * @return Collection<int, Fleet>
326
     */
327 1
    public function getBlockers(): Collection
328
    {
329 1
        return $this->blockers;
330
    }
331
332
    public function getCrewAssignments(): Collection
333
    {
334
        return $this->crewAssignments;
335
    }
336
337 3
    public function getCrewAssignmentAmount(): int
338
    {
339 3
        return $this->crewAssignments->count();
340
    }
341
342 1
    public function getCrewTrainingAmount(): int
343
    {
344 1
        return $this->crewTrainings->count();
345
    }
346
347 5
    public function isFree(): bool
348
    {
349 5
        return $this->getUserId() === UserConstants::USER_NOONE;
350
    }
351
352 16
    public function getUser(): User
353
    {
354 16
        return $this->user;
355
    }
356
357
    public function setUser(User $user): Colony
358
    {
359
        $this->user = $user;
360
        return $this;
361
    }
362
363 10
    public function getWorkers(): int
364
    {
365 10
        return $this->getChangeable()->getWorkers();
366
    }
367
368 5
    public function getWorkless(): int
369
    {
370 5
        return $this->getChangeable()->getWorkless();
371
    }
372
373 4
    public function getMaxBev(): int
374
    {
375 4
        return $this->getChangeable()->getMaxBev();
376
    }
377
378 7
    public function getMaxEps(): int
379
    {
380 7
        return $this->getChangeable()->getMaxEps();
381
    }
382
383 11
    public function getMaxStorage(): int
384
    {
385 11
        return $this->getChangeable()->getMaxStorage();
386
    }
387
388 10
    public function getPopulation(): int
389
    {
390 10
        return $this->getChangeable()->getPopulation();
391
    }
392
393 2
    public function getSectorString(): string
394
    {
395 2
        return $this->getStarsystemMap()->getSectorString();
396
    }
397
398
    public function isColony(): bool
399
    {
400
        return true;
401
    }
402
403 30
    public function getHostType(): PlanetFieldHostTypeEnum
404
    {
405 30
        return PlanetFieldHostTypeEnum::COLONY;
406
    }
407
408
    public function getDefaultViewIdentifier(): string
409
    {
410
        return ShowColony::VIEW_IDENTIFIER;
411
    }
412
413
    public function isMenuAllowed(ColonyMenuEnum $menu): bool
414
    {
415
        return true;
416
    }
417
418 4
    public function getTransferEntityType(): TransferEntityTypeEnum
419
    {
420 4
        return TransferEntityTypeEnum::COLONY;
421
    }
422
423
    public function getHref(): string
424
    {
425
        return sprintf(
426
            '%s?%s=1&id=%d',
427
            ModuleEnum::COLONY->getPhpPage(),
428
            ShowColony::VIEW_IDENTIFIER,
429
            $this->getId()
430
        );
431
    }
432
433
    public function getComponentParameters(): string
434
    {
435
        return sprintf(
436
            '&hosttype=%d&id=%d',
437
            $this->getHostType()->value,
438
            $this->getId()
439
        );
440
    }
441
}
442