Complex classes like Management 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 Management, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | trait Management { |
||
| 33 | /** |
||
| 34 | * User id after registration |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | protected $reg_id = 0; |
||
| 38 | /** |
||
| 39 | * Search keyword in login, username and email |
||
| 40 | * |
||
| 41 | * @param string $search_phrase |
||
| 42 | * |
||
| 43 | * @return false|int[] |
||
| 44 | */ |
||
| 45 | 2 | function search_users ($search_phrase) { |
|
| 46 | 2 | $search_phrase = trim($search_phrase, "%\n"); |
|
| 47 | 2 | $found_users = $this->db()->qfas( |
|
| 48 | "SELECT `id` |
||
| 49 | FROM `[prefix]users` |
||
| 50 | WHERE |
||
| 51 | ( |
||
| 52 | `login` LIKE '%1\$s' OR |
||
| 53 | `username` LIKE '%1\$s' OR |
||
| 54 | `email` LIKE '%1\$s' |
||
| 55 | ) AND |
||
| 56 | 2 | `status` != '%2\$s'", |
|
| 57 | $search_phrase, |
||
| 58 | 2 | User::STATUS_NOT_ACTIVATED |
|
| 59 | ); |
||
| 60 | 2 | if (!$found_users) { |
|
| 61 | 2 | return false; |
|
| 62 | } |
||
| 63 | 2 | return _int($found_users); |
|
| 64 | } |
||
| 65 | /** |
||
| 66 | * User registration |
||
| 67 | * |
||
| 68 | * @param string $email |
||
| 69 | * @param bool $confirmation If <b>true</b> - default system option is used, if <b>false</b> - registration will be finished without necessity of |
||
| 70 | * confirmation, independently from default system option (is used for manual registration). |
||
| 71 | * @param bool $auto_sign_in If <b>false</b> - no auto sign in, if <b>true</b> - according to system configuration |
||
| 72 | * |
||
| 73 | * @return array|false|string <b>exists</b> - if user with such email is already registered<br> |
||
|
|
|||
| 74 | * <b>error</b> - if error occurred<br> |
||
| 75 | * <b>false</b> - if email is incorrect<br> |
||
| 76 | * <b>[<br> |
||
| 77 | * 'reg_key' => *,</b> //Registration confirmation key, or <b>true</b> if confirmation is not |
||
| 78 | * required<br> |
||
| 79 | * <b>'password' => *,</b> //Automatically generated password (empty if confirmation is needed and |
||
| 80 | * will be set during registration confirmation)<br> |
||
| 81 | * <b>'id' => *</b> //Id of registered user in DB<br> |
||
| 82 | * <b>]</b> |
||
| 83 | */ |
||
| 84 | 6 | function registration ($email, $confirmation = true, $auto_sign_in = true) { |
|
| 188 | /** |
||
| 189 | * Confirmation of registration process |
||
| 190 | * |
||
| 191 | * @param string $reg_key |
||
| 192 | * |
||
| 193 | * @return array|false ['id' => <i>id</i>, 'email' => <i>email</i>, 'password' => <i>password</i>] or <b>false</b> on failure |
||
| 194 | */ |
||
| 195 | 2 | function registration_confirmation ($reg_key) { |
|
| 196 | 2 | if (!is_md5($reg_key)) { |
|
| 197 | 2 | return false; |
|
| 198 | } |
||
| 199 | 2 | if (!Event::instance()->fire( |
|
| 200 | 2 | 'System/User/registration/confirmation/before', |
|
| 201 | [ |
||
| 202 | 2 | 'reg_key' => $reg_key |
|
| 203 | ] |
||
| 204 | ) |
||
| 205 | ) { |
||
| 206 | 2 | $this->registration_cancel(); |
|
| 207 | 2 | return false; |
|
| 208 | } |
||
| 209 | 2 | $this->delete_unconfirmed_users(); |
|
| 210 | 2 | $data = $this->db_prime()->qf( |
|
| 211 | "SELECT |
||
| 212 | `id`, |
||
| 213 | `login_hash`, |
||
| 214 | `email` |
||
| 215 | FROM `[prefix]users` |
||
| 216 | WHERE |
||
| 217 | `reg_key` = '%s' AND |
||
| 218 | `status` = '%s' |
||
| 219 | 2 | LIMIT 1", |
|
| 220 | $reg_key, |
||
| 221 | 2 | User::STATUS_NOT_ACTIVATED |
|
| 222 | ); |
||
| 223 | 2 | if (!$data) { |
|
| 224 | 2 | return false; |
|
| 225 | } |
||
| 226 | 2 | $this->reg_id = (int)$data['id']; |
|
| 227 | 2 | $Config = Config::instance(); |
|
| 228 | 2 | $password = ''; |
|
| 229 | 2 | if (!$this->get('password_hash', $this->reg_id)) { |
|
| 230 | 2 | $password = password_generate($Config->core['password_min_length'], $Config->core['password_min_strength']); |
|
| 231 | 2 | $this->set_password($password, $this->reg_id); |
|
| 232 | } |
||
| 233 | 2 | $this->set('status', User::STATUS_ACTIVE, $this->reg_id); |
|
| 234 | 2 | $this->set_groups([User::USER_GROUP_ID], $this->reg_id); |
|
| 235 | 2 | Session::instance()->add($this->reg_id); |
|
| 236 | 2 | if (!Event::instance()->fire( |
|
| 237 | 2 | 'System/User/registration/confirmation/after', |
|
| 238 | [ |
||
| 239 | 2 | 'id' => $this->reg_id |
|
| 240 | ] |
||
| 241 | ) |
||
| 242 | ) { |
||
| 243 | 2 | $this->registration_cancel(); |
|
| 244 | 2 | return false; |
|
| 245 | } |
||
| 246 | 2 | unset($this->cache->{$data['login_hash']}); |
|
| 247 | return [ |
||
| 248 | 2 | 'id' => $this->reg_id, |
|
| 249 | 2 | 'email' => $data['email'], |
|
| 250 | 2 | 'password' => $password |
|
| 251 | ]; |
||
| 252 | } |
||
| 253 | /** |
||
| 254 | * Canceling of bad/failed registration |
||
| 255 | */ |
||
| 256 | 4 | function registration_cancel () { |
|
| 263 | /** |
||
| 264 | * Checks for unconfirmed registrations and deletes expired |
||
| 265 | */ |
||
| 266 | 6 | protected function delete_unconfirmed_users () { |
|
| 280 | /** |
||
| 281 | * Proper password setting without any need to deal with low-level implementation |
||
| 282 | * |
||
| 283 | * @param string $new_password |
||
| 284 | * @param false|int $user |
||
| 285 | * @param bool $already_prepared If true - assumed that `sha512(sha512(password) + public_key)` was applied to password |
||
| 286 | * |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | 6 | function set_password ($new_password, $user = false, $already_prepared = false) { |
|
| 302 | /** |
||
| 303 | * Proper password validation without any need to deal with low-level implementation |
||
| 304 | * |
||
| 305 | * @param string $password |
||
| 306 | * @param false|int $user |
||
| 307 | * @param bool $already_prepared If true - assumed that `sha512(sha512(password) + public_key)` was applied to password |
||
| 308 | * |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | 6 | function validate_password ($password, $user = false, $already_prepared = false) { |
|
| 332 | /** |
||
| 333 | * Restoring of password |
||
| 334 | * |
||
| 335 | * @param int $user |
||
| 336 | * |
||
| 337 | * @return false|string Key for confirmation or <b>false</b> on failure |
||
| 338 | */ |
||
| 339 | 2 | function restore_password ($user) { |
|
| 351 | /** |
||
| 352 | * Confirmation of password restoring process |
||
| 353 | * |
||
| 354 | * @param string $key |
||
| 355 | * |
||
| 356 | * @return array|false ['id' => <i>id</i>, 'password' => <i>password</i>] or <b>false</b> on failure |
||
| 357 | */ |
||
| 358 | 2 | function restore_password_confirmation ($key) { |
|
| 389 | /** |
||
| 390 | * Delete specified user or array of users |
||
| 391 | * |
||
| 392 | * @param int|int[] $user User id or array of users ids |
||
| 393 | */ |
||
| 394 | 4 | function del_user ($user) { |
|
| 395 | 4 | $this->disable_memory_cache(); |
|
| 396 | 4 | if (is_array($user)) { |
|
| 397 | 2 | foreach ($user as $id) { |
|
| 398 | 2 | $this->del_user($id); |
|
| 399 | } |
||
| 400 | 2 | return; |
|
| 401 | } |
||
| 402 | 4 | $user = (int)$user; |
|
| 403 | 4 | if (in_array($user, [0, User::GUEST_ID, User::ROOT_ID]) || !$this->get('id', $user)) { |
|
| 404 | 2 | return; |
|
| 405 | } |
||
| 406 | 4 | Event::instance()->fire( |
|
| 407 | 4 | 'System/User/del/before', |
|
| 408 | [ |
||
| 409 | 4 | 'id' => $user |
|
| 410 | ] |
||
| 411 | ); |
||
| 412 | 4 | $this->set_groups([], $user); |
|
| 413 | 4 | $this->del_permissions_all($user); |
|
| 414 | 4 | $Cache = $this->cache; |
|
| 415 | 4 | $login_hash = hash('sha224', $this->get('login', $user)); |
|
| 416 | 4 | $this->db_prime()->q( |
|
| 417 | "DELETE FROM `[prefix]users` |
||
| 418 | 4 | WHERE `id` = $user" |
|
| 419 | ); |
||
| 420 | unset( |
||
| 421 | 4 | $Cache->$login_hash, |
|
| 422 | 4 | $Cache->$user |
|
| 423 | ); |
||
| 424 | 4 | Event::instance()->fire( |
|
| 425 | 4 | 'System/User/del/after', |
|
| 426 | [ |
||
| 427 | 4 | 'id' => $user |
|
| 428 | ] |
||
| 429 | ); |
||
| 430 | 4 | } |
|
| 431 | /** |
||
| 432 | * Returns array of user id, that are associated as contacts |
||
| 433 | * |
||
| 434 | * @param false|int $user If not specified - current user assumed |
||
| 435 | * |
||
| 436 | * @return int[] Array of user id |
||
| 437 | */ |
||
| 438 | function get_contacts ($user = false) { |
||
| 453 | } |
||
| 454 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.