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 | View Code Duplication | 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) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Физически меняет пароль аккаунта в БД |
||
| 266 | * |
||
| 267 | * @param string $password_unsafe |
||
| 268 | * @param string $salt_unsafe |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | // OK v4.5 |
||
| 273 | public function db_set_password($password_unsafe, $salt_unsafe) { |
||
| 293 | |||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * Просаливает пароль |
||
| 298 | * |
||
| 299 | * @param $password |
||
| 300 | * @param $salt |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | // OK v4.5 |
||
| 305 | protected function password_encode($password, $salt) { |
||
| 308 | /** |
||
| 309 | * Генерирует соль |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | // OK v4.5 |
||
| 314 | protected function password_salt_generate() { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Вставляет запись об изменении количества ММ в лог ММ |
||
| 320 | * |
||
| 321 | * @param $comment |
||
| 322 | * @param $change_type |
||
| 323 | * @param $metamatter |
||
| 324 | * |
||
| 325 | * @return int|string |
||
| 326 | */ |
||
| 327 | // OK 4.8 |
||
| 328 | protected function db_mm_log_insert($comment, $change_type, $metamatter, $user_id_unsafe) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param int $change_type |
||
| 365 | * @param int $metamatter |
||
| 366 | * @param string $comment |
||
| 367 | * @param bool $already_changed |
||
| 368 | * |
||
| 369 | * @return array|bool|int|mysqli_result|null|string |
||
| 370 | */ |
||
| 371 | public function metamatter_change($change_type, $metamatter, $comment = '', $already_changed = false) { |
||
| 446 | |||
| 447 | |||
| 448 | // ------ UNUSED ----------------------------------------------------------------------------------------------------- |
||
| 449 | |||
| 450 | } |
||
| 451 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.