Building   B
last analyzed

Complexity

Total Complexity 50

Size/Duplication

Total Lines 362
Duplicated Lines 0 %

Test Coverage

Coverage 44.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 125
dl 0
loc 362
ccs 53
cts 118
cp 0.4492
rs 8.4
c 1
b 0
f 0
wmc 50

45 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 3 1
A getResearchId() 0 3 1
A setLimit() 0 4 1
A setWorkers() 0 4 1
A getStorage() 0 3 1
A getBuildtime() 0 3 1
A getRestrictions() 0 3 1
A getCommodities() 0 3 1
A __construct() 0 7 1
A getBuildingType() 0 4 1
A setEpsCost() 0 4 1
A setEpsProduction() 0 4 1
A setEpsStorage() 0 4 1
A getEpsProductionCss() 0 9 3
A getLimitColony() 0 3 1
A getIntegrity() 0 3 1
A getCosts() 0 3 1
A setLimitColony() 0 4 1
A setResearchId() 0 4 1
A getShieldCapacity() 0 10 3
A getIsActivateable() 0 3 1
A setIsActivateable() 0 4 1
A setBuildtime() 0 4 1
A getBmCol() 0 3 1
A getWorkers() 0 3 1
A setIntegrity() 0 4 1
A getBuildableFields() 0 3 1
A setBmCol() 0 4 1
A getLimit() 0 3 1
A getEpsStorage() 0 3 1
A setStorage() 0 4 1
A setView() 0 4 1
A getEpsProduction() 0 3 1
A getView() 0 3 1
A getName() 0 3 1
A getHousing() 0 3 1
A isViewable() 0 3 1
A hasLimit() 0 3 1
A setHousing() 0 4 1
A isRemovable() 0 4 2
A getEpsCost() 0 3 1
A isActivateable() 0 3 1
A hasLimitColony() 0 3 1
A getId() 0 3 1
A setName() 0 4 1

How to fix   Complexity   

Complex Class

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
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\OneToMany;
15
use Doctrine\ORM\Mapping\OrderBy;
16
use Doctrine\ORM\Mapping\Table;
17
use Stu\Component\Building\BuildingFunctionEnum;
18
use Stu\Component\Colony\Shields\ColonyShieldingManager;
19
use Stu\Orm\Repository\BuildingRepository;
20
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 7
    public function getId(): int
121
    {
122 7
        return $this->id;
123
    }
124
125 8
    public function getName(): string
126
    {
127 8
        return $this->name;
128
    }
129
130
    public function setName(string $name): Building
131
    {
132
        $this->name = $name;
133
        return $this;
134
    }
135
136 2
    public function getStorage(): int
137
    {
138 2
        return $this->lager;
139
    }
140
141
    public function setStorage(int $storage): Building
142
    {
143
        $this->lager = $storage;
144
        return $this;
145
    }
146
147 2
    public function getEpsStorage(): int
148
    {
149 2
        return $this->eps;
150
    }
151
152
    public function setEpsStorage(int $epsStorage): Building
153
    {
154
        $this->eps = $epsStorage;
155
        return $this;
156
    }
157
158 1
    public function getEpsCost(): int
159
    {
160 1
        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 2
    public function getEpsProduction(): int
170
    {
171 2
        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 2
    public function getHousing(): int
181
    {
182 2
        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 2
    public function getWorkers(): int
192
    {
193 2
        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 6
    public function getIntegrity(): int
203
    {
204 6
        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 1
    public function getBuildtime(): int
236
    {
237 1
        return $this->buildtime;
238
    }
239
240
    public function setBuildtime(int $buildtime): Building
241
    {
242
        $this->buildtime = $buildtime;
243
        return $this;
244
    }
245
246 1
    public function getLimit(): int
247
    {
248 1
        return $this->blimit;
249
    }
250
251
    public function setLimit(int $limit): Building
252
    {
253
        $this->blimit = $limit;
254
        return $this;
255
    }
256
257 1
    public function getLimitColony(): int
258
    {
259 1
        return $this->bclimit;
260
    }
261
262
    public function setLimitColony(int $limitColony): Building
263
    {
264
        $this->bclimit = $limitColony;
265
        return $this;
266
    }
267
268 8
    public function getIsActivateable(): bool
269
    {
270 8
        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 8
    public function isActivateable(): bool
291
    {
292 8
        return $this->getIsActivateable();
293
    }
294
295
    public function isViewable(): bool
296
    {
297
        return $this->getView();
298
    }
299
300 6
    public function getBuildingType(): int
301
    {
302
        // return 0 for now
303 6
        return 0;
304
    }
305
306 1
    public function getEpsProductionCss(): string
307
    {
308 1
        if ($this->getEpsProduction() < 0) {
309
            return 'negative';
310
        }
311 1
        if ($this->getEpsProduction() > 0) {
312 1
            return 'positive';
313
        }
314
        return '';
315
    }
316
317 1
    public function hasLimit(): bool
318
    {
319 1
        return $this->getLimit() > 0;
320
    }
321
322 1
    public function hasLimitColony(): bool
323
    {
324 1
        return $this->getLimitColony() > 0;
325
    }
326
327
    /**
328
     * @return Collection<int, PlanetFieldTypeBuilding>
329
     */
330 5
    public function getBuildableFields(): Collection
331
    {
332 5
        return $this->possibleFieldTypes;
333
    }
334
335
    /**
336
     * @return Collection<int, BuildingCost>
337
     */
338 1
    public function getCosts(): Collection
339
    {
340 1
        return $this->costs;
341
    }
342
343
    /**
344
     * @return Collection<int, BuildingCommodity>
345
     */
346 2
    public function getCommodities(): Collection
347
    {
348 2
        return $this->commodities;
349
    }
350
351
    /**
352
     * @return Collection<int, BuildingFunction>
353
     */
354 5
    public function getFunctions(): Collection
355
    {
356 5
        return $this->functions;
357
    }
358
359 1
    public function isRemovable(): bool
360
    {
361 1
        return !$this->getFunctions()->containsKey(BuildingFunctionEnum::COLONY_CENTRAL->value)
362 1
            && !$this->getFunctions()->containsKey(BuildingFunctionEnum::BASE_CAMP->value);
363
    }
364
365 2
    public function getShieldCapacity(): ?int
366
    {
367 2
        if ($this->getFunctions()->containsKey(BuildingFunctionEnum::SHIELD_GENERATOR->value) === true) {
368
            return ColonyShieldingManager::SHIELD_GENERATOR_CAPACITY;
369
        }
370
371 2
        if ($this->getFunctions()->containsKey(BuildingFunctionEnum::SHIELD_BATTERY->value) === true) {
372
            return ColonyShieldingManager::SHIELD_BATTERY_CAPACITY;
373
        }
374 2
        return null;
375
    }
376
377
    /**
378
     * @return Collection<int, ColonyClassRestriction>
379
     */
380
    public function getRestrictions(): Collection
381
    {
382
        return $this->restrictions;
383
    }
384
}
385