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 Account 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 Account, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class Account { |
||
7 | /** |
||
8 | * @var int |
||
9 | */ |
||
10 | public $account_id = 0; |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | public $account_name = ''; |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | public $account_password = ''; |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | public $account_salt = ''; |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | public $account_email = ''; |
||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | public $account_email_verified = 0; |
||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | public $account_register_time = ''; |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | public $account_language = ''; |
||
39 | |||
40 | public $account_metamatter = 0; |
||
41 | public $account_metamatter_total = 0; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | public $is_exists = 0; |
||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | public $is_loaded = 0; |
||
51 | |||
52 | /** |
||
53 | * @var db_mysql |
||
54 | */ |
||
55 | public $db; |
||
56 | |||
57 | protected $table_check = array( |
||
58 | 'account' => 'account', |
||
59 | 'log_metamatter' => 'log_metamatter', |
||
60 | ); |
||
61 | |||
62 | public function reset() { |
||
75 | |||
76 | public function __construct($db = null) { |
||
88 | |||
89 | // OK 4.5 |
||
90 | public function password_check($password_unsafe) { |
||
93 | |||
94 | /** |
||
95 | * Меняет пароль у аккаунта в БД |
||
96 | * |
||
97 | * @param $old_password_unsafe |
||
98 | * @param $new_password_unsafe |
||
99 | * @param null $salt_unsafe |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | // OK v4.6 |
||
104 | public function password_change($old_password_unsafe, $new_password_unsafe, $salt_unsafe = null) { |
||
114 | |||
115 | |||
116 | /** |
||
117 | * Заполняет поля объекта значениями результата запроса |
||
118 | * |
||
119 | * @param array $row |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | // OK v4.5 |
||
124 | public function assign_from_db_row($row) { |
||
146 | |||
147 | /** |
||
148 | * Возвращает аккаунт по его ID |
||
149 | * |
||
150 | * @param $account_id_unsafe |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | // OK v4.5 |
||
155 | public function db_get_by_id($account_id_unsafe) { |
||
164 | /** |
||
165 | * Возвращает аккаунт по имени |
||
166 | * |
||
167 | * @param string $account_name_safe |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | // OK v4.5 |
||
172 | public function db_get_by_name($account_name_unsafe) { |
||
181 | /** |
||
182 | * Возвращает аккаунт по емейлу |
||
183 | * |
||
184 | * @param string $email_unsafe |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | // OK v4.5 |
||
189 | public function db_get_by_email($email_unsafe) { |
||
198 | /** |
||
199 | * Возвращает аккаунт по имени или аккаунту - проверка уникальных значений |
||
200 | * |
||
201 | * @param string $account_name_unsafe |
||
202 | * @param string $email_unsafe |
||
203 | * |
||
204 | * @return bool |
||
205 | * |
||
206 | */ |
||
207 | // OK v4.5 |
||
208 | public function db_get_by_name_or_email($account_name_unsafe, $email_unsafe) { |
||
228 | /** |
||
229 | * Создает аккаунт |
||
230 | * |
||
231 | * @throws Exception |
||
232 | */ |
||
233 | // OK v4.5 |
||
234 | public function db_create($account_name_unsafe, $password_raw, $email_unsafe, $language_unsafe = null, $salt_unsafe = null) { |
||
257 | |||
258 | /** |
||
259 | * Физически меняет пароль аккаунта в БД |
||
260 | * |
||
261 | * @param string $password_unsafe |
||
262 | * @param string $salt_unsafe |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | // OK v4.5 |
||
267 | public function db_set_password($password_unsafe, $salt_unsafe) { |
||
287 | |||
288 | |||
289 | |||
290 | /** |
||
291 | * Просаливает пароль |
||
292 | * |
||
293 | * @param $password |
||
294 | * @param $salt |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | // OK v4.5 |
||
299 | protected function password_encode($password, $salt) { |
||
302 | /** |
||
303 | * Генерирует соль |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | // OK v4.5 |
||
308 | protected function password_salt_generate() { |
||
311 | |||
312 | /** |
||
313 | * Вставляет запись об изменении количества ММ в лог ММ |
||
314 | * |
||
315 | * @param $comment |
||
316 | * @param $change_type |
||
317 | * @param $metamatter |
||
318 | * |
||
319 | * @return int|string |
||
320 | */ |
||
321 | // OK 4.8 |
||
322 | protected function db_mm_log_insert($comment, $change_type, $metamatter, $user_id_unsafe) { |
||
341 | |||
342 | /** |
||
343 | * @param int $change_type |
||
344 | * @param int $metamatter |
||
345 | * @param string $comment |
||
346 | * @param bool $already_changed |
||
347 | * |
||
348 | * @return array|bool|int|mysqli_result|null|string |
||
349 | */ |
||
350 | public function metamatter_change($change_type, $metamatter, $comment = '', $already_changed = false) { |
||
425 | |||
426 | |||
427 | // ------ UNUSED ----------------------------------------------------------------------------------------------------- |
||
428 | |||
429 | } |
||
430 |
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.