Total Complexity | 50 |
Total Lines | 362 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Building 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 Building, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | #[Table(name: 'stu_buildings')] |
||
22 | #[Index(name: 'eps_production_idx', columns: ['eps_proc'])] |
||
23 | #[Index(name: 'buildmenu_column_idx', columns: ['bm_col'])] |
||
24 | #[Index(name: 'building_research_idx', columns: ['research_id'])] |
||
25 | #[Entity(repositoryClass: BuildingRepository::class)] |
||
26 | class Building |
||
27 | { |
||
28 | #[Id] |
||
29 | #[Column(type: 'integer')] |
||
30 | #[GeneratedValue(strategy: 'IDENTITY')] |
||
31 | private int $id; |
||
32 | |||
33 | #[Column(type: 'string')] |
||
34 | private string $name = ''; |
||
35 | |||
36 | #[Column(type: 'smallint')] |
||
37 | private int $lager = 0; |
||
38 | |||
39 | #[Column(type: 'smallint')] |
||
40 | private int $eps = 0; |
||
41 | |||
42 | #[Column(type: 'smallint')] |
||
43 | private int $eps_cost = 0; |
||
44 | |||
45 | #[Column(type: 'smallint')] |
||
46 | private int $eps_proc = 0; |
||
47 | |||
48 | #[Column(type: 'smallint')] |
||
49 | private int $bev_pro = 0; |
||
50 | |||
51 | #[Column(type: 'smallint')] |
||
52 | private int $bev_use = 0; |
||
53 | |||
54 | #[Column(type: 'smallint')] |
||
55 | private int $integrity = 0; |
||
56 | |||
57 | #[Column(type: 'integer')] |
||
58 | private int $research_id = 0; |
||
59 | |||
60 | #[Column(type: 'boolean')] |
||
61 | private bool $view = false; |
||
62 | |||
63 | #[Column(type: 'integer')] |
||
64 | private int $buildtime = 0; |
||
65 | |||
66 | #[Column(type: 'smallint')] |
||
67 | private int $blimit = 0; |
||
68 | |||
69 | #[Column(type: 'smallint')] |
||
70 | private int $bclimit = 0; |
||
71 | |||
72 | #[Column(type: 'boolean')] |
||
73 | private bool $is_activateable = false; |
||
74 | |||
75 | #[Column(type: 'smallint')] |
||
76 | private int $bm_col = 0; |
||
77 | |||
78 | /** |
||
79 | * @var ArrayCollection<int, BuildingCost> |
||
80 | */ |
||
81 | #[OneToMany(targetEntity: BuildingCost::class, mappedBy: 'building')] |
||
82 | #[OrderBy(['commodity_id' => 'ASC'])] |
||
83 | private Collection $costs; |
||
84 | |||
85 | /** |
||
86 | * @var ArrayCollection<int, BuildingFunction> |
||
87 | */ |
||
88 | #[OneToMany(targetEntity: BuildingFunction::class, mappedBy: 'building', indexBy: 'function')] |
||
89 | private Collection $functions; |
||
90 | |||
91 | /** |
||
92 | * @var ArrayCollection<int, BuildingCommodity> |
||
93 | */ |
||
94 | #[OneToMany(targetEntity: BuildingCommodity::class, mappedBy: 'building', indexBy: 'commodity_id')] |
||
95 | #[OrderBy(['commodity_id' => 'ASC'])] |
||
96 | private Collection $commodities; |
||
97 | |||
98 | /** |
||
99 | * @var ArrayCollection<int, PlanetFieldTypeBuilding> |
||
100 | */ |
||
101 | #[OneToMany(targetEntity: PlanetFieldTypeBuilding::class, mappedBy: 'building', indexBy: 'type')] |
||
102 | private Collection $possibleFieldTypes; |
||
103 | |||
104 | /** |
||
105 | * @var ArrayCollection<int, ColonyClassRestriction> |
||
106 | */ |
||
107 | #[OneToMany(mappedBy: 'building', targetEntity: ColonyClassRestriction::class)] |
||
108 | private Collection $restrictions; |
||
109 | |||
110 | |||
111 | public function __construct() |
||
112 | { |
||
113 | $this->costs = new ArrayCollection(); |
||
114 | $this->functions = new ArrayCollection(); |
||
115 | $this->commodities = new ArrayCollection(); |
||
116 | $this->possibleFieldTypes = new ArrayCollection(); |
||
117 | $this->restrictions = new ArrayCollection(); |
||
118 | } |
||
119 | |||
120 | public function getId(): int |
||
121 | { |
||
122 | return $this->id; |
||
123 | } |
||
124 | |||
125 | public function getName(): string |
||
126 | { |
||
127 | return $this->name; |
||
128 | } |
||
129 | |||
130 | public function setName(string $name): Building |
||
131 | { |
||
132 | $this->name = $name; |
||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | public function getStorage(): int |
||
137 | { |
||
138 | return $this->lager; |
||
139 | } |
||
140 | |||
141 | public function setStorage(int $storage): Building |
||
142 | { |
||
143 | $this->lager = $storage; |
||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | public function getEpsStorage(): int |
||
148 | { |
||
149 | return $this->eps; |
||
150 | } |
||
151 | |||
152 | public function setEpsStorage(int $epsStorage): Building |
||
153 | { |
||
154 | $this->eps = $epsStorage; |
||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | public function getEpsCost(): int |
||
159 | { |
||
160 | return $this->eps_cost; |
||
161 | } |
||
162 | |||
163 | public function setEpsCost(int $epsCost): Building |
||
164 | { |
||
165 | $this->eps_cost = $epsCost; |
||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | public function getEpsProduction(): int |
||
170 | { |
||
171 | return $this->eps_proc; |
||
172 | } |
||
173 | |||
174 | public function setEpsProduction(int $epsProduction): Building |
||
175 | { |
||
176 | $this->eps_proc = $epsProduction; |
||
177 | return $this; |
||
178 | } |
||
179 | |||
180 | public function getHousing(): int |
||
181 | { |
||
182 | return $this->bev_pro; |
||
183 | } |
||
184 | |||
185 | public function setHousing(int $housing): Building |
||
186 | { |
||
187 | $this->bev_pro = $housing; |
||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | public function getWorkers(): int |
||
192 | { |
||
193 | return $this->bev_use; |
||
194 | } |
||
195 | |||
196 | public function setWorkers(int $workers): Building |
||
197 | { |
||
198 | $this->bev_use = $workers; |
||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | public function getIntegrity(): int |
||
203 | { |
||
204 | return $this->integrity; |
||
205 | } |
||
206 | |||
207 | public function setIntegrity(int $integrity): Building |
||
208 | { |
||
209 | $this->integrity = $integrity; |
||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | public function getResearchId(): int |
||
214 | { |
||
215 | return $this->research_id; |
||
216 | } |
||
217 | |||
218 | public function setResearchId(int $researchId): Building |
||
219 | { |
||
220 | $this->research_id = $researchId; |
||
221 | return $this; |
||
222 | } |
||
223 | |||
224 | public function getView(): bool |
||
225 | { |
||
226 | return $this->view; |
||
227 | } |
||
228 | |||
229 | public function setView(bool $view): Building |
||
230 | { |
||
231 | $this->view = $view; |
||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | public function getBuildtime(): int |
||
236 | { |
||
237 | return $this->buildtime; |
||
238 | } |
||
239 | |||
240 | public function setBuildtime(int $buildtime): Building |
||
241 | { |
||
242 | $this->buildtime = $buildtime; |
||
243 | return $this; |
||
244 | } |
||
245 | |||
246 | public function getLimit(): int |
||
247 | { |
||
248 | return $this->blimit; |
||
249 | } |
||
250 | |||
251 | public function setLimit(int $limit): Building |
||
252 | { |
||
253 | $this->blimit = $limit; |
||
254 | return $this; |
||
255 | } |
||
256 | |||
257 | public function getLimitColony(): int |
||
258 | { |
||
259 | return $this->bclimit; |
||
260 | } |
||
261 | |||
262 | public function setLimitColony(int $limitColony): Building |
||
263 | { |
||
264 | $this->bclimit = $limitColony; |
||
265 | return $this; |
||
266 | } |
||
267 | |||
268 | public function getIsActivateable(): bool |
||
269 | { |
||
270 | return $this->is_activateable; |
||
271 | } |
||
272 | |||
273 | public function setIsActivateable(bool $isActivateable): Building |
||
274 | { |
||
275 | $this->is_activateable = $isActivateable; |
||
276 | return $this; |
||
277 | } |
||
278 | |||
279 | public function getBmCol(): int |
||
280 | { |
||
281 | return $this->bm_col; |
||
282 | } |
||
283 | |||
284 | public function setBmCol(int $buildmenuColumn): Building |
||
285 | { |
||
286 | $this->bm_col = $buildmenuColumn; |
||
287 | return $this; |
||
288 | } |
||
289 | |||
290 | public function isActivateable(): bool |
||
291 | { |
||
292 | return $this->getIsActivateable(); |
||
293 | } |
||
294 | |||
295 | public function isViewable(): bool |
||
296 | { |
||
297 | return $this->getView(); |
||
298 | } |
||
299 | |||
300 | public function getBuildingType(): int |
||
301 | { |
||
302 | // return 0 for now |
||
303 | return 0; |
||
304 | } |
||
305 | |||
306 | public function getEpsProductionCss(): string |
||
307 | { |
||
308 | if ($this->getEpsProduction() < 0) { |
||
309 | return 'negative'; |
||
310 | } |
||
311 | if ($this->getEpsProduction() > 0) { |
||
312 | return 'positive'; |
||
313 | } |
||
314 | return ''; |
||
315 | } |
||
316 | |||
317 | public function hasLimit(): bool |
||
318 | { |
||
319 | return $this->getLimit() > 0; |
||
320 | } |
||
321 | |||
322 | public function hasLimitColony(): bool |
||
323 | { |
||
324 | return $this->getLimitColony() > 0; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @return Collection<int, PlanetFieldTypeBuilding> |
||
329 | */ |
||
330 | public function getBuildableFields(): Collection |
||
331 | { |
||
332 | return $this->possibleFieldTypes; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @return Collection<int, BuildingCost> |
||
337 | */ |
||
338 | public function getCosts(): Collection |
||
339 | { |
||
340 | return $this->costs; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @return Collection<int, BuildingCommodity> |
||
345 | */ |
||
346 | public function getCommodities(): Collection |
||
347 | { |
||
348 | return $this->commodities; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @return Collection<int, BuildingFunction> |
||
353 | */ |
||
354 | public function getFunctions(): Collection |
||
357 | } |
||
358 | |||
359 | public function isRemovable(): bool |
||
360 | { |
||
361 | return !$this->getFunctions()->containsKey(BuildingFunctionEnum::COLONY_CENTRAL->value) |
||
362 | && !$this->getFunctions()->containsKey(BuildingFunctionEnum::BASE_CAMP->value); |
||
363 | } |
||
364 | |||
365 | public function getShieldCapacity(): ?int |
||
366 | { |
||
367 | if ($this->getFunctions()->containsKey(BuildingFunctionEnum::SHIELD_GENERATOR->value) === true) { |
||
368 | return ColonyShieldingManager::SHIELD_GENERATOR_CAPACITY; |
||
369 | } |
||
370 | |||
371 | if ($this->getFunctions()->containsKey(BuildingFunctionEnum::SHIELD_BATTERY->value) === true) { |
||
372 | return ColonyShieldingManager::SHIELD_BATTERY_CAPACITY; |
||
373 | } |
||
374 | return null; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @return Collection<int, ColonyClassRestriction> |
||
379 | */ |
||
380 | public function getRestrictions(): Collection |
||
383 | } |
||
384 | } |
||
385 |