Total Complexity | 57 |
Total Lines | 324 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PlanetField 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 PlanetField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | #[Table(name: 'stu_colonies_fielddata')] |
||
19 | #[Index(name: 'colony_field_idx', columns: ['colonies_id', 'field_id'])] |
||
20 | #[Index(name: 'sandbox_field_idx', columns: ['colony_sandbox_id', 'field_id'])] |
||
21 | #[Index(name: 'colony_building_active_idx', columns: ['colonies_id', 'buildings_id', 'aktiv'])] |
||
22 | #[Index(name: 'sandbox_building_active_idx', columns: ['colony_sandbox_id', 'buildings_id', 'aktiv'])] |
||
23 | #[Index(name: 'active_idx', columns: ['aktiv'])] |
||
24 | #[Entity(repositoryClass: PlanetFieldRepository::class)] |
||
25 | class PlanetField |
||
26 | { |
||
27 | #[Id] |
||
28 | #[Column(type: 'integer')] |
||
29 | #[GeneratedValue(strategy: 'IDENTITY')] |
||
30 | private int $id; |
||
31 | |||
32 | #[Column(type: 'integer', nullable: true)] |
||
33 | private ?int $colonies_id = null; |
||
|
|||
34 | |||
35 | #[Column(type: 'integer', nullable: true)] |
||
36 | private ?int $colony_sandbox_id = null; |
||
37 | |||
38 | #[Column(type: 'smallint')] |
||
39 | private int $field_id = 0; |
||
40 | |||
41 | #[Column(type: 'integer')] |
||
42 | private int $type_id = 0; |
||
43 | |||
44 | #[Column(type: 'integer', nullable: true)] |
||
45 | private ?int $buildings_id = null; |
||
46 | |||
47 | #[Column(type: 'integer', nullable: true)] |
||
48 | private ?int $terraforming_id = null; |
||
49 | |||
50 | #[Column(type: 'smallint')] |
||
51 | private int $integrity = 0; |
||
52 | |||
53 | #[Column(type: 'integer')] |
||
54 | private int $aktiv = 0; |
||
55 | |||
56 | #[Column(type: 'boolean')] |
||
57 | private bool $activate_after_build = true; |
||
58 | |||
59 | #[ManyToOne(targetEntity: Building::class)] |
||
60 | #[JoinColumn(name: 'buildings_id', referencedColumnName: 'id')] |
||
61 | private ?Building $building = null; |
||
62 | |||
63 | #[ManyToOne(targetEntity: Terraforming::class)] |
||
64 | #[JoinColumn(name: 'terraforming_id', referencedColumnName: 'id')] |
||
65 | private ?Terraforming $terraforming = null; |
||
66 | |||
67 | #[ManyToOne(targetEntity: Colony::class)] |
||
68 | #[JoinColumn(name: 'colonies_id', referencedColumnName: 'id', onDelete: 'CASCADE')] |
||
69 | private ?Colony $colony = null; |
||
70 | |||
71 | #[ManyToOne(targetEntity: ColonySandbox::class)] |
||
72 | #[JoinColumn(name: 'colony_sandbox_id', referencedColumnName: 'id', onDelete: 'CASCADE')] |
||
73 | private ?ColonySandbox $sandbox = null; |
||
74 | |||
75 | private bool $buildmode = false; |
||
76 | |||
77 | public function getId(): int |
||
78 | { |
||
79 | return $this->id; |
||
80 | } |
||
81 | |||
82 | public function getFieldId(): int |
||
83 | { |
||
84 | return $this->field_id; |
||
85 | } |
||
86 | |||
87 | public function setFieldId(int $fieldId): PlanetField |
||
88 | { |
||
89 | $this->field_id = $fieldId; |
||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | public function getFieldType(): int |
||
94 | { |
||
95 | return $this->type_id; |
||
96 | } |
||
97 | |||
98 | public function setFieldType(int $planetFieldTypeId): PlanetField |
||
99 | { |
||
100 | $this->type_id = $planetFieldTypeId; |
||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | public function getBuildingId(): ?int |
||
105 | { |
||
106 | return $this->buildings_id; |
||
107 | } |
||
108 | |||
109 | public function getTerraformingId(): ?int |
||
112 | } |
||
113 | |||
114 | public function getIntegrity(): int |
||
115 | { |
||
116 | return $this->integrity; |
||
117 | } |
||
118 | |||
119 | public function setIntegrity(int $integrity): PlanetField |
||
120 | { |
||
121 | $this->integrity = $integrity; |
||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | public function getActive(): int |
||
126 | { |
||
127 | return $this->aktiv; |
||
128 | } |
||
129 | |||
130 | public function setActive(int $aktiv): PlanetField |
||
131 | { |
||
132 | $this->aktiv = $aktiv; |
||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | public function getActivateAfterBuild(): bool |
||
137 | { |
||
138 | return $this->activate_after_build; |
||
139 | } |
||
140 | |||
141 | public function setActivateAfterBuild(bool $activateAfterBuild): PlanetField |
||
142 | { |
||
143 | $this->activate_after_build = $activateAfterBuild; |
||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | public function setBuildMode(bool $value): void |
||
148 | { |
||
149 | $this->buildmode = $value; |
||
150 | } |
||
151 | |||
152 | public function getBuildtime(): int |
||
153 | { |
||
154 | return $this->getActive(); |
||
155 | } |
||
156 | |||
157 | public function isActive(): bool |
||
158 | { |
||
159 | return $this->getActive() === 1; |
||
160 | } |
||
161 | |||
162 | public function isActivateable(): bool |
||
163 | { |
||
164 | $building = $this->building; |
||
165 | if ($building === null) { |
||
166 | return false; |
||
167 | } |
||
168 | if ($this->isUnderConstruction()) { |
||
169 | return false; |
||
170 | } |
||
171 | return $building->isActivateable(); |
||
172 | } |
||
173 | |||
174 | public function hasHighDamage(): bool |
||
175 | { |
||
176 | if (!$this->isDamaged()) { |
||
177 | return false; |
||
178 | } |
||
179 | |||
180 | $building = $this->getBuilding(); |
||
181 | if ($building === null) { |
||
182 | throw new RuntimeException('should only be called on fields with building!'); |
||
183 | } |
||
184 | |||
185 | return round((100 / $building->getIntegrity()) * $this->getIntegrity()) < 50; |
||
186 | } |
||
187 | |||
188 | public function isUnderConstruction(): bool |
||
189 | { |
||
190 | return $this->getActive() > 1; |
||
191 | } |
||
192 | |||
193 | public function hasBuilding(): bool |
||
194 | { |
||
195 | return $this->getBuilding() !== null; |
||
196 | } |
||
197 | |||
198 | public function getCssClass(): string |
||
199 | { |
||
200 | if ($this->buildmode === true) { |
||
201 | return 'cfb'; |
||
202 | } |
||
203 | if ($this->isActive()) { |
||
204 | if ($this->isDamaged()) { |
||
205 | return 'cfld'; |
||
206 | } |
||
207 | return 'cfa'; |
||
208 | } |
||
209 | if ($this->hasHighDamage()) { |
||
210 | return 'cfhd'; |
||
211 | } |
||
212 | if ($this->hasBuilding()) { |
||
213 | if ($this->isUnderConstruction()) { |
||
214 | return 'cfc'; |
||
215 | } |
||
216 | |||
217 | if ($this->isActivateable()) { |
||
218 | return 'cfd'; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | return 'cfu'; |
||
223 | } |
||
224 | |||
225 | public function getBuildingState(): string |
||
231 | } |
||
232 | |||
233 | public function getBuilding(): ?Building |
||
234 | { |
||
235 | return $this->building; |
||
236 | } |
||
237 | |||
238 | public function setBuilding(?Building $building): PlanetField |
||
239 | { |
||
240 | $this->building = $building; |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | public function isDamaged(): bool |
||
246 | { |
||
247 | $building = $this->building; |
||
248 | if ($building === null) { |
||
249 | return false; |
||
250 | } |
||
251 | if ($this->isUnderConstruction()) { |
||
252 | return false; |
||
253 | } |
||
254 | return $this->getIntegrity() !== $building->getIntegrity(); |
||
255 | } |
||
256 | |||
257 | public function clearBuilding(): void |
||
258 | { |
||
259 | $this->setBuilding(null); |
||
260 | $this->setIntegrity(0); |
||
261 | $this->setActive(0); |
||
262 | } |
||
263 | |||
264 | public function getHost(): Colony|ColonySandbox |
||
265 | { |
||
266 | $colony = $this->colony; |
||
267 | $sandbox = $this->sandbox; |
||
268 | |||
269 | if ($colony === null && $sandbox === null) { |
||
270 | throw new RuntimeException('Both colony and sandbox are null. Ensure one is set before calling getHost().'); |
||
271 | } |
||
272 | |||
273 | return $colony ?? $sandbox; |
||
274 | } |
||
275 | |||
276 | public function setColony(Colony $colony): PlanetField |
||
277 | { |
||
278 | $this->colony = $colony; |
||
279 | return $this; |
||
280 | } |
||
281 | |||
282 | public function setColonySandbox(ColonySandbox $sandbox): PlanetField |
||
283 | { |
||
284 | $this->sandbox = $sandbox; |
||
285 | return $this; |
||
286 | } |
||
287 | |||
288 | public function getTerraforming(): ?Terraforming |
||
291 | } |
||
292 | |||
293 | public function setTerraforming(?Terraforming $terraforming): PlanetField |
||
298 | } |
||
299 | |||
300 | public function getDayNightPrefix(int $timestamp): string |
||
301 | { |
||
302 | $twilightZone = $this->getHost()->getTwilightZone($timestamp); |
||
303 | |||
304 | if ($twilightZone >= 0) { |
||
305 | return $this->getFieldId() % $this->getHost()->getSurfaceWidth() >= $twilightZone ? 'n' : 't'; |
||
306 | } |
||
307 | |||
308 | return $this->getFieldId() % $this->getHost()->getSurfaceWidth() < -$twilightZone ? 'n' : 't'; |
||
309 | } |
||
310 | |||
311 | public function getBuildProgress(): int |
||
312 | { |
||
313 | $building = $this->getBuilding(); |
||
314 | if ($building === null) { |
||
315 | return 0; |
||
316 | } |
||
317 | |||
318 | $start = $this->getBuildtime() - $building->getBuildTime(); |
||
319 | return time() - $start; |
||
320 | } |
||
321 | |||
322 | public function getOverlayWidth(): int |
||
323 | { |
||
324 | $building = $this->getBuilding(); |
||
325 | if ($building === null) { |
||
326 | throw new RuntimeException('building is null'); |
||
327 | } |
||
328 | |||
329 | $buildtime = $building->getBuildtime(); |
||
330 | $perc = max(0, @round((100 / $buildtime) * min($this->getBuildProgress(), $buildtime))); |
||
331 | return (int) round((40 / 100) * $perc); |
||
332 | } |
||
333 | |||
334 | public function getPictureType(): string |
||
337 | } |
||
338 | |||
339 | public function isColonizeAble(): bool |
||
340 | { |
||
341 | return in_array($this->getFieldType(), $this->getHost()->getColonyClass()->getColonizeableFields()); |
||
342 | } |
||
343 | } |
||
344 |