We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 40 |
| Total Lines | 213 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
Complex classes like SmrShip 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 SmrShip, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 7 | class SmrShip extends AbstractSmrShip { |
||
| 8 | protected static array $CACHE_SHIPS = []; |
||
| 9 | |||
| 10 | protected string $SQL; |
||
| 11 | |||
| 12 | public static function refreshCache() : void { |
||
| 13 | foreach (self::$CACHE_SHIPS as &$gameShips) { |
||
| 14 | foreach ($gameShips as &$ship) { |
||
| 15 | $ship = self::getShip($ship->getPlayer(), true); |
||
| 16 | } |
||
| 17 | } |
||
| 18 | } |
||
| 19 | |||
| 20 | public static function clearCache() : void { |
||
| 21 | self::$CACHE_SHIPS = array(); |
||
| 22 | } |
||
| 23 | |||
| 24 | public static function saveShips() : void { |
||
| 25 | foreach (self::$CACHE_SHIPS as $gameShips) { |
||
| 26 | foreach ($gameShips as $ship) { |
||
| 27 | $ship->update(); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | public static function getShip(AbstractSmrPlayer $player, bool $forceUpdate = false) : self { |
||
| 33 | if ($forceUpdate || !isset(self::$CACHE_SHIPS[$player->getGameID()][$player->getAccountID()])) { |
||
| 34 | $s = new SmrShip($player); |
||
| 35 | self::$CACHE_SHIPS[$player->getGameID()][$player->getAccountID()] = $s; |
||
| 36 | } |
||
| 37 | return self::$CACHE_SHIPS[$player->getGameID()][$player->getAccountID()]; |
||
| 38 | } |
||
| 39 | |||
| 40 | protected function __construct(AbstractSmrPlayer $player) { |
||
| 41 | parent::__construct($player); |
||
| 42 | $db = MySqlDatabase::getInstance(); |
||
| 43 | $this->SQL = 'account_id=' . $db->escapeNumber($this->getAccountID()) . ' AND game_id=' . $db->escapeNumber($this->getGameID()); |
||
| 44 | |||
| 45 | $this->loadHardware(); |
||
| 46 | $this->loadWeapons(); |
||
| 47 | $this->loadCargo(); |
||
| 48 | $this->loadCloak(); |
||
| 49 | $this->loadIllusion(); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function update() : void { |
||
| 53 | $this->updateHardware(); |
||
| 54 | $this->updateWeapons(); |
||
| 55 | $this->updateCargo(); |
||
| 56 | $this->updateCloak(); |
||
| 57 | $this->updateIllusion(); |
||
| 58 | // note: SmrShip::setShipTypeID updates the SmrPlayer only |
||
| 59 | $this->getPlayer()->update(); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Initialize the weapons onboard this ship. |
||
| 64 | */ |
||
| 65 | protected function loadWeapons() : void { |
||
| 66 | // determine weapon |
||
| 67 | $db = MySqlDatabase::getInstance(); |
||
| 68 | $db->query('SELECT * FROM ship_has_weapon JOIN weapon_type USING (weapon_type_id) |
||
| 69 | WHERE ' . $this->SQL . ' |
||
| 70 | ORDER BY order_id LIMIT ' . $db->escapeNumber($this->getHardpoints())); |
||
| 71 | |||
| 72 | $this->weapons = array(); |
||
| 73 | // generate list of weapon names the user transports |
||
| 74 | while ($db->nextRecord()) { |
||
| 75 | $weaponTypeID = $db->getInt('weapon_type_id'); |
||
| 76 | $orderID = $db->getInt('order_id'); |
||
| 77 | $weapon = SmrWeapon::getWeapon($weaponTypeID, $db); |
||
| 78 | $weapon->setBonusAccuracy($db->getBoolean('bonus_accuracy')); |
||
| 79 | $weapon->setBonusDamage($db->getBoolean('bonus_damage')); |
||
| 80 | $this->weapons[$orderID] = $weapon; |
||
| 81 | } |
||
| 82 | $this->checkForExcessWeapons(); |
||
| 83 | } |
||
| 84 | |||
| 85 | protected function loadHardware() : void { |
||
| 86 | $this->hardware = array(); |
||
| 87 | $this->oldHardware = array(); |
||
| 88 | |||
| 89 | // get currently hardware from db |
||
| 90 | $db = MySqlDatabase::getInstance(); |
||
| 91 | $db->query('SELECT * |
||
| 92 | FROM ship_has_hardware |
||
| 93 | JOIN hardware_type USING(hardware_type_id) |
||
| 94 | WHERE ' . $this->SQL); |
||
| 95 | |||
| 96 | while ($db->nextRecord()) { |
||
| 97 | $hardwareTypeID = $db->getInt('hardware_type_id'); |
||
| 98 | |||
| 99 | // adding hardware to array |
||
| 100 | $this->hardware[$hardwareTypeID] = $db->getInt('amount'); |
||
| 101 | $this->oldHardware[$hardwareTypeID] = $db->getInt('old_amount'); |
||
| 102 | } |
||
| 103 | $this->checkForExcessHardware(); |
||
| 104 | } |
||
| 105 | |||
| 106 | protected function loadCargo() : void { |
||
| 107 | // initialize cargo array |
||
| 108 | $this->cargo = array(); |
||
| 109 | |||
| 110 | // get cargo from db |
||
| 111 | $db = MySqlDatabase::getInstance(); |
||
| 112 | $db->query('SELECT * FROM ship_has_cargo WHERE ' . $this->SQL); |
||
| 113 | while ($db->nextRecord()) { |
||
| 114 | // adding cargo and amount to array |
||
| 115 | $this->cargo[$db->getInt('good_id')] = $db->getInt('amount'); |
||
| 116 | } |
||
| 117 | $this->checkForExcessCargo(); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function updateCargo() : void { |
||
| 121 | if ($this->hasChangedCargo === false) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | // write cargo info |
||
| 125 | $db = MySqlDatabase::getInstance(); |
||
| 126 | foreach ($this->getCargo() as $id => $amount) { |
||
| 127 | if ($amount > 0) { |
||
| 128 | $db->query('REPLACE INTO ship_has_cargo (account_id, game_id, good_id, amount) VALUES(' . $db->escapeNumber($this->getAccountID()) . ', ' . $db->escapeNumber($this->getGameID()) . ', ' . $db->escapeNumber($id) . ', ' . $db->escapeNumber($amount) . ')'); |
||
| 129 | } else { |
||
| 130 | $db->query('DELETE FROM ship_has_cargo WHERE ' . $this->SQL . ' AND good_id = ' . $db->escapeNumber($id) . ' LIMIT 1'); |
||
| 131 | // Unset now to omit displaying this good with 0 amount |
||
| 132 | // before the next page is loaded. |
||
| 133 | unset($this->cargo[$id]); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | $this->hasChangedCargo = false; |
||
| 137 | } |
||
| 138 | |||
| 139 | public function updateHardware() : void { |
||
| 154 | } |
||
| 155 | |||
| 156 | private function updateWeapons() : void { |
||
| 157 | if ($this->hasChangedWeapons === false) { |
||
| 158 | return; |
||
| 159 | } |
||
| 160 | // write weapon info |
||
| 161 | $db = MySqlDatabase::getInstance(); |
||
| 162 | $db->query('DELETE FROM ship_has_weapon WHERE ' . $this->SQL); |
||
| 163 | foreach ($this->weapons as $orderID => $weapon) { |
||
| 164 | $db->query('INSERT INTO ship_has_weapon (account_id, game_id, order_id, weapon_type_id, bonus_accuracy, bonus_damage) |
||
| 165 | VALUES(' . $db->escapeNumber($this->getAccountID()) . ', ' . $db->escapeNumber($this->getGameID()) . ', ' . $db->escapeNumber($orderID) . ', ' . $db->escapeNumber($weapon->getWeaponTypeID()) . ', ' . $db->escapeBoolean($weapon->hasBonusAccuracy()) . ', ' . $db->escapeBoolean($weapon->hasBonusDamage()) . ')'); |
||
| 166 | } |
||
| 167 | $this->hasChangedWeapons = false; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function loadCloak() : void { |
||
| 171 | $this->isCloaked = false; |
||
| 172 | if ($this->hasCloak() === false) { |
||
| 173 | return; |
||
| 174 | } |
||
| 175 | $db = MySqlDatabase::getInstance(); |
||
| 176 | $db->query('SELECT 1 FROM ship_is_cloaked WHERE ' . $this->SQL . ' LIMIT 1'); |
||
| 177 | $this->isCloaked = $db->getNumRows() > 0; |
||
| 178 | } |
||
| 179 | |||
| 180 | public function updateCloak() : void { |
||
| 191 | } |
||
| 192 | |||
| 193 | public function loadIllusion() : void { |
||
| 205 | ]; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | public function updateIllusion() : void { |
||
| 210 | if ($this->hasChangedIllusion === false) { |
||
| 211 | return; |
||
| 212 | } |
||
| 213 | $db = MySqlDatabase::getInstance(); |
||
| 220 | } |
||
| 221 | |||
| 222 | } |
||
| 223 |