We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 114 |
Total Lines | 525 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractSmrLocation 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 AbstractSmrLocation, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
16 | class AbstractSmrLocation { |
||
17 | |||
18 | /** @var array<int, SmrLocation> */ |
||
19 | protected static array $CACHE_ALL_LOCATIONS; |
||
20 | /** @var array<int, SmrLocation> */ |
||
21 | protected static array $CACHE_LOCATIONS = []; |
||
22 | /** @var array<int, array<int, array<int, SmrLocation>>> */ |
||
23 | protected static array $CACHE_SECTOR_LOCATIONS = []; |
||
24 | |||
25 | protected Database $db; |
||
26 | protected readonly string $SQL; |
||
27 | |||
28 | protected string $name; |
||
29 | protected ?string $processor; |
||
30 | protected string $image; |
||
31 | |||
32 | protected bool $fed; |
||
33 | protected bool $bank; |
||
34 | protected bool $bar; |
||
35 | protected bool $HQ; |
||
36 | protected bool $UG; |
||
37 | |||
38 | /** @var array<int, \Smr\HardwareType> */ |
||
39 | protected array $hardwareSold; |
||
40 | /** @var array<int, SmrShipType> */ |
||
41 | protected array $shipsSold; |
||
42 | /** @var array<int, SmrWeapon> */ |
||
43 | protected array $weaponsSold; |
||
44 | |||
45 | public static function clearCache(): void { |
||
46 | self::$CACHE_ALL_LOCATIONS = []; |
||
47 | self::$CACHE_LOCATIONS = []; |
||
48 | self::$CACHE_SECTOR_LOCATIONS = []; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return array<int, SmrLocation> |
||
53 | */ |
||
54 | public static function getAllLocations(int $gameID, bool $forceUpdate = false): array { |
||
55 | if ($forceUpdate || !isset(self::$CACHE_ALL_LOCATIONS)) { |
||
56 | $db = Database::getInstance(); |
||
57 | $dbResult = $db->read('SELECT * FROM location_type ORDER BY location_type_id'); |
||
58 | $locations = []; |
||
59 | foreach ($dbResult->records() as $dbRecord) { |
||
60 | $locationTypeID = $dbRecord->getInt('location_type_id'); |
||
61 | $locations[$locationTypeID] = SmrLocation::getLocation($gameID, $locationTypeID, $forceUpdate, $dbRecord); |
||
62 | } |
||
63 | self::$CACHE_ALL_LOCATIONS = $locations; |
||
64 | } |
||
65 | return self::$CACHE_ALL_LOCATIONS; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return array<int, array<int, SmrLocation>> |
||
70 | */ |
||
71 | public static function getGalaxyLocations(int $gameID, int $galaxyID, bool $forceUpdate = false): array { |
||
72 | $db = Database::getInstance(); |
||
73 | $dbResult = $db->read('SELECT location_type.*, sector_id FROM location LEFT JOIN sector USING(game_id, sector_id) LEFT JOIN location_type USING (location_type_id) WHERE game_id = ' . $db->escapeNumber($gameID) . ' AND galaxy_id = ' . $db->escapeNumber($galaxyID)); |
||
74 | $galaxyLocations = []; |
||
75 | foreach ($dbResult->records() as $dbRecord) { |
||
76 | $sectorID = $dbRecord->getInt('sector_id'); |
||
77 | $locationTypeID = $dbRecord->getInt('location_type_id'); |
||
78 | $location = self::getLocation($gameID, $locationTypeID, $forceUpdate, $dbRecord); |
||
79 | self::$CACHE_SECTOR_LOCATIONS[$gameID][$sectorID][$locationTypeID] = $location; |
||
80 | $galaxyLocations[$sectorID][$locationTypeID] = $location; |
||
81 | } |
||
82 | return $galaxyLocations; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return array<int, SmrLocation> |
||
87 | */ |
||
88 | public static function getSectorLocations(int $gameID, int $sectorID, bool $forceUpdate = false): array { |
||
89 | if ($forceUpdate || !isset(self::$CACHE_SECTOR_LOCATIONS[$gameID][$sectorID])) { |
||
90 | $db = Database::getInstance(); |
||
91 | $dbResult = $db->read('SELECT * FROM location LEFT JOIN location_type USING (location_type_id) WHERE sector_id = ' . $db->escapeNumber($sectorID) . ' AND game_id=' . $db->escapeNumber($gameID)); |
||
92 | $locations = []; |
||
93 | foreach ($dbResult->records() as $dbRecord) { |
||
94 | $locationTypeID = $dbRecord->getInt('location_type_id'); |
||
95 | $locations[$locationTypeID] = self::getLocation($gameID, $locationTypeID, $forceUpdate, $dbRecord); |
||
96 | } |
||
97 | self::$CACHE_SECTOR_LOCATIONS[$gameID][$sectorID] = $locations; |
||
98 | } |
||
99 | return self::$CACHE_SECTOR_LOCATIONS[$gameID][$sectorID]; |
||
100 | } |
||
101 | |||
102 | public static function addSectorLocation(int $gameID, int $sectorID, SmrLocation $location): void { |
||
103 | self::getSectorLocations($gameID, $sectorID); // make sure cache is populated |
||
104 | $db = Database::getInstance(); |
||
105 | $db->insert('location', [ |
||
106 | 'game_id' => $db->escapeNumber($gameID), |
||
107 | 'sector_id' => $db->escapeNumber($sectorID), |
||
108 | 'location_type_id' => $db->escapeNumber($location->getTypeID()), |
||
109 | ]); |
||
110 | self::$CACHE_SECTOR_LOCATIONS[$gameID][$sectorID][$location->getTypeID()] = $location; |
||
111 | } |
||
112 | |||
113 | public static function moveSectorLocation(int $gameID, int $oldSectorID, int $newSectorID, SmrLocation $location): void { |
||
114 | if ($oldSectorID === $newSectorID) { |
||
115 | return; |
||
116 | } |
||
117 | |||
118 | // Make sure cache is populated |
||
119 | self::getSectorLocations($gameID, $oldSectorID); |
||
120 | self::getSectorLocations($gameID, $newSectorID); |
||
121 | |||
122 | $db = Database::getInstance(); |
||
123 | $db->write('UPDATE location SET sector_id = ' . $db->escapeNumber($newSectorID) . ' WHERE game_id = ' . $db->escapeNumber($gameID) . ' AND sector_id = ' . $db->escapeNumber($oldSectorID) . ' AND location_type_id = ' . $location->getTypeID()); |
||
124 | unset(self::$CACHE_SECTOR_LOCATIONS[$gameID][$oldSectorID][$location->getTypeID()]); |
||
125 | self::$CACHE_SECTOR_LOCATIONS[$gameID][$newSectorID][$location->getTypeID()] = $location; |
||
126 | |||
127 | // Preserve the same element order that we'd have in getSectorLocations |
||
128 | ksort(self::$CACHE_SECTOR_LOCATIONS[$gameID][$newSectorID]); |
||
129 | } |
||
130 | |||
131 | public static function removeSectorLocations(int $gameID, int $sectorID): void { |
||
132 | $db = Database::getInstance(); |
||
133 | $db->write('DELETE FROM location WHERE game_id = ' . $db->escapeNumber($gameID) . ' AND sector_id = ' . $db->escapeNumber($sectorID)); |
||
134 | self::$CACHE_SECTOR_LOCATIONS[$gameID][$sectorID] = []; |
||
135 | } |
||
136 | |||
137 | public static function getLocation(int $gameID, int $locationTypeID, bool $forceUpdate = false, DatabaseRecord $dbRecord = null): SmrLocation { |
||
138 | if ($forceUpdate || !isset(self::$CACHE_LOCATIONS[$locationTypeID])) { |
||
139 | self::$CACHE_LOCATIONS[$locationTypeID] = new SmrLocation($gameID, $locationTypeID, $dbRecord); |
||
140 | } |
||
141 | return self::$CACHE_LOCATIONS[$locationTypeID]; |
||
142 | } |
||
143 | |||
144 | protected function __construct( |
||
145 | protected readonly int $gameID, // use 0 to be independent of game |
||
146 | protected readonly int $typeID, |
||
147 | DatabaseRecord $dbRecord = null |
||
148 | ) { |
||
149 | $this->db = Database::getInstance(); |
||
150 | $this->SQL = 'location_type_id = ' . $this->db->escapeNumber($typeID); |
||
|
|||
151 | |||
152 | if ($dbRecord === null) { |
||
153 | $dbResult = $this->db->read('SELECT * FROM location_type WHERE ' . $this->SQL); |
||
154 | if ($dbResult->hasRecord()) { |
||
155 | $dbRecord = $dbResult->record(); |
||
156 | } |
||
157 | } |
||
158 | $locationExists = $dbRecord !== null; |
||
159 | |||
160 | if ($locationExists) { |
||
161 | $this->name = $dbRecord->getString('location_name'); |
||
162 | $this->processor = $dbRecord->getNullableString('location_processor'); |
||
163 | $this->image = $dbRecord->getString('location_image'); |
||
164 | } else { |
||
165 | throw new Exception('Cannot find location: ' . $typeID); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | public function getGameID(): int { |
||
170 | return $this->gameID; |
||
171 | } |
||
172 | |||
173 | public function getTypeID(): int { |
||
174 | return $this->typeID; |
||
175 | } |
||
176 | |||
177 | public function getRaceID(): int { |
||
178 | if ($this->isFed() && $this->getTypeID() != LOCATION_TYPE_FEDERAL_BEACON) { |
||
179 | return $this->getTypeID() - LOCATION_GROUP_RACIAL_BEACONS; |
||
180 | } |
||
181 | if ($this->isHQ() && $this->getTypeID() != LOCATION_TYPE_FEDERAL_HQ) { |
||
182 | return $this->getTypeID() - LOCATION_GROUP_RACIAL_HQS; |
||
183 | } |
||
184 | return RACE_NEUTRAL; |
||
185 | } |
||
186 | |||
187 | public function getName(): string { |
||
188 | return $this->name; |
||
189 | } |
||
190 | |||
191 | public function setName(string $name): void { |
||
198 | } |
||
199 | |||
200 | public function hasAction(): bool { |
||
201 | return $this->processor !== null; |
||
202 | } |
||
203 | |||
204 | public function getAction(): ?string { |
||
205 | return $this->processor; |
||
206 | } |
||
207 | |||
208 | public function getImage(): string { |
||
209 | return $this->image; |
||
210 | } |
||
211 | |||
212 | public function isFed(): bool { |
||
213 | if (!isset($this->fed)) { |
||
214 | $dbResult = $this->db->read('SELECT 1 FROM location_is_fed WHERE ' . $this->SQL); |
||
215 | $this->fed = $dbResult->hasRecord(); |
||
216 | } |
||
217 | return $this->fed; |
||
218 | } |
||
219 | |||
220 | public function setFed(bool $bool): void { |
||
221 | if ($this->fed === $bool) { |
||
222 | return; |
||
223 | } |
||
224 | if ($bool) { |
||
225 | $this->db->write('INSERT IGNORE INTO location_is_fed (location_type_id) values (' . $this->db->escapeNumber($this->getTypeID()) . ')'); |
||
226 | } else { |
||
227 | $this->db->write('DELETE FROM location_is_fed WHERE ' . $this->SQL); |
||
228 | } |
||
229 | $this->fed = $bool; |
||
230 | } |
||
231 | |||
232 | public function isBank(): bool { |
||
233 | if (!isset($this->bank)) { |
||
234 | $dbResult = $this->db->read('SELECT 1 FROM location_is_bank WHERE ' . $this->SQL); |
||
235 | $this->bank = $dbResult->hasRecord(); |
||
236 | } |
||
237 | return $this->bank; |
||
238 | } |
||
239 | |||
240 | public function setBank(bool $bool): void { |
||
241 | if ($this->bank === $bool) { |
||
242 | return; |
||
243 | } |
||
244 | if ($bool) { |
||
245 | $this->db->insert('location_is_bank', [ |
||
246 | 'location_type_id' => $this->db->escapeNumber($this->getTypeID()), |
||
247 | ]); |
||
248 | } else { |
||
249 | $this->db->write('DELETE FROM location_is_bank WHERE ' . $this->SQL); |
||
250 | } |
||
251 | $this->bank = $bool; |
||
252 | } |
||
253 | |||
254 | public function isBar(): bool { |
||
255 | if (!isset($this->bar)) { |
||
256 | $dbResult = $this->db->read('SELECT 1 FROM location_is_bar WHERE ' . $this->SQL); |
||
257 | $this->bar = $dbResult->hasRecord(); |
||
258 | } |
||
259 | return $this->bar; |
||
260 | } |
||
261 | |||
262 | public function setBar(bool $bool): void { |
||
263 | if ($this->bar === $bool) { |
||
264 | return; |
||
265 | } |
||
266 | if ($bool) { |
||
267 | $this->db->write('INSERT IGNORE INTO location_is_bar (location_type_id) values (' . $this->db->escapeNumber($this->getTypeID()) . ')'); |
||
268 | } else { |
||
269 | $this->db->write('DELETE FROM location_is_bar WHERE ' . $this->SQL); |
||
270 | } |
||
271 | $this->bar = $bool; |
||
272 | } |
||
273 | |||
274 | public function isHQ(): bool { |
||
275 | if (!isset($this->HQ)) { |
||
276 | $dbResult = $this->db->read('SELECT 1 FROM location_is_hq WHERE ' . $this->SQL); |
||
277 | $this->HQ = $dbResult->hasRecord(); |
||
278 | } |
||
279 | return $this->HQ; |
||
280 | } |
||
281 | |||
282 | public function setHQ(bool $bool): void { |
||
283 | if ($this->HQ === $bool) { |
||
284 | return; |
||
285 | } |
||
286 | if ($bool) { |
||
287 | $this->db->write('INSERT IGNORE INTO location_is_hq (location_type_id) values (' . $this->db->escapeNumber($this->getTypeID()) . ')'); |
||
288 | } else { |
||
289 | $this->db->write('DELETE FROM location_is_hq WHERE ' . $this->SQL); |
||
290 | } |
||
291 | $this->HQ = $bool; |
||
292 | } |
||
293 | |||
294 | public function isUG(): bool { |
||
295 | if (!isset($this->UG)) { |
||
296 | $dbResult = $this->db->read('SELECT 1 FROM location_is_ug WHERE ' . $this->SQL); |
||
297 | $this->UG = $dbResult->hasRecord(); |
||
298 | } |
||
299 | return $this->UG; |
||
300 | } |
||
301 | |||
302 | public function setUG(bool $bool): void { |
||
303 | if ($this->UG === $bool) { |
||
304 | return; |
||
305 | } |
||
306 | if ($bool) { |
||
307 | $this->db->insert('location_is_ug', [ |
||
308 | 'location_type_id' => $this->db->escapeNumber($this->getTypeID()), |
||
309 | ]); |
||
310 | } else { |
||
311 | $this->db->write('DELETE FROM location_is_ug WHERE ' . $this->SQL); |
||
312 | } |
||
313 | $this->UG = $bool; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * @return array<int, \Smr\HardwareType> |
||
318 | */ |
||
319 | public function getHardwareSold(): array { |
||
320 | if (!isset($this->hardwareSold)) { |
||
321 | $this->hardwareSold = []; |
||
322 | $dbResult = $this->db->read('SELECT hardware_type_id FROM location_sells_hardware WHERE ' . $this->SQL); |
||
323 | foreach ($dbResult->records() as $dbRecord) { |
||
324 | $hardwareTypeID = $dbRecord->getInt('hardware_type_id'); |
||
325 | $this->hardwareSold[$hardwareTypeID] = HardwareType::get($hardwareTypeID); |
||
326 | } |
||
327 | } |
||
328 | return $this->hardwareSold; |
||
329 | } |
||
330 | |||
331 | public function isHardwareSold(int $hardwareTypeID = null): bool { |
||
332 | $hardware = $this->getHardwareSold(); |
||
333 | if ($hardwareTypeID === null) { |
||
334 | return count($hardware) != 0; |
||
335 | } |
||
336 | return isset($hardware[$hardwareTypeID]); |
||
337 | } |
||
338 | |||
339 | public function addHardwareSold(int $hardwareTypeID): void { |
||
340 | if ($this->isHardwareSold($hardwareTypeID)) { |
||
341 | return; |
||
342 | } |
||
343 | $dbResult = $this->db->read('SELECT 1 FROM hardware_type WHERE hardware_type_id = ' . $this->db->escapeNumber($hardwareTypeID)); |
||
344 | if (!$dbResult->hasRecord()) { |
||
345 | throw new Exception('Invalid hardware type id given'); |
||
346 | } |
||
347 | $this->db->insert('location_sells_hardware', [ |
||
348 | 'location_type_id' => $this->db->escapeNumber($this->getTypeID()), |
||
349 | 'hardware_type_id' => $this->db->escapeNumber($hardwareTypeID), |
||
350 | ]); |
||
351 | $this->hardwareSold[$hardwareTypeID] = HardwareType::get($hardwareTypeID); |
||
352 | } |
||
353 | |||
354 | public function removeHardwareSold(int $hardwareTypeID): void { |
||
355 | if (!$this->isHardwareSold($hardwareTypeID)) { |
||
356 | return; |
||
357 | } |
||
358 | $this->db->write('DELETE FROM location_sells_hardware WHERE ' . $this->SQL . ' AND hardware_type_id = ' . $this->db->escapeNumber($hardwareTypeID)); |
||
359 | unset($this->hardwareSold[$hardwareTypeID]); |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * @return array<int, SmrShipType> |
||
364 | */ |
||
365 | public function getShipsSold(): array { |
||
366 | if (!isset($this->shipsSold)) { |
||
367 | $this->shipsSold = []; |
||
368 | $dbResult = $this->db->read('SELECT * FROM location_sells_ships JOIN ship_type USING (ship_type_id) WHERE ' . $this->SQL); |
||
369 | foreach ($dbResult->records() as $dbRecord) { |
||
370 | $shipTypeID = $dbRecord->getInt('ship_type_id'); |
||
371 | $this->shipsSold[$shipTypeID] = SmrShipType::get($shipTypeID, $dbRecord); |
||
372 | } |
||
373 | |||
374 | if ($this->gameID > 0 && SmrGame::getGame($this->gameID)->isGameType(SmrGame::GAME_TYPE_HUNTER_WARS)) { |
||
375 | // Remove ships that are not allowed in Hunter Wars |
||
376 | unset($this->shipsSold[SHIP_TYPE_PLANETARY_SUPER_FREIGHTER]); |
||
377 | foreach ($this->shipsSold as $shipID => $ship) { |
||
378 | if ($ship->getClass() === ShipClass::Raider) { |
||
379 | unset($this->shipsSold[$shipID]); |
||
380 | } |
||
381 | } |
||
382 | } |
||
383 | } |
||
384 | return $this->shipsSold; |
||
385 | } |
||
386 | |||
387 | public function isShipSold(int $shipTypeID = null): bool { |
||
388 | $ships = $this->getShipsSold(); |
||
389 | if ($shipTypeID === null) { |
||
390 | return count($ships) != 0; |
||
391 | } |
||
392 | return isset($ships[$shipTypeID]); |
||
393 | } |
||
394 | |||
395 | public function addShipSold(int $shipTypeID): void { |
||
396 | if ($this->isShipSold($shipTypeID)) { |
||
397 | return; |
||
398 | } |
||
399 | $ship = SmrShipType::get($shipTypeID); |
||
400 | $this->db->insert('location_sells_ships', [ |
||
401 | 'location_type_id' => $this->db->escapeNumber($this->getTypeID()), |
||
402 | 'ship_type_id' => $this->db->escapeNumber($shipTypeID), |
||
403 | ]); |
||
404 | $this->shipsSold[$shipTypeID] = $ship; |
||
405 | } |
||
406 | |||
407 | public function removeShipSold(int $shipTypeID): void { |
||
408 | if (!$this->isShipSold($shipTypeID)) { |
||
409 | return; |
||
410 | } |
||
411 | $this->db->write('DELETE FROM location_sells_ships WHERE ' . $this->SQL . ' AND ship_type_id = ' . $this->db->escapeNumber($shipTypeID)); |
||
412 | unset($this->shipsSold[$shipTypeID]); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * @return array<int, SmrWeapon> |
||
417 | */ |
||
418 | public function getWeaponsSold(): array { |
||
419 | if (!isset($this->weaponsSold)) { |
||
420 | $this->weaponsSold = []; |
||
421 | $dbResult = $this->db->read('SELECT * FROM location_sells_weapons JOIN weapon_type USING (weapon_type_id) WHERE ' . $this->SQL); |
||
422 | foreach ($dbResult->records() as $dbRecord) { |
||
423 | $weaponTypeID = $dbRecord->getInt('weapon_type_id'); |
||
424 | $this->weaponsSold[$weaponTypeID] = SmrWeapon::getWeapon($weaponTypeID, $dbRecord); |
||
425 | } |
||
426 | } |
||
427 | return $this->weaponsSold; |
||
428 | } |
||
429 | |||
430 | public function isWeaponSold(int $weaponTypeID = null): bool { |
||
436 | } |
||
437 | |||
438 | public function addWeaponSold(int $weaponTypeID): void { |
||
448 | } |
||
449 | |||
450 | public function removeWeaponSold(int $weaponTypeID): void { |
||
451 | if (!$this->isWeaponSold($weaponTypeID)) { |
||
452 | return; |
||
453 | } |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * @return array<SmrLocation> |
||
460 | */ |
||
461 | public function getLinkedLocations(): array { |
||
475 | } |
||
476 | |||
477 | public function getExamineHREF(): string { |
||
478 | $action = $this->processor; |
||
479 | $container = match ($action) { |
||
480 | 'shop_hardware.php' => new ShopHardware($this->typeID), |
||
481 | 'shop_ship.php' => new ShopShip($this->typeID), |
||
482 | 'shop_weapon.php' => new ShopWeapon($this->typeID), |
||
483 | 'government.php' => new Government($this->typeID), |
||
484 | 'underground.php' => new Underground($this->typeID), |
||
485 | 'bank_personal.php' => new PersonalBank(), |
||
486 | 'bar_main.php' => new BarMain($this->typeID), |
||
487 | default => throw new Exception('Unknown action: ' . $action), |
||
488 | }; |
||
489 | return $container->href(); |
||
490 | } |
||
491 | |||
492 | public function getEditHREF(): string { |
||
493 | $container = new EditLocations($this->getTypeID()); |
||
494 | return $container->href(); |
||
495 | } |
||
496 | |||
497 | public function equals(SmrLocation $otherLocation): bool { |
||
499 | } |
||
500 | |||
501 | public function hasX(mixed $x, AbstractSmrPlayer $player = null): bool { |
||
541 | } |
||
542 | |||
543 | } |
||
544 |