Complex classes like Fleet 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Fleet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Fleet extends UnitContainer { |
||
| 32 | |||
| 33 | |||
| 34 | // DBRow inheritance ************************************************************************************************* |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Table name in DB |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected static $_table = 'fleets'; |
||
| 42 | /** |
||
| 43 | * Name of ID field in DB |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected static $_dbIdFieldName = 'fleet_id'; |
||
| 48 | /** |
||
| 49 | * DB_ROW to Class translation scheme |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected static $_properties = array( |
||
| 54 | 'dbId' => array( |
||
| 55 | P_DB_FIELD => 'fleet_id', |
||
| 56 | ), |
||
| 57 | 'playerOwnerId' => array( |
||
| 58 | P_DB_FIELD => 'fleet_owner', |
||
| 59 | ), |
||
| 60 | 'mission_type' => array( |
||
| 61 | P_DB_FIELD => 'fleet_mission', |
||
| 62 | P_FUNC_INPUT => 'intval', |
||
| 63 | ), |
||
| 64 | |||
| 65 | 'target_owner_id' => array( |
||
| 66 | P_DB_FIELD => 'fleet_target_owner', |
||
| 67 | ), |
||
| 68 | 'group_id' => array( |
||
| 69 | P_DB_FIELD => 'fleet_group', |
||
| 70 | ), |
||
| 71 | 'is_returning' => array( |
||
| 72 | P_DB_FIELD => 'fleet_mess', |
||
| 73 | P_FUNC_INPUT => 'intval', |
||
| 74 | ), |
||
| 75 | |||
| 76 | 'shipCount' => array( |
||
| 77 | P_DB_FIELD => 'fleet_amount', |
||
| 78 | // TODO - CHECK !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
||
|
|
|||
| 79 | // P_FUNC_OUTPUT => 'get_ship_count', |
||
| 80 | // P_DB_FIELDS_LINKED => array( |
||
| 81 | // 'fleet_amount', |
||
| 82 | // ), |
||
| 83 | P_READ_ONLY => true, |
||
| 84 | ), |
||
| 85 | |||
| 86 | 'time_launch' => array( |
||
| 87 | P_DB_FIELD => 'start_time', |
||
| 88 | ), |
||
| 89 | |||
| 90 | |||
| 91 | 'time_arrive_to_target' => array( |
||
| 92 | P_DB_FIELD => 'fleet_start_time', |
||
| 93 | ), |
||
| 94 | 'time_mission_job_complete' => array( |
||
| 95 | P_DB_FIELD => 'fleet_end_stay', |
||
| 96 | ), |
||
| 97 | 'time_return_to_source' => array( |
||
| 98 | P_DB_FIELD => 'fleet_end_time', |
||
| 99 | ), |
||
| 100 | |||
| 101 | 'fleet_start_planet_id' => array( |
||
| 102 | P_DB_FIELD => 'fleet_start_planet_id', |
||
| 103 | P_FUNC_INPUT => 'nullIfEmpty', |
||
| 104 | ), |
||
| 105 | |||
| 106 | |||
| 107 | 'fleet_start_galaxy' => array( |
||
| 108 | P_DB_FIELD => 'fleet_start_galaxy', |
||
| 109 | ), |
||
| 110 | 'fleet_start_system' => array( |
||
| 111 | P_DB_FIELD => 'fleet_start_system', |
||
| 112 | ), |
||
| 113 | 'fleet_start_planet' => array( |
||
| 114 | P_DB_FIELD => 'fleet_start_planet', |
||
| 115 | ), |
||
| 116 | 'fleet_start_type' => array( |
||
| 117 | P_DB_FIELD => 'fleet_start_type', |
||
| 118 | ), |
||
| 119 | |||
| 120 | 'fleet_end_planet_id' => array( |
||
| 121 | P_DB_FIELD => 'fleet_end_planet_id', |
||
| 122 | P_FUNC_INPUT => 'nullIfEmpty', |
||
| 123 | ), |
||
| 124 | 'fleet_end_galaxy' => array( |
||
| 125 | P_DB_FIELD => 'fleet_end_galaxy', |
||
| 126 | ), |
||
| 127 | 'fleet_end_system' => array( |
||
| 128 | P_DB_FIELD => 'fleet_end_system', |
||
| 129 | ), |
||
| 130 | 'fleet_end_planet' => array( |
||
| 131 | P_DB_FIELD => 'fleet_end_planet', |
||
| 132 | ), |
||
| 133 | 'fleet_end_type' => array( |
||
| 134 | P_DB_FIELD => 'fleet_end_type', |
||
| 135 | ), |
||
| 136 | |||
| 137 | |||
| 138 | 'resource_list' => array( |
||
| 139 | P_METHOD_EXTRACT => 'resourcesExtract', |
||
| 140 | P_METHOD_INJECT => 'resourcesInject', |
||
| 141 | P_DB_FIELDS_LINKED => array( |
||
| 142 | 'fleet_resource_metal', |
||
| 143 | 'fleet_resource_crystal', |
||
| 144 | 'fleet_resource_deuterium', |
||
| 145 | ), |
||
| 146 | ), |
||
| 147 | ); |
||
| 148 | |||
| 149 | |||
| 150 | // UnitContainer inheritance ***************************************************************************************** |
||
| 151 | /** |
||
| 152 | * Type of this location |
||
| 153 | * |
||
| 154 | * @var int $locationType |
||
| 155 | */ |
||
| 156 | protected static $locationType = LOC_FLEET; |
||
| 157 | |||
| 158 | |||
| 159 | // New properties **************************************************************************************************** |
||
| 160 | /** |
||
| 161 | * `fleet_owner` |
||
| 162 | * |
||
| 163 | * @var int |
||
| 164 | */ |
||
| 165 | protected $_playerOwnerId = 0; |
||
| 166 | /** |
||
| 167 | * `fleet_group` |
||
| 168 | * |
||
| 169 | * @var int |
||
| 170 | */ |
||
| 171 | protected $_group_id = 0; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * `fleet_mission` |
||
| 175 | * |
||
| 176 | * @var int |
||
| 177 | */ |
||
| 178 | protected $_mission_type = 0; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * `fleet_target_owner` |
||
| 182 | * |
||
| 183 | * @var int |
||
| 184 | */ |
||
| 185 | protected $_target_owner_id = null; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @var array |
||
| 189 | */ |
||
| 190 | protected $resource_list = array( |
||
| 191 | RES_METAL => 0, |
||
| 192 | RES_CRYSTAL => 0, |
||
| 193 | RES_DEUTERIUM => 0, |
||
| 194 | ); |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * `fleet__mess` - Флаг возвращающегося флота |
||
| 199 | * |
||
| 200 | * @var int |
||
| 201 | */ |
||
| 202 | protected $_is_returning = 0; |
||
| 203 | /** |
||
| 204 | * `start_time` - Время отправления - таймштамп взлёта флота из точки отправления |
||
| 205 | * |
||
| 206 | * @var int $_time_launch |
||
| 207 | */ |
||
| 208 | protected $_time_launch = 0; // `start_time` = SN_TIME_NOW |
||
| 209 | /** |
||
| 210 | * `fleet_start_time` - Время прибытия в точку миссии/время начала выполнения миссии |
||
| 211 | * |
||
| 212 | * @var int $_time_arrive_to_target |
||
| 213 | */ |
||
| 214 | protected $_time_arrive_to_target = 0; // `fleet_start_time` = SN_TIME_NOW + $time_travel |
||
| 215 | /** |
||
| 216 | * `fleet_end_stay` - Время окончания миссии в точке назначения |
||
| 217 | * |
||
| 218 | * @var int $_time_mission_job_complete |
||
| 219 | */ |
||
| 220 | protected $_time_mission_job_complete = 0; // `fleet_end_stay` |
||
| 221 | /** |
||
| 222 | * `fleet_end_time` - Время возвращения флота после окончания миссии |
||
| 223 | * |
||
| 224 | * @var int $_time_return_to_source |
||
| 225 | */ |
||
| 226 | protected $_time_return_to_source = 0; // `fleet_end_time` |
||
| 227 | |||
| 228 | |||
| 229 | protected $_fleet_start_planet_id = null; |
||
| 230 | protected $_fleet_start_galaxy = 0; |
||
| 231 | protected $_fleet_start_system = 0; |
||
| 232 | protected $_fleet_start_planet = 0; |
||
| 233 | protected $_fleet_start_type = PT_ALL; |
||
| 234 | |||
| 235 | protected $_fleet_end_planet_id = null; |
||
| 236 | protected $_fleet_end_galaxy = 0; |
||
| 237 | protected $_fleet_end_system = 0; |
||
| 238 | protected $_fleet_end_planet = 0; |
||
| 239 | protected $_fleet_end_type = PT_ALL; |
||
| 240 | |||
| 241 | // Missile properties |
||
| 242 | public $missile_target = 0; |
||
| 243 | |||
| 244 | // Fleet event properties |
||
| 245 | public $fleet_start_name = ''; |
||
| 246 | public $fleet_end_name = ''; |
||
| 247 | public $ov_label = ''; |
||
| 248 | public $ov_this_planet = ''; |
||
| 249 | public $event_time = 0; |
||
| 250 | |||
| 251 | protected $resource_delta = array(); |
||
| 252 | protected $resource_replace = array(); |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * Returns location's player owner ID |
||
| 257 | * |
||
| 258 | * @return int |
||
| 259 | */ |
||
| 260 | // TODO - REMOVE! TEMPORARY UNTIL THERE BE FULLLY FUNCTIONAL Player CLASS AND FLEETS WOULD BE LOCATED ON PLANET OR PLAYER!!!!! |
||
| 261 | public function getPlayerOwnerId() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Fleet constructor. |
||
| 267 | */ |
||
| 268 | public function __construct() { |
||
| 271 | |||
| 272 | public function isEmpty() { |
||
| 276 | |||
| 277 | // public function getPlayerOwnerId() { |
||
| 278 | // return $this->playerOwnerId; |
||
| 279 | // } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Initializes Fleet from user params and posts it to DB |
||
| 283 | */ |
||
| 284 | public function dbInsert() { |
||
| 293 | |||
| 294 | |||
| 295 | /* FLEET DB ACCESS =================================================================================================*/ |
||
| 296 | |||
| 297 | /** |
||
| 298 | * LOCK - Lock all records which can be used with mission |
||
| 299 | * |
||
| 300 | * @param $mission_data |
||
| 301 | * @param $fleet_id |
||
| 302 | * |
||
| 303 | * @return array|bool|mysqli_result|null |
||
| 304 | */ |
||
| 305 | public function dbLockFlying(&$mission_data) { |
||
| 344 | |||
| 345 | |||
| 346 | /* FLEET HELPERS =====================================================================================================*/ |
||
| 347 | /** |
||
| 348 | * Forcibly returns fleet before time outs |
||
| 349 | */ |
||
| 350 | public function commandReturn() { |
||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | public function target_coordinates_without_type() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | public function target_coordinates_typed() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public function launch_coordinates_typed() { |
||
| 411 | |||
| 412 | |||
| 413 | /** |
||
| 414 | * Restores fleet or resources to planet |
||
| 415 | * |
||
| 416 | * @param bool $start |
||
| 417 | * @param bool $only_resources |
||
| 418 | * @param int $result |
||
| 419 | * |
||
| 420 | * @return int |
||
| 421 | */ |
||
| 422 | // TODO - split to functions |
||
| 423 | public function RestoreFleetToPlanet($start = true, $only_resources = false, &$result = CACHE_NOTHING) { |
||
| 493 | |||
| 494 | |||
| 495 | /** |
||
| 496 | * Sets object fields for fleet return |
||
| 497 | */ |
||
| 498 | public function markReturned() { |
||
| 502 | |||
| 503 | public function isReturning() { |
||
| 506 | |||
| 507 | public function markReturnedAndSave() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Parses extended unit_array which can include not only ships but resources, captains etc |
||
| 514 | * |
||
| 515 | * @param $unit_array |
||
| 516 | */ |
||
| 517 | // TODO - separate shipList and unitList |
||
| 518 | public function unitsSetFromArray($unit_array) { |
||
| 534 | |||
| 535 | |||
| 536 | /** |
||
| 537 | * Sets fleet timers based on flight duration, time on mission (HOLD/EXPLORE) and fleet departure time. |
||
| 538 | * |
||
| 539 | * @param int $time_to_travel - flight duration in seconds |
||
| 540 | * @param int $time_on_mission - time on mission in seconds |
||
| 541 | * @param int $group_sync_delta_time - delta time to adjust fleet arrival time if fleet is a part of group (i.e. ACS) |
||
| 542 | * @param int $flight_departure - fleet departure from source planet timestamp. Allows to send fleet in future or in past |
||
| 543 | */ |
||
| 544 | public function set_times($time_to_travel, $time_on_mission = 0, $group_sync_delta_time = 0, $flight_departure = SN_TIME_NOW) { |
||
| 551 | |||
| 552 | |||
| 553 | public function parse_missile_db_row($missile_db_row) { |
||
| 592 | |||
| 593 | |||
| 594 | /** |
||
| 595 | * @param $from |
||
| 596 | */ |
||
| 597 | public function set_start_planet($from) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @param $to |
||
| 607 | */ |
||
| 608 | public function set_end_planet($to) { |
||
| 616 | |||
| 617 | |||
| 618 | |||
| 619 | // UnitList/Ships access *************************************************************************************************** |
||
| 620 | |||
| 621 | // TODO - перекрывать пожже - для миссайл-флотов и дефенс-флотов |
||
| 622 | protected function isShip($unit_id) { |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Set unit count of $unit_id to $unit_count |
||
| 628 | * If there is no $unit_id - it will be created and saved to DB on dbSave |
||
| 629 | * |
||
| 630 | * @param int $unit_id |
||
| 631 | * @param int $unit_count |
||
| 632 | */ |
||
| 633 | public function shipSetCount($unit_id, $unit_count = 0) { |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Adjust unit count of $unit_id by $unit_count - or just replace value |
||
| 639 | * If there is no $unit_id - it will be created and saved to DB on dbSave |
||
| 640 | * |
||
| 641 | * @param int $unit_id |
||
| 642 | * @param int $unit_count |
||
| 643 | * @param bool $replace_value |
||
| 644 | */ |
||
| 645 | public function shipAdjustCount($unit_id, $unit_count = 0, $replace_value = false) { |
||
| 648 | |||
| 649 | public function shipGetCount($unit_id) { |
||
| 652 | |||
| 653 | public function shipsCountApplyLossMultiplier($ships_lost_multiplier) { |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Returns ship list in fleet |
||
| 659 | */ |
||
| 660 | public function shipsGetArray() { |
||
| 663 | |||
| 664 | public function shipsGetTotal() { |
||
| 667 | |||
| 668 | public function shipsGetCapacity() { |
||
| 671 | |||
| 672 | public function shipsGetHoldFree() { |
||
| 675 | |||
| 676 | public function shipsGetTotalById($ship_id) { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Возвращает ёмкость переработчиков во флоте |
||
| 682 | * |
||
| 683 | * @param array $recycler_info |
||
| 684 | * |
||
| 685 | * @return int |
||
| 686 | * |
||
| 687 | * @version 41a6.30 |
||
| 688 | */ |
||
| 689 | public function shipsGetCapacityRecyclers(array $recycler_info) { |
||
| 698 | |||
| 699 | // Resources access *************************************************************************************************** |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Extracts resources value from db_row |
||
| 703 | * |
||
| 704 | * @param array $db_row |
||
| 705 | * |
||
| 706 | * @internal param Fleet $that |
||
| 707 | * @version 41a6.30 |
||
| 708 | */ |
||
| 709 | protected function resourcesExtract(array &$db_row) { |
||
| 716 | |||
| 717 | protected function resourcesInject(array &$db_row) { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Set current resource list from array of units |
||
| 725 | * |
||
| 726 | * @param array $resource_list |
||
| 727 | */ |
||
| 728 | public function resourcesSet($resource_list) { |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Updates fleet resource list with deltas |
||
| 737 | * |
||
| 738 | * @param $resource_delta_list |
||
| 739 | */ |
||
| 740 | public function resourcesAdjust($resource_delta_list, $replace_value = false) { |
||
| 767 | |||
| 768 | public function resourcesGetTotal() { |
||
| 771 | |||
| 772 | /** |
||
| 773 | * @param array $rate |
||
| 774 | * |
||
| 775 | * @return float |
||
| 776 | */ |
||
| 777 | public function resourcesGetTotalInMetal(array $rate) { |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Returns resource list in fleet |
||
| 786 | */ |
||
| 787 | // TODO |
||
| 788 | public function resourcesGetList() { |
||
| 791 | |||
| 792 | public function resourcesReset() { |
||
| 799 | |||
| 800 | protected function isResource($unit_id) { |
||
| 803 | |||
| 804 | } |
||
| 805 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.