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) { |
||
446 | /** |
||
447 | * Add bot |
||
448 | * |
||
449 | * @param string $name Bot name |
||
450 | * @param string $user_agent User Agent string or regular expression |
||
451 | * @param string $ip IP string or regular expression |
||
452 | * |
||
453 | * @return false|int Bot <b>id</b> in DB or <b>false</b> on failure |
||
454 | */ |
||
455 | function add_bot ($name, $user_agent, $ip) { |
||
487 | /** |
||
488 | * Set bot |
||
489 | * |
||
490 | * @param int $id Bot id |
||
491 | * @param string $name Bot name |
||
492 | * @param string $user_agent User Agent string or regular expression |
||
493 | * @param string $ip IP string or regular expression |
||
494 | * |
||
495 | * @return bool |
||
496 | */ |
||
497 | function set_bot ($id, $name, $user_agent, $ip) { |
||
510 | /** |
||
511 | * Delete specified bot or array of bots |
||
512 | * |
||
513 | * @param int|int[] $bot Bot id or array of bots ids |
||
514 | */ |
||
515 | function del_bot ($bot) { |
||
519 | /** |
||
520 | * Returns array of user id, that are associated as contacts |
||
521 | * |
||
522 | * @param false|int $user If not specified - current user assumed |
||
523 | * |
||
524 | * @return int[] Array of user id |
||
525 | */ |
||
526 | function get_contacts ($user = false) { |
||
541 | } |
||
542 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.