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 |
||
14 | class DBStaticUser { |
||
15 | // public static $_table = 'users'; |
||
|
|||
16 | // public static $_idField = 'id'; |
||
17 | |||
18 | // TODO - это вообще-то надо хранить в конфигурации |
||
19 | /** |
||
20 | * @return string |
||
21 | */ |
||
22 | View Code Duplication | public static function getLastRegisteredUserName() { |
|
44 | |||
45 | /** |
||
46 | * @return DbResultIterator |
||
47 | */ |
||
48 | View Code Duplication | public static function db_player_list_export_blitz_info() { |
|
68 | |||
69 | /** |
||
70 | * @return DbResultIterator |
||
71 | */ |
||
72 | View Code Duplication | public static function db_user_list_non_bots() { |
|
86 | |||
87 | public static function db_user_lock_with_target_owner_and_acs($user, $planet = array()) { |
||
97 | |||
98 | /** |
||
99 | * @param bool $online |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | public static function db_user_count($online = false) { |
||
112 | |||
113 | public static function db_user_list_admin_sorted($sort, $online = false) { |
||
140 | |||
141 | public static function db_user_list_to_celebrate($config_user_birthday_range) { |
||
159 | |||
160 | /** |
||
161 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
162 | */ |
||
163 | public static function db_user_list_admin_multiaccounts() { |
||
172 | |||
173 | public static function db_player_list_blitz_delete_players() { |
||
182 | |||
183 | /** |
||
184 | * @deprecated - NEVER change DM amount directly w/o logging! |
||
185 | */ |
||
186 | public static function db_player_list_blitz_set_50k_dm() { |
||
196 | |||
197 | |||
198 | /** |
||
199 | * Выбирает записи игроков по списку их ID |
||
200 | * |
||
201 | * @param $user_id_list |
||
202 | * |
||
203 | * @return array |
||
204 | */ |
||
205 | public static function db_user_list_by_id($user_id_list) { |
||
216 | |||
217 | |||
218 | public static function db_user_by_username($username_unsafe, $for_update = false, $fields = '*', $player = null, $like = false) { |
||
255 | |||
256 | public static function db_user_list($user_filter = '', $for_update = false, $fields = '*') { |
||
259 | |||
260 | /** |
||
261 | * @param $user_id |
||
262 | * @param array $set |
||
263 | * |
||
264 | * @return array|bool|mysqli_result|null |
||
265 | */ |
||
266 | public static function db_user_set_by_id($user_id, $set) { |
||
269 | |||
270 | /** |
||
271 | * @param $user_id |
||
272 | * @param array $set |
||
273 | * @param array $adjust |
||
274 | * |
||
275 | * @return array|bool|mysqli_result|null |
||
276 | */ |
||
277 | public static function db_user_adjust_by_id($user_id, $adjust) { |
||
280 | |||
281 | /** |
||
282 | * Возвращает информацию о пользователе по его ID |
||
283 | * |
||
284 | * @param int|array $user_id_unsafe |
||
285 | * <p>int - ID пользователя</p> |
||
286 | * <p>array - запись пользователя с установленным полем ['id']</p> |
||
287 | * @param bool $for_update @deprecated |
||
288 | * @param string $fields @deprecated список полей или '*'/'' для всех полей |
||
289 | * @param null $player |
||
290 | * @param bool|null $player Признак выбора записи пользователь типа "игрок" |
||
291 | * <p>null - Можно выбрать запись любого типа</p> |
||
292 | * <p>true - Выбирается только запись типа "игрок"</p> |
||
293 | * <p>false - Выбирается только запись типа "альянс"</p> |
||
294 | * |
||
295 | * @return array|false |
||
296 | * <p>false - Нет записи с указанным ID и $player</p> |
||
297 | * <p>array - запись типа $user</p> |
||
298 | */ |
||
299 | public static function db_user_by_id($user_id_unsafe, $for_update = false, $fields = '*', $player = null) { |
||
311 | |||
312 | |||
313 | /** |
||
314 | * @param $ally_id |
||
315 | * @param $ally_rank_id |
||
316 | * @param array $set |
||
317 | * @param array $adjust |
||
318 | */ |
||
319 | public static function db_user_list_set_by_ally_and_rank($ally_id, $ally_rank_id, $set, $adjust) { |
||
333 | |||
334 | /** |
||
335 | * @param array $playerArray |
||
336 | */ |
||
337 | public static function renderNameAndCoordinates($playerArray) { |
||
340 | |||
341 | /** |
||
342 | * @param mixed $user |
||
343 | */ |
||
344 | public static function validateUserRecord($user) { |
||
359 | |||
360 | /** |
||
361 | * @param array $playerRowFieldChanges - array of $resourceId => $amount |
||
362 | * @param int $userId |
||
363 | * |
||
364 | * // TODO - DEDUPLICATE |
||
365 | * @see DBStaticPlanet::db_planet_update_resources |
||
366 | */ |
||
367 | View Code Duplication | public static function db_user_update_resources($playerRowFieldChanges, $userId) { |
|
383 | |||
384 | public static function lockAllRecords() { |
||
387 | |||
388 | } |
||
389 |
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.