| Total Complexity | 42 |
| Total Lines | 234 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like FleetDispatchEvent 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 FleetDispatchEvent, and based on these observations, apply Extract Interface, too.
| 1 | <?php /** @noinspection PhpUnnecessaryCurlyVarSyntaxInspection, PhpCastIsUnnecessaryInspection, PhpDeprecationInspection */ |
||
| 27 | class FleetDispatchEvent { |
||
| 28 | const IS_ATTACK = 'isAttack'; |
||
| 29 | const IS_TRANSPORT = 'isTransport'; |
||
| 30 | |||
| 31 | const F_FLEET_ID = 'fleet_id'; |
||
| 32 | const F_FLEET_OWNER_ID = 'fleet_owner'; |
||
| 33 | const F_PLANET_ID = 'id'; |
||
| 34 | const F_PLANET_OWNER_ID = 'id_owner'; |
||
| 35 | const IS_LOCK_SOURCE = 'isLockSource'; |
||
| 36 | |||
| 37 | /** @var array[] $sn_groups_mission */ |
||
| 38 | public static $sn_groups_mission = []; |
||
| 39 | /** @var int[] $userIdsToLock */ |
||
| 40 | public $userIdsToLock = []; |
||
| 41 | /** @var int[] $planetIdsToLock */ |
||
| 42 | public $planetIdsToLock = []; |
||
| 43 | /** @var int[] $fleetIdsToLock */ |
||
| 44 | public $fleetIdsToLock = []; |
||
| 45 | /** @var int $processedIPR Processed IPR on this run */ |
||
| 46 | public static $processedIPR = -1; |
||
| 47 | |||
| 48 | public static $MISSIONS = [ |
||
| 49 | MT_ATTACK => [self::IS_TRANSPORT => false, self::IS_ATTACK => true,], |
||
| 50 | MT_AKS => [self::IS_TRANSPORT => false, self::IS_ATTACK => true,], |
||
| 51 | MT_DESTROY => [self::IS_TRANSPORT => false, self::IS_ATTACK => true,], |
||
| 52 | MT_HOLD => [self::IS_TRANSPORT => false,], |
||
| 53 | MT_SPY => [self::IS_TRANSPORT => false, self::IS_LOCK_SOURCE => true, 'AJAX' => true,], |
||
| 54 | MT_TRANSPORT => [self::IS_TRANSPORT => true, self::IS_LOCK_SOURCE => true,], |
||
| 55 | MT_RELOCATE => [self::IS_TRANSPORT => true, self::IS_LOCK_SOURCE => true,], |
||
| 56 | MT_RECYCLE => [self::IS_TRANSPORT => false, 'AJAX' => true,], |
||
| 57 | MT_EXPLORE => [self::IS_TRANSPORT => false,], |
||
| 58 | MT_COLONIZE => [self::IS_TRANSPORT => true,], |
||
| 59 | MT_MISSILE => [self::IS_TRANSPORT => false, 'AJAX' => true,], |
||
| 60 | ]; |
||
| 61 | |||
| 62 | public function __construct($fleetRow, $eventType) { |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * LOCK - Lock all records which can be used with mission |
||
| 148 | * |
||
| 149 | * @return array|bool|mysqli_result|null |
||
| 150 | */ |
||
| 151 | public function lockEventRecords() { |
||
| 152 | $locks = []; |
||
| 153 | |||
| 154 | if (!empty($this->userIdsToLock)) { |
||
| 155 | $locks['users'] = $this->userIdsToLock; |
||
| 156 | } |
||
| 157 | if (!empty($this->planetIdsToLock)) { |
||
| 158 | $locks['planets'] = $this->planetIdsToLock; |
||
| 159 | } |
||
| 160 | if (!empty($this->fleetIdsToLock)) { |
||
| 161 | $locks['fleets'] = $this->fleetIdsToLock; |
||
| 162 | } |
||
| 163 | return SN::$gc->db->lockRecords($locks); |
||
| 164 | } |
||
| 165 | |||
| 166 | public static function sortEvents(&$eventList) { |
||
| 167 | uasort($eventList, function (FleetDispatchEvent $a, FleetDispatchEvent $b) { |
||
| 168 | return |
||
| 169 | // Сравниваем время флотов - кто раньше, тот и первый обрабатывается |
||
| 170 | $a->eventTimeStamp > $b->eventTimeStamp ? 1 : ($a->eventTimeStamp < $b->eventTimeStamp ? -1 : |
||
| 171 | // Если время - одинаковое, сравниваем события флотов |
||
| 172 | // Если события - одинаковые, то флоты равны |
||
| 173 | ($a->event == $b->event ? 0 : |
||
| 174 | // Если события разные - первыми считаем прибывающие флоты |
||
| 175 | ($a->event == EVENT_FLT_ARRIVE ? 1 : ($b->event == EVENT_FLT_ARRIVE ? -1 : |
||
| 176 | // Если нет прибывающих флотов - дальше считаем флоты, которые закончили миссию |
||
| 177 | ($a->event == EVENT_FLT_ACCOMPLISH ? 1 : ($b->event == EVENT_FLT_ACCOMPLISH ? -1 : |
||
| 178 | // Если нет флотов, закончивших задание - остались возвращающиеся флоты, которые равны между собой |
||
| 179 | // TODO: Добавить еще проверку по ID флота и/или времени запуска - что бы обсчитывать их в порядке запуска |
||
| 180 | ( |
||
| 181 | 0 // Вообще сюда доходить не должно - будет отсекаться на равенстве событий |
||
| 182 | ) |
||
| 183 | )) |
||
| 184 | )) |
||
| 185 | ) |
||
| 186 | ); |
||
| 187 | }); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | */ |
||
| 192 | public function refreshMissionData() { |
||
| 193 | if (!empty($this->srcPlanetId) && !empty($this->fleetOwnerId)) { |
||
| 194 | // $this->srcPlanetRow = DBStaticPlanet::db_planet_by_vector($this->fleet, 'fleet_start_'); |
||
| 195 | $updateResult = sys_o_get_updated($this->fleetOwnerId, $this->srcPlanetId, $this->eventTimeStamp); |
||
| 196 | |||
| 197 | $this->updateSrcPlanetRow($updateResult['planet']); |
||
| 198 | } |
||
| 199 | |||
| 200 | if (!empty($this->dstPlanetId) && !empty($this->dstPlanetOwnerId)) { |
||
| 201 | $updateResult = sys_o_get_updated($this->dstPlanetOwnerId, $this->dstPlanetId, $this->eventTimeStamp); |
||
| 202 | |||
| 203 | $this->updateDstPlanetRow($updateResult['planet']); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return array|false |
||
| 209 | */ |
||
| 210 | public function refreshFleet() { |
||
| 211 | return $this->fleet = DbFleetStatic::db_fleet_get($this->fleetId); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function getSrcPlanetRowFromFleet() { |
||
| 215 | $this->srcPlanetRow = DBStaticPlanet::db_planet_by_vector($this->fleet, 'fleet_start_'); |
||
| 216 | |||
| 217 | $this->updateSrcPlanetRow($this->srcPlanetRow); |
||
| 218 | |||
| 219 | return $this->srcPlanetRow; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param array $srcPlanetRow |
||
| 224 | * |
||
| 225 | * @return array|bool |
||
| 226 | */ |
||
| 227 | public function updateSrcPlanetRow($srcPlanetRow) { |
||
| 228 | // $this->srcPlanetRow = is_array($srcPlanetRow) ? $srcPlanetRow : DBStaticPlanet::db_planet_by_vector($this->fleet, 'fleet_start_'); |
||
| 229 | $this->srcPlanetRow = $srcPlanetRow; |
||
| 230 | |||
| 231 | $this->srcPlanetId = !empty($this->srcPlanetRow[self::F_PLANET_ID]) ? (int)$this->srcPlanetRow[self::F_PLANET_ID] : 0; |
||
| 232 | // Starting planet can change owner while fleet mission - and even change planet ID |
||
| 233 | // It can happen due to teleport shenanigans or because of planet capturing (in certain game modes) |
||
| 234 | $this->fleetOwnerId = !empty($this->srcPlanetRow[self::F_PLANET_OWNER_ID]) ? (int)$this->srcPlanetRow[self::F_PLANET_OWNER_ID] : $this->fleetOwnerId; |
||
| 235 | |||
| 236 | return $this->srcPlanetRow; |
||
| 237 | } |
||
| 238 | |||
| 239 | |||
| 240 | public function getDstPlanetRowFromFleet() { |
||
| 241 | $this->dstPlanetRow = DBStaticPlanet::db_planet_by_vector($this->fleet, 'fleet_end_'); |
||
| 242 | |||
| 243 | $this->updateDstPlanetRow($this->dstPlanetRow); |
||
| 244 | |||
| 245 | return $this->dstPlanetRow; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param ?array $dstPlanetRow |
||
| 250 | * |
||
| 251 | * @return array|bool|null |
||
| 252 | */ |
||
| 253 | public function updateDstPlanetRow($dstPlanetRow = null) { |
||
| 261 | } |
||
| 262 | |||
| 263 | } |
||
| 264 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..