Total Complexity | 54 |
Total Lines | 404 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 |
||
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; |
||
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 = []; |
||
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 | public function getId(): int |
||
146 | { |
||
147 | return $this->id; |
||
148 | } |
||
149 | |||
150 | public function getChangeable(): ColonyChangeable |
||
151 | { |
||
152 | 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 | public function getUserId(): int |
||
163 | { |
||
164 | return $this->user_id; |
||
165 | } |
||
166 | |||
167 | public function getSx(): int |
||
168 | { |
||
169 | return $this->getStarsystemMap()->getSx(); |
||
170 | } |
||
171 | |||
172 | public function getSy(): int |
||
173 | { |
||
174 | return $this->getStarsystemMap()->getSy(); |
||
175 | } |
||
176 | |||
177 | public function getName(): string |
||
178 | { |
||
179 | return $this->name; |
||
180 | } |
||
181 | |||
182 | public function setName(string $name): Colony |
||
183 | { |
||
184 | $this->name = $name; |
||
185 | return $this; |
||
186 | } |
||
187 | |||
188 | public function getPlanetName(): string |
||
189 | { |
||
190 | 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 | public function getRotationFactor(): int |
||
222 | { |
||
223 | 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 | public function getSurfaceWidth(): int |
||
236 | } |
||
237 | |||
238 | public function setSurfaceWidth(int $surfaceWidth): Colony |
||
239 | { |
||
240 | $this->surface_width = $surfaceWidth; |
||
241 | return $this; |
||
242 | } |
||
243 | |||
244 | public function getColonyClass(): ColonyClass |
||
245 | { |
||
246 | return $this->colonyClass; |
||
247 | } |
||
248 | |||
249 | public function setColonyClass(ColonyClass $colonyClass): Colony |
||
253 | } |
||
254 | |||
255 | public function getStorageSum(): int |
||
256 | { |
||
257 | return array_reduce( |
||
258 | $this->getStorage()->getValues(), |
||
259 | fn(int $sum, Storage $storage): int => $sum + $storage->getAmount(), |
||
260 | 0 |
||
261 | ); |
||
262 | } |
||
263 | |||
264 | public function getStarsystemMap(): StarSystemMap |
||
265 | { |
||
266 | return $this->starsystem_map; |
||
267 | } |
||
268 | |||
269 | public function getLocation(): Map|StarSystemMap |
||
270 | { |
||
271 | 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 | public function getSystem(): StarSystem |
||
282 | { |
||
283 | return $this->getStarsystemMap()->getSystem(); |
||
284 | } |
||
285 | |||
286 | public function getBeamFactor(): int |
||
287 | { |
||
288 | return 10; |
||
289 | } |
||
290 | |||
291 | public function getPlanetFields(): Collection |
||
292 | { |
||
293 | return $this->planetFields; |
||
294 | } |
||
295 | |||
296 | public function getBeamableStorage(): Collection |
||
297 | { |
||
298 | return CommodityTransfer::excludeNonBeamable($this->storage); |
||
299 | } |
||
300 | |||
301 | public function getStorage(): Collection |
||
302 | { |
||
303 | return $this->storage; |
||
304 | } |
||
305 | |||
306 | public function isDefended(): bool |
||
307 | { |
||
308 | return !$this->getDefenders()->isEmpty(); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @return Collection<int, Fleet> |
||
313 | */ |
||
314 | public function getDefenders(): Collection |
||
315 | { |
||
316 | return $this->defenders; |
||
317 | } |
||
318 | |||
319 | public function isBlocked(): bool |
||
320 | { |
||
321 | return !$this->getBlockers()->isEmpty(); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @return Collection<int, Fleet> |
||
326 | */ |
||
327 | public function getBlockers(): Collection |
||
328 | { |
||
329 | return $this->blockers; |
||
330 | } |
||
331 | |||
332 | public function getCrewAssignments(): Collection |
||
333 | { |
||
334 | return $this->crewAssignments; |
||
335 | } |
||
336 | |||
337 | public function getCrewAssignmentAmount(): int |
||
338 | { |
||
339 | return $this->crewAssignments->count(); |
||
340 | } |
||
341 | |||
342 | public function getCrewTrainingAmount(): int |
||
343 | { |
||
344 | return $this->crewTrainings->count(); |
||
345 | } |
||
346 | |||
347 | public function isFree(): bool |
||
348 | { |
||
349 | return $this->getUserId() === UserConstants::USER_NOONE; |
||
350 | } |
||
351 | |||
352 | public function getUser(): User |
||
353 | { |
||
354 | return $this->user; |
||
355 | } |
||
356 | |||
357 | public function setUser(User $user): Colony |
||
358 | { |
||
359 | $this->user = $user; |
||
360 | return $this; |
||
361 | } |
||
362 | |||
363 | public function getWorkers(): int |
||
364 | { |
||
365 | return $this->getChangeable()->getWorkers(); |
||
366 | } |
||
367 | |||
368 | public function getWorkless(): int |
||
369 | { |
||
370 | return $this->getChangeable()->getWorkless(); |
||
371 | } |
||
372 | |||
373 | public function getMaxBev(): int |
||
374 | { |
||
375 | return $this->getChangeable()->getMaxBev(); |
||
376 | } |
||
377 | |||
378 | public function getMaxEps(): int |
||
379 | { |
||
380 | return $this->getChangeable()->getMaxEps(); |
||
381 | } |
||
382 | |||
383 | public function getMaxStorage(): int |
||
384 | { |
||
385 | return $this->getChangeable()->getMaxStorage(); |
||
386 | } |
||
387 | |||
388 | public function getPopulation(): int |
||
389 | { |
||
390 | return $this->getChangeable()->getPopulation(); |
||
391 | } |
||
392 | |||
393 | public function getSectorString(): string |
||
394 | { |
||
395 | return $this->getStarsystemMap()->getSectorString(); |
||
396 | } |
||
397 | |||
398 | public function isColony(): bool |
||
399 | { |
||
400 | return true; |
||
401 | } |
||
402 | |||
403 | public function getHostType(): PlanetFieldHostTypeEnum |
||
404 | { |
||
405 | 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 | public function getTransferEntityType(): TransferEntityTypeEnum |
||
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( |
||
439 | ); |
||
440 | } |
||
441 | } |
||
442 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths