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 DBStaticUser 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 DBStaticUser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class DBStaticUser extends DBStaticRecord { |
||
| 7 | |||
| 8 | public static $_table = 'users'; |
||
| 9 | public static $_idField = 'id'; |
||
| 10 | |||
| 11 | protected static function whereNotAlly() { |
||
| 14 | |||
| 15 | // TODO - это вообще-то надо хранить в конфигурации |
||
| 16 | /** |
||
| 17 | * @return string |
||
| 18 | */ |
||
| 19 | public static function getLastRegisteredUserName() { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return DbResultIterator |
||
| 31 | */ |
||
| 32 | public static function db_player_list_export_blitz_info() { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return DbResultIterator |
||
| 43 | */ |
||
| 44 | public static function db_user_list_non_bots() { |
||
| 56 | |||
| 57 | public static function db_user_lock_with_target_owner_and_acs($user, $planet = array()) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param bool $online |
||
| 67 | * |
||
| 68 | * @return int |
||
| 69 | */ |
||
| 70 | public static function db_user_count($online = false) { |
||
| 79 | |||
| 80 | public static function db_user_list_admin_sorted($sort, $online = false) { |
||
| 106 | |||
| 107 | public static function db_user_list_to_celebrate($config_user_birthday_range) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 141 | */ |
||
| 142 | public static function db_user_list_admin_multiaccounts() { |
||
| 151 | |||
| 152 | public static function db_player_list_blitz_delete_players() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @deprecated - NEVER change DM amount directly w/o logging! |
||
| 164 | */ |
||
| 165 | public static function db_player_list_blitz_set_50k_dm() { |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * Выбирает записи игроков по списку их ID |
||
| 178 | * |
||
| 179 | * @param $user_id_list |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public static function db_user_list_by_id($user_id_list) { |
||
| 194 | |||
| 195 | |||
| 196 | public static function db_user_by_username($username_unsafe, $for_update = false, $fields = '*', $player = null, $like = false) { |
||
| 233 | |||
| 234 | public static function db_user_list($user_filter = '', $for_update = false, $fields = '*') { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param $user_id |
||
| 240 | * @param array $set |
||
| 241 | * |
||
| 242 | * @return array|bool|mysqli_result|null |
||
| 243 | */ |
||
| 244 | public static function db_user_set_by_id($user_id, $set) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param $user_id |
||
| 250 | * @param array $set |
||
| 251 | * @param array $adjust |
||
| 252 | * |
||
| 253 | * @return array|bool|mysqli_result|null |
||
| 254 | */ |
||
| 255 | public static function db_user_adjust_by_id($user_id, $adjust) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Возвращает информацию о пользователе по его ID |
||
| 261 | * |
||
| 262 | * @param int|array $user_id_unsafe |
||
| 263 | * <p>int - ID пользователя</p> |
||
| 264 | * <p>array - запись пользователя с установленным полем ['id']</p> |
||
| 265 | * @param bool $for_update @deprecated |
||
| 266 | * @param string $fields @deprecated список полей или '*'/'' для всех полей |
||
| 267 | * @param null $player |
||
| 268 | * @param bool|null $player Признак выбора записи пользователь типа "игрок" |
||
| 269 | * <p>null - Можно выбрать запись любого типа</p> |
||
| 270 | * <p>true - Выбирается только запись типа "игрок"</p> |
||
| 271 | * <p>false - Выбирается только запись типа "альянс"</p> |
||
| 272 | * |
||
| 273 | * @return array|false |
||
| 274 | * <p>false - Нет записи с указанным ID и $player</p> |
||
| 275 | * <p>array - запись типа $user</p> |
||
| 276 | */ |
||
| 277 | public static function db_user_by_id($user_id_unsafe, $for_update = false, $fields = '*', $player = null) { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @param $ally_id |
||
| 293 | * @param $ally_rank_id |
||
| 294 | * @param array $set |
||
| 295 | * @param array $adjust |
||
| 296 | */ |
||
| 297 | public static function db_user_list_set_by_ally_and_rank($ally_id, $ally_rank_id, $set, $adjust) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param array $playerArray |
||
| 314 | */ |
||
| 315 | public static function renderNameAndCoordinates($playerArray) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param mixed $user |
||
| 321 | */ |
||
| 322 | public static function validateUserRecord($user) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param array $playerRowFieldChanges - array of $resourceId => $amount |
||
| 340 | * @param int $userId |
||
| 341 | * |
||
| 342 | * @see DBStaticPlanet::db_planet_update_resources |
||
| 343 | * // TODO - DEDUPLICATE |
||
| 344 | */ |
||
| 345 | View Code Duplication | public static function db_user_update_resources($playerRowFieldChanges, $userId) { |
|
| 361 | |||
| 362 | } |
||
| 363 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: