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) { |
||
| 71 | return intval(static::getDb()->doSelectFetchValue( |
||
| 72 | "SELECT COUNT(`id`) AS `user_count` |
||
| 73 | FROM `{{users}}` |
||
| 74 | WHERE |
||
| 75 | `user_as_ally` IS NULL" . |
||
| 76 | ($online ? ' AND `onlinetime` > ' . (SN_TIME_NOW - classSupernova::$config->game_users_online_timeout) : '') |
||
| 77 | )); |
||
| 78 | } |
||
| 79 | |||
| 80 | public static function db_user_list_admin_sorted($sort, $online = false) { |
||
| 81 | // $query = "SELECT |
||
| 82 | // u.*, COUNT(r.id) AS referral_count, SUM(r.dark_matter) AS referral_dm |
||
| 83 | // FROM |
||
| 84 | // {{users}} as u |
||
| 85 | // LEFT JOIN |
||
| 86 | // {{referrals}} as r on r.id_partner = u.id |
||
| 87 | // WHERE " . |
||
| 88 | // ($online ? "`onlinetime` >= :onlineTime" : 'user_as_ally IS NULL') . |
||
| 89 | // " GROUP BY u.id |
||
| 90 | // ORDER BY user_as_ally, {$sort} ASC"; |
||
| 91 | |||
| 92 | $query = static::buildDBQ() |
||
| 93 | ->setAlias('u') |
||
| 94 | ->field('u.*') |
||
| 95 | ->fieldCount('r.id', 'referral_count') |
||
| 96 | ->fieldSingleFunction('sum', 'r.dark_matter', 'referral_dm') |
||
| 97 | ->join('LEFT JOIN {{referrals}} as r on r.id_partner = u.id') |
||
| 98 | ->where($online ? "`onlinetime` >= " . intval(SN_TIME_NOW - classSupernova::$config->game_users_online_timeout) : 'user_as_ally IS NULL') |
||
| 99 | ->groupBy('u.id') |
||
| 100 | ->orderBy("user_as_ally, {$sort} ASC"); |
||
| 101 | |||
| 102 | $result = $query->selectIterator(); |
||
| 103 | |||
| 104 | return $result; |
||
| 105 | } |
||
| 106 | |||
| 107 | public static function db_user_list_to_celebrate($config_user_birthday_range) { |
||
| 108 | $query = static::buildDBQ() |
||
| 109 | ->field('id', 'username', 'user_birthday', 'user_birthday_celebrated') |
||
| 110 | ->fieldLiteral('CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, \'-%m-%d\')) AS `current_birthday`') |
||
| 111 | ->fieldLiteral('DATEDIFF(CURRENT_DATE, CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, \'-%m-%d\'))) AS `days_after_birthday`') |
||
| 112 | ->where('`user_birthday` IS NOT NULL') |
||
| 113 | ->where('(`user_birthday_celebrated` IS NULL OR DATE_ADD(`user_birthday_celebrated`, INTERVAL 1 YEAR) < CURRENT_DATE)') |
||
| 114 | ->where('`user_as_ally` IS NULL') |
||
| 115 | ->having('`days_after_birthday` >= 0') |
||
| 116 | ->having('`days_after_birthday` < ' . intval($config_user_birthday_range)) |
||
| 117 | ->setForUpdate(); |
||
| 118 | |||
| 119 | $result = $query->selectIterator(); |
||
| 120 | // |
||
| 121 | // $query = "SELECT |
||
| 122 | // `id`, `username`, `user_birthday`, `user_birthday_celebrated`, |
||
| 123 | // CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, '-%m-%d')) AS `current_birthday`, |
||
| 124 | // DATEDIFF(CURRENT_DATE, CONCAT(YEAR(CURRENT_DATE), DATE_FORMAT(`user_birthday`, '-%m-%d'))) AS `days_after_birthday` |
||
| 125 | // FROM |
||
| 126 | // `{{users}}` |
||
| 127 | // WHERE |
||
| 128 | // `user_birthday` IS NOT NULL |
||
| 129 | // AND `user_as_ally` IS NULL |
||
| 130 | // AND (`user_birthday_celebrated` IS NULL OR DATE_ADD(`user_birthday_celebrated`, INTERVAL 1 YEAR) < CURRENT_DATE) |
||
| 131 | // HAVING |
||
| 132 | // `days_after_birthday` >= 0 AND `days_after_birthday` < {$config_user_birthday_range} FOR UPDATE"; |
||
| 133 | // |
||
| 134 | // $result = static::$dbStatic->doQueryIterator($query); |
||
| 135 | |||
| 136 | return $result; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 141 | */ |
||
| 142 | public static function db_user_list_admin_multiaccounts() { |
||
| 143 | $query = "SELECT COUNT(*) AS `ip_count`, `user_lastip` |
||
| 144 | FROM `{{users}}` |
||
| 145 | WHERE `user_as_ally` IS NULL |
||
| 146 | GROUP BY `user_lastip` |
||
| 147 | HAVING COUNT(*) > 1"; |
||
| 148 | |||
| 149 | return static::getDb()->doSelectIterator($query); |
||
| 150 | } |
||
| 151 | |||
| 152 | public static function db_player_list_blitz_delete_players() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @deprecated - NEVER change DM amount directly w/o logging! |
||
| 158 | */ |
||
| 159 | public static function db_player_list_blitz_set_50k_dm() { |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * Выбирает записи игроков по списку их ID |
||
| 173 | * |
||
| 174 | * @param $user_id_list |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public static function db_user_list_by_id($user_id_list) { |
||
| 189 | |||
| 190 | |||
| 191 | public static function db_user_by_username($username_unsafe, $for_update = false, $fields = '*', $player = null, $like = false) { |
||
| 228 | |||
| 229 | public static function db_user_list($user_filter = '', $for_update = false, $fields = '*') { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param $user_id |
||
| 235 | * @param string $set |
||
| 236 | * |
||
| 237 | * @return array|bool|mysqli_result|null |
||
| 238 | * @deprecated |
||
| 239 | */ |
||
| 240 | public static function db_user_set_by_id_DEPRECATED($user_id, $set) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Возвращает информацию о пользователе по его ID |
||
| 246 | * |
||
| 247 | * @param int|array $user_id_unsafe |
||
| 248 | * <p>int - ID пользователя</p> |
||
| 249 | * <p>array - запись пользователя с установленным полем ['id']</p> |
||
| 250 | * @param bool $for_update @deprecated |
||
| 251 | * @param string $fields @deprecated список полей или '*'/'' для всех полей |
||
| 252 | * @param null $player |
||
| 253 | * @param bool|null $player Признак выбора записи пользователь типа "игрок" |
||
| 254 | * <p>null - Можно выбрать запись любого типа</p> |
||
| 255 | * <p>true - Выбирается только запись типа "игрок"</p> |
||
| 256 | * <p>false - Выбирается только запись типа "альянс"</p> |
||
| 257 | * |
||
| 258 | * @return array|false |
||
| 259 | * <p>false - Нет записи с указанным ID и $player</p> |
||
| 260 | * <p>array - запись типа $user</p> |
||
| 261 | */ |
||
| 262 | public static function db_user_by_id($user_id_unsafe, $for_update = false, $fields = '*', $player = null) { |
||
| 274 | |||
| 275 | |||
| 276 | public static function db_user_list_set_mass_mail(&$owners_list, $set) { |
||
| 283 | |||
| 284 | public static function db_user_list_set_by_ally_and_rank($ally_id, $ally_rank_id, $set) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param $ally_id |
||
| 294 | * @param $i |
||
| 295 | * @param $rank_id |
||
| 296 | * |
||
| 297 | * @return array|bool|mysqli_result|null |
||
| 298 | * @deprecated |
||
| 299 | */ |
||
| 300 | public static function db_user_list_set_ally_deprecated_convert_ranks($ally_id, $i, $rank_id) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param array $playerArray |
||
| 316 | */ |
||
| 317 | public static function renderNameAndCoordinates($playerArray) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param mixed $user |
||
| 323 | */ |
||
| 324 | public static function validateUserRecord($user) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param array $playerRowFieldChanges - array of $resourceId => $amount |
||
| 342 | * @param int $userId |
||
| 343 | * |
||
| 344 | * @see DBStaticPlanet::db_planet_update_resources |
||
| 345 | * // TODO - DEDUPLICATE |
||
| 346 | */ |
||
| 347 | View Code Duplication | public static function db_user_update_resources($playerRowFieldChanges, $userId) { |
|
| 363 | |||
| 364 | } |
||
| 365 |
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: