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 |
||
| 31 | trait Management { |
||
| 32 | /** |
||
| 33 | * User id after registration |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | protected $reg_id = 0; |
||
| 37 | /** |
||
| 38 | * Search keyword in login, username and email |
||
| 39 | * |
||
| 40 | * @param string $search_phrase |
||
| 41 | * |
||
| 42 | * @return false|int[] |
||
|
|
|||
| 43 | */ |
||
| 44 | function search_users ($search_phrase) { |
||
| 68 | /** |
||
| 69 | * User registration |
||
| 70 | * |
||
| 71 | * @param string $email |
||
| 72 | * @param bool $confirmation If <b>true</b> - default system option is used, if <b>false</b> - registration will be finished without necessity of |
||
| 73 | * confirmation, independently from default system option (is used for manual registration). |
||
| 74 | * @param bool $auto_sign_in If <b>false</b> - no auto sign in, if <b>true</b> - according to system configuration |
||
| 75 | * |
||
| 76 | * @return array|false|string <b>exists</b> - if user with such email is already registered<br> |
||
| 77 | * <b>error</b> - if error occurred<br> |
||
| 78 | * <b>false</b> - if email is incorrect<br> |
||
| 79 | * <b>[<br> |
||
| 80 | * 'reg_key' => *,</b> //Registration confirmation key, or <b>true</b> if confirmation is not required<br> |
||
| 81 | * <b>'password' => *,</b> //Automatically generated password<br> |
||
| 82 | * <b>'id' => *</b> //Id of registered user in DB<br> |
||
| 83 | * <b>]</b> |
||
| 84 | */ |
||
| 85 | function registration ($email, $confirmation = true, $auto_sign_in = true) { |
||
| 193 | /** |
||
| 194 | * Confirmation of registration process |
||
| 195 | * |
||
| 196 | * @param string $reg_key |
||
| 197 | * |
||
| 198 | * @return array|false ['id' => <i>id</i>, 'email' => <i>email</i>, 'password' => <i>password</i>] or <b>false</b> on failure |
||
| 199 | */ |
||
| 200 | function registration_confirmation ($reg_key) { |
||
| 257 | /** |
||
| 258 | * Canceling of bad/failed registration |
||
| 259 | */ |
||
| 260 | function registration_cancel () { |
||
| 268 | /** |
||
| 269 | * Checks for unconfirmed registrations and deletes expired |
||
| 270 | */ |
||
| 271 | protected function delete_unconfirmed_users () { |
||
| 287 | /** |
||
| 288 | * Proper password setting without any need to deal with low-level implementation |
||
| 289 | * |
||
| 290 | * @param string $new_password |
||
| 291 | * @param false|int $user |
||
| 292 | * @param bool $already_prepared If true - assumed that `sha512(sha512(password) + public_key)` was applied to password |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | function set_password ($new_password, $user = false, $already_prepared = false) { |
||
| 309 | /** |
||
| 310 | * Proper password validation without any need to deal with low-level implementation |
||
| 311 | * |
||
| 312 | * @param string $password |
||
| 313 | * @param false|int $user |
||
| 314 | * @param bool $already_prepared If true - assumed that `sha512(sha512(password) + public_key)` was applied to password |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | function validate_password ($password, $user = false, $already_prepared = false) { |
||
| 339 | /** |
||
| 340 | * Restoring of password |
||
| 341 | * |
||
| 342 | * @param int $user |
||
| 343 | * |
||
| 344 | * @return false|string Key for confirmation or <b>false</b> on failure |
||
| 345 | */ |
||
| 346 | function restore_password ($user) { |
||
| 359 | /** |
||
| 360 | * Confirmation of password restoring process |
||
| 361 | * |
||
| 362 | * @param string $key |
||
| 363 | * |
||
| 364 | * @return array|false ['id' => <i>id</i>, 'password' => <i>password</i>] or <b>false</b> on failure |
||
| 365 | */ |
||
| 366 | function restore_password_confirmation ($key) { |
||
| 404 | /** |
||
| 405 | * Delete specified user or array of users |
||
| 406 | * |
||
| 407 | * @param int|int[] $user User id or array of users ids |
||
| 408 | */ |
||
| 409 | function del_user ($user) { |
||
| 412 | /** |
||
| 413 | * Delete specified user or array of users |
||
| 414 | * |
||
| 415 | * @param int|int[] $user |
||
| 416 | * @param bool $update |
||
| 417 | */ |
||
| 418 | protected function del_user_internal ($user, $update = true) { |
||
| 462 | /** |
||
| 463 | * Add bot |
||
| 464 | * |
||
| 465 | * @param string $name Bot name |
||
| 466 | * @param string $user_agent User Agent string or regular expression |
||
| 467 | * @param string $ip IP string or regular expression |
||
| 468 | * |
||
| 469 | * @return false|int Bot <b>id</b> in DB or <b>false</b> on failure |
||
| 470 | */ |
||
| 471 | function add_bot ($name, $user_agent, $ip) { |
||
| 503 | /** |
||
| 504 | * Set bot |
||
| 505 | * |
||
| 506 | * @param int $id Bot id |
||
| 507 | * @param string $name Bot name |
||
| 508 | * @param string $user_agent User Agent string or regular expression |
||
| 509 | * @param string $ip IP string or regular expression |
||
| 510 | * |
||
| 511 | * @return bool |
||
| 512 | */ |
||
| 513 | function set_bot ($id, $name, $user_agent, $ip) { |
||
| 526 | /** |
||
| 527 | * Delete specified bot or array of bots |
||
| 528 | * |
||
| 529 | * @param int|int[] $bot Bot id or array of bots ids |
||
| 530 | */ |
||
| 531 | function del_bot ($bot) { |
||
| 535 | /** |
||
| 536 | * Returns array of user id, that are associated as contacts |
||
| 537 | * |
||
| 538 | * @param false|int $user If not specified - current user assumed |
||
| 539 | * |
||
| 540 | * @return int[] Array of user id |
||
| 541 | */ |
||
| 542 | function get_contacts ($user = false) { |
||
| 557 | } |
||
| 558 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.