We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 45 |
Total Lines | 227 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
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); |
||
3 | class SmrShip extends AbstractSmrShip { |
||
4 | protected static $CACHE_SHIPS = array(); |
||
5 | |||
6 | protected $db; |
||
7 | protected $SQL; |
||
8 | |||
9 | protected $isCloaked; |
||
10 | |||
11 | public static function refreshCache() { |
||
12 | foreach (self::$CACHE_SHIPS as &$gameShips) { |
||
13 | foreach ($gameShips as &$ship) { |
||
14 | $ship = self::getShip($ship->getPlayer(), true); |
||
15 | } |
||
16 | } |
||
17 | } |
||
18 | |||
19 | public static function clearCache() { |
||
20 | self::$CACHE_SHIPS = array(); |
||
21 | } |
||
22 | |||
23 | public static function saveShips() { |
||
24 | foreach (self::$CACHE_SHIPS as $gameShips) { |
||
25 | foreach ($gameShips as $ship) { |
||
26 | $ship->update(); |
||
27 | } |
||
28 | } |
||
29 | } |
||
30 | |||
31 | public static function getShip(AbstractSmrPlayer $player, $forceUpdate = false) { |
||
37 | } |
||
38 | |||
39 | protected function __construct(AbstractSmrPlayer $player) { |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Initialize the weapons onboard this ship. |
||
51 | */ |
||
52 | protected function loadWeapons() { |
||
53 | // determine weapon |
||
54 | $this->db->query('SELECT * FROM ship_has_weapon JOIN weapon_type USING (weapon_type_id) |
||
55 | WHERE ' . $this->SQL . ' |
||
56 | ORDER BY order_id LIMIT ' . $this->db->escapeNumber($this->getHardpoints())); |
||
57 | |||
58 | $this->weapons = array(); |
||
59 | // generate list of weapon names the user transports |
||
60 | while ($this->db->nextRecord()) { |
||
61 | $weaponTypeID = $this->db->getInt('weapon_type_id'); |
||
62 | $orderID = $this->db->getInt('order_id'); |
||
63 | $weapon = SmrWeapon::getWeapon($weaponTypeID, $this->db); |
||
64 | $weapon->setBonusAccuracy($this->db->getBoolean('bonus_accuracy')); |
||
65 | $weapon->setBonusDamage($this->db->getBoolean('bonus_damage')); |
||
66 | $this->weapons[$orderID] = $weapon; |
||
67 | } |
||
68 | $this->checkForExcessWeapons(); |
||
69 | } |
||
70 | |||
71 | protected function loadCargo() { |
||
72 | if (!isset($this->cargo)) { |
||
73 | // initialize cargo array |
||
74 | $this->cargo = array(); |
||
75 | |||
76 | // get cargo from db |
||
77 | $this->db->query('SELECT * FROM ship_has_cargo WHERE ' . $this->SQL); |
||
78 | while ($this->db->nextRecord()) { |
||
79 | // adding cargo and amount to array |
||
80 | $this->cargo[$this->db->getInt('good_id')] = $this->db->getInt('amount'); |
||
81 | } |
||
82 | } |
||
83 | $this->checkForExcessCargo(); |
||
84 | } |
||
85 | |||
86 | protected function loadHardware() { |
||
87 | $this->hardware = array(); |
||
88 | $this->oldHardware = array(); |
||
89 | |||
90 | // get currently hardware from db |
||
91 | $this->db->query('SELECT * |
||
92 | FROM ship_has_hardware |
||
93 | JOIN hardware_type USING(hardware_type_id) |
||
94 | WHERE ' . $this->SQL); |
||
95 | |||
96 | while ($this->db->nextRecord()) { |
||
97 | $hardwareTypeID = $this->db->getInt('hardware_type_id'); |
||
98 | |||
99 | // adding hardware to array |
||
100 | $this->hardware[$hardwareTypeID] = $this->db->getInt('amount'); |
||
101 | $this->oldHardware[$hardwareTypeID] = $this->db->getInt('old_amount'); |
||
102 | } |
||
103 | $this->checkForExcessHardware(); |
||
104 | } |
||
105 | |||
106 | public function getAccountID() { |
||
107 | return $this->getPlayer()->getAccountID(); |
||
108 | } |
||
109 | |||
110 | public function updateCargo() { |
||
111 | if ($this->hasChangedCargo === true) { |
||
112 | // write cargo info |
||
113 | foreach ($this->getCargo() as $id => $amount) { |
||
114 | if ($amount > 0) { |
||
115 | $this->db->query('REPLACE INTO ship_has_cargo (account_id, game_id, good_id, amount) VALUES(' . $this->db->escapeNumber($this->getAccountID()) . ', ' . $this->db->escapeNumber($this->getGameID()) . ', ' . $this->db->escapeNumber($id) . ', ' . $this->db->escapeNumber($amount) . ')'); |
||
116 | } else { |
||
117 | $this->db->query('DELETE FROM ship_has_cargo WHERE ' . $this->SQL . ' AND good_id = ' . $this->db->escapeNumber($id) . ' LIMIT 1'); |
||
118 | // Unset now to omit displaying this good with 0 amount |
||
119 | // before the next page is loaded. |
||
120 | unset($this->cargo[$id]); |
||
121 | } |
||
122 | } |
||
123 | $this->hasChangedCargo = false; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | public function updateHardware() { |
||
141 | } |
||
142 | |||
143 | private function updateWeapon() { |
||
144 | if ($this->hasChangedWeapons === true) { |
||
145 | // write weapon info |
||
146 | $this->db->query('DELETE FROM ship_has_weapon WHERE ' . $this->SQL); |
||
147 | foreach ($this->weapons as $orderID => $weapon) { |
||
148 | $this->db->query('INSERT INTO ship_has_weapon (account_id, game_id, order_id, weapon_type_id, bonus_accuracy, bonus_damage) |
||
149 | VALUES(' . $this->db->escapeNumber($this->getAccountID()) . ', ' . $this->db->escapeNumber($this->getGameID()) . ', ' . $this->db->escapeNumber($orderID) . ', ' . $this->db->escapeNumber($weapon->getWeaponTypeID()) . ', ' . $this->db->escapeBoolean($weapon->hasBonusAccuracy()) . ', ' . $this->db->escapeBoolean($weapon->hasBonusDamage()) . ')'); |
||
150 | } |
||
151 | $this->hasChangedWeapons = false; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | public function update() { |
||
156 | $this->updateHardware(); |
||
157 | $this->updateWeapon(); |
||
158 | $this->updateCargo(); |
||
159 | // note: SmrShip::setShipTypeID updates the SmrPlayer only |
||
160 | $this->getPlayer()->update(); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * given power level of new weapon, return whether there is enough power available to install it on this ship |
||
165 | */ |
||
166 | public function checkPowerAvailable($powerLevel) { |
||
167 | return $this->getRemainingPower() >= $powerLevel; |
||
168 | } |
||
169 | |||
170 | public function isCloaked() { |
||
171 | if (!$this->hasCloak()) { |
||
172 | return false; |
||
173 | } |
||
174 | if (!isset($this->isCloaked)) { |
||
175 | $this->db->query('SELECT 1 FROM ship_is_cloaked WHERE ' . $this->SQL . ' LIMIT 1'); |
||
176 | $this->isCloaked = $this->db->getNumRows() > 0; |
||
177 | } |
||
178 | return $this->isCloaked; |
||
179 | } |
||
180 | |||
181 | public function decloak() { |
||
182 | $this->isCloaked = false; |
||
183 | $this->db->query('DELETE FROM ship_is_cloaked WHERE ' . $this->SQL . ' LIMIT 1'); |
||
184 | } |
||
185 | |||
186 | public function enableCloak() { |
||
187 | $this->isCloaked = true; |
||
188 | $this->db->query('REPLACE INTO ship_is_cloaked VALUES(' . $this->db->escapeNumber($this->getAccountID()) . ', ' . $this->db->escapeNumber($this->getGameID()) . ')'); |
||
189 | } |
||
190 | |||
191 | public function cloakOverload() { |
||
192 | // 1 in 25 chance of cloak being destroyed if active |
||
193 | if ($this->isCloaked() && mt_rand(0, 99) < 5) { |
||
194 | $this->db->query('DELETE FROM ship_has_hardware |
||
195 | WHERE ' . $this->SQL . ' |
||
196 | AND hardware_type_id = 8 |
||
197 | LIMIT 1'); |
||
198 | $this->decloak(); |
||
199 | $this->setHardware(HARDWARE_CLOAK, 0); |
||
200 | return true; |
||
201 | } |
||
202 | |||
203 | return false; |
||
204 | } |
||
205 | |||
206 | public function setIllusion($shipID, $attack, $defense) { |
||
207 | $this->db->query('REPLACE INTO ship_has_illusion VALUES(' . $this->db->escapeNumber($this->getAccountID()) . ', ' . $this->db->escapeNumber($this->getGameID()) . ', ' . $this->db->escapeNumber($shipID) . ', ' . $this->db->escapeNumber($attack) . ', ' . $this->db->escapeNumber($defense) . ')'); |
||
208 | } |
||
209 | |||
210 | public function disableIllusion() { |
||
212 | } |
||
213 | |||
214 | public function getIllusionShip() { |
||
215 | if (!isset($this->illusionShip)) { |
||
216 | $this->illusionShip = false; |
||
217 | $this->db->query('SELECT ship_has_illusion.*,ship_type.ship_name |
||
218 | FROM ship_has_illusion |
||
230 | |||
231 | } |
||
232 | |||
233 | } |
||
234 |