Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DBStaticPlanet 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 DBStaticPlanet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class DBStaticPlanet { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @param array $rowUser |
||
| 9 | * @param array $rowPlanet |
||
| 10 | * |
||
| 11 | * @return array |
||
| 12 | */ |
||
| 13 | public static function getResources($rowUser, $rowPlanet) { |
||
| 14 | $planetResources = array(); |
||
| 15 | |||
| 16 | $sn_group_resources = sn_get_groups('resources_loot'); |
||
| 17 | foreach ($sn_group_resources as $resource_id) { |
||
| 18 | $planetResources[$resource_id] = floor(mrc_get_level($rowUser, $rowPlanet, $resource_id)); |
||
| 19 | } |
||
| 20 | |||
| 21 | return $planetResources; |
||
| 22 | } |
||
| 23 | |||
| 24 | |||
| 25 | public static function db_planets_purge() { |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * @param int $planet_id |
||
| 32 | * @param bool $for_update |
||
| 33 | * @param string $fields |
||
| 34 | * |
||
| 35 | * @return array|null |
||
| 36 | */ |
||
| 37 | public static function db_planet_by_id($planet_id, $for_update = false, $fields = '*') { |
||
| 42 | |||
| 43 | public static function db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update = false, $fields = '*') { |
||
| 44 | return classSupernova::$gc->cacheOperator->db_get_record_list(LOC_PLANET, |
||
| 45 | "`{{planets}}`.`galaxy` = {$galaxy} AND `{{planets}}`.`system` = {$system} AND `{{planets}}`.`planet` = {$planet} AND `{{planets}}`.`planet_type` = {$planet_type}", true); |
||
| 46 | } |
||
| 47 | |||
| 48 | public static function db_planet_by_gspt($galaxy, $system, $planet, $planet_type, $for_update = false, $fields = '*') { |
||
| 49 | $galaxy = intval($galaxy); |
||
| 50 | $system = intval($system); |
||
| 51 | $planet = intval($planet); |
||
| 52 | $planet_type = intval($planet_type); |
||
| 53 | |||
| 54 | return DBStaticPlanet::db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update, $fields); |
||
| 55 | } |
||
| 56 | |||
| 57 | public static function db_planet_by_vector($vector, $prefix = '', $for_update = false, $fields = '*') { |
||
| 58 | $galaxy = isset($vector[$prefix . 'galaxy']) ? intval($vector[$prefix . 'galaxy']) : 0; |
||
| 59 | $system = isset($vector[$prefix . 'system']) ? intval($vector[$prefix . 'system']) : 0; |
||
| 60 | $planet = isset($vector[$prefix . 'planet']) ? intval($vector[$prefix . 'planet']) : 0; |
||
| 61 | $planet_type = isset($vector[$prefix . 'planet_type']) ? intval($vector[$prefix . 'planet_type']) : |
||
| 62 | (isset($vector[$prefix . 'type']) ? intval($vector[$prefix . 'type']) : 0); |
||
| 63 | $planet_type = $planet_type == PT_DEBRIS ? PT_PLANET : $planet_type; |
||
| 64 | |||
| 65 | return DBStaticPlanet::db_planet_by_gspt_safe($galaxy, $system, $planet, $planet_type, $for_update, $fields); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param Vector $vector |
||
| 70 | * @param bool $for_update |
||
| 71 | * @param string $fields |
||
| 72 | * |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | public static function db_planet_by_vector_object($vector, $for_update = false, $fields = '*') { |
||
| 81 | |||
| 82 | public static function db_planet_by_parent($parent_id, $for_update = false, $fields = '*') { |
||
| 83 | if (!($parent_id = idval($parent_id))) { |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | return classSupernova::$gc->cacheOperator->db_get_record_list(LOC_PLANET, |
||
| 88 | "`parent_planet` = {$parent_id} AND `planet_type` = " . PT_MOON, true); |
||
| 89 | } |
||
| 90 | |||
| 91 | View Code Duplication | public static function db_planet_by_id_and_owner($planet_id, $owner_id, $for_update = false, $fields = '*') { |
|
| 100 | |||
| 101 | |||
| 102 | View Code Duplication | public static function db_planet_list_moon_other($user_id, $this_moon_id) { |
|
| 111 | |||
| 112 | public static function db_planet_list_in_system($galaxy, $system) { |
||
| 113 | $galaxy = intval($galaxy); |
||
| 114 | $system = intval($system); |
||
| 115 | |||
| 116 | return classSupernova::$gc->cacheOperator->db_get_record_list(LOC_PLANET, |
||
| 117 | "`galaxy` = {$galaxy} AND `system` = {$system}"); |
||
| 118 | } |
||
| 119 | |||
| 120 | public static function db_planet_list_sorted($user_row, $skip_planet_id = false, $field_list = '', $conditions = '') { |
||
| 121 | if (!is_array($user_row)) { |
||
| 122 | return false; |
||
| 123 | } |
||
| 124 | // $field_list = $field_list != '*' ? "{{planets}}.`id`, `name`, `image`, {{planets}}.`galaxy`, {{planets}}.`system`, {{planets}}.`planet`, `planet_type`{$field_list}" : $field_list; |
||
| 125 | $conditions .= $skip_planet_id ? " AND `id` <> {$skip_planet_id} " : ''; |
||
| 126 | |||
| 127 | $sort_orders = array( |
||
| 128 | SORT_ID => '{{planets}}.`id`', |
||
| 129 | SORT_LOCATION => '{{planets}}.`galaxy`, {{planets}}.`system`, {{planets}}.`planet`, {{planets}}.`planet_type`', |
||
| 130 | SORT_NAME => '`name`', |
||
| 131 | SORT_SIZE => '({{planets}}.`field_max`)', |
||
| 132 | ); |
||
| 133 | $order_by = classSupernova::$user_options[PLAYER_OPTION_PLANET_SORT]; |
||
| 134 | empty($sort_orders[$order_by]) ? $order_by = SORT_ID : false; |
||
| 135 | $order_by = $sort_orders[$order_by] . ' ' . (classSupernova::$user_options[PLAYER_OPTION_PLANET_SORT_INVERSE] == SORT_ASCENDING ? 'ASC' : 'DESC'); |
||
| 136 | |||
| 137 | // Compilating query |
||
| 138 | return classSupernova::$gc->cacheOperator->db_get_record_list(LOC_PLANET, |
||
| 139 | "`id_owner` = '{$user_row['id']}' {$conditions} ORDER BY {$order_by}"); |
||
| 140 | } |
||
| 141 | |||
| 142 | View Code Duplication | public static function db_planet_list_by_user_or_planet($user_id, $planet_id) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param array $planetRowFieldChanges - array of $resourceId => $amount |
||
| 153 | * @param int $planetId |
||
| 154 | * |
||
| 155 | * @see DBStaticUser::db_user_update_resources |
||
| 156 | * // TODO - DEDUPLICATE |
||
| 157 | */ |
||
| 158 | View Code Duplication | public static function db_planet_update_resources($planetRowFieldChanges, $planetId) { |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param $planet_id |
||
| 177 | * @param string $set |
||
| 178 | * |
||
| 179 | * @return array|bool|mysqli_result|null |
||
| 180 | * @deprecated |
||
| 181 | */ |
||
| 182 | public static function db_planet_update_set_by_id_DEPRECATED($planet_id, $set) { |
||
| 189 | |||
| 190 | public static function db_planet_update_by_gspt($ui_galaxy, $ui_system, $ui_planet, $ui_planet_type = PT_ALL, $set, $adjust) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param int $ui_parent_id |
||
| 213 | * @param array $set |
||
| 214 | */ |
||
| 215 | View Code Duplication | public static function db_planet_set_by_parent($ui_parent_id, $set) { |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param $ui_owner_id |
||
| 232 | * @param array $set |
||
| 233 | */ |
||
| 234 | View Code Duplication | public static function db_planet_set_by_owner($ui_owner_id, $set) { |
|
| 248 | |||
| 249 | |||
| 250 | View Code Duplication | public static function db_planet_delete_by_id($planet_id) { |
|
| 264 | |||
| 265 | View Code Duplication | public static function db_planet_list_delete_by_owner($ui_owner_id) { |
|
| 279 | |||
| 280 | |||
| 281 | View Code Duplication | public static function db_planet_count_by_type($ui_user_id, $ui_planet_type = PT_PLANET) { |
|
| 293 | |||
| 294 | public static function db_planet_list_resources_by_owner() { |
||
| 297 | |||
| 298 | } |
||
| 299 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: