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 | public 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 | 28 | public function registration ($email, $confirmation = true, $auto_sign_in = true) { |
|
| 85 | 28 | if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
|
| 86 | 6 | return false; |
|
| 87 | } |
||
| 88 | 28 | $email = mb_strtolower($email); |
|
| 89 | 28 | $email_hash = hash('sha224', $email); |
|
| 90 | 28 | $login = strstr($email, '@', true); |
|
| 91 | 28 | $login_hash = hash('sha224', $login); |
|
| 92 | 28 | if ($this->registration_login_already_occupied($login, $login_hash)) { |
|
| 93 | 4 | $login = $email; |
|
| 94 | 4 | $login_hash = $email_hash; |
|
| 95 | } |
||
| 96 | 28 | if ($this->get_id($email_hash)) { |
|
| 97 | 4 | return 'exists'; |
|
| 98 | } |
||
| 99 | 28 | $this->delete_unconfirmed_users(); |
|
| 100 | 28 | if (!Event::instance()->fire( |
|
| 101 | 28 | 'System/User/registration/before', |
|
| 102 | [ |
||
| 103 | 28 | 'email' => $email |
|
| 104 | ] |
||
| 105 | ) |
||
| 106 | ) { |
||
| 107 | 2 | return false; |
|
| 108 | } |
||
| 109 | 28 | $Config = Config::instance(); |
|
| 110 | 28 | $confirmation = $confirmation && $Config->core['require_registration_confirmation']; |
|
| 111 | 28 | $reg_key = md5(random_bytes(1000)); |
|
| 112 | 28 | if ($this->db_prime()->q( |
|
| 113 | "INSERT INTO `[prefix]users` ( |
||
| 114 | `login`, |
||
| 115 | `login_hash`, |
||
| 116 | `email`, |
||
| 117 | `email_hash`, |
||
| 118 | `reg_date`, |
||
| 119 | `reg_ip`, |
||
| 120 | `reg_key`, |
||
| 121 | `status` |
||
| 122 | ) VALUES ( |
||
| 123 | '%s', |
||
| 124 | '%s', |
||
| 125 | '%s', |
||
| 126 | '%s', |
||
| 127 | '%s', |
||
| 128 | '%s', |
||
| 129 | '%s', |
||
| 130 | '%s' |
||
| 131 | 28 | )", |
|
| 132 | $login, |
||
| 133 | $login_hash, |
||
| 134 | $email, |
||
| 135 | $email_hash, |
||
| 136 | time(), |
||
| 137 | 28 | ip2hex(Request::instance()->ip), |
|
| 138 | $reg_key, |
||
| 139 | 28 | !$confirmation ? 1 : -1 |
|
| 140 | ) |
||
| 141 | ) { |
||
| 142 | 28 | $this->reg_id = (int)$this->db_prime()->id(); |
|
| 143 | 28 | $password = ''; |
|
| 144 | 28 | if (!$confirmation) { |
|
| 145 | 26 | $password = password_generate($Config->core['password_min_length'], $Config->core['password_min_strength']); |
|
| 146 | 26 | $this->set_password($password, $this->reg_id); |
|
| 147 | 26 | $this->set_groups([User::USER_GROUP_ID], $this->reg_id); |
|
| 148 | 26 | if ($auto_sign_in && $Config->core['auto_sign_in_after_registration']) { |
|
| 149 | 12 | Session::instance()->add($this->reg_id); |
|
| 150 | } |
||
| 151 | } |
||
| 152 | 28 | if (!Event::instance()->fire( |
|
| 153 | 28 | 'System/User/registration/after', |
|
| 154 | [ |
||
| 155 | 28 | 'id' => $this->reg_id |
|
| 156 | ] |
||
| 157 | ) |
||
| 158 | ) { |
||
| 159 | 2 | $this->registration_cancel(); |
|
| 160 | 2 | return false; |
|
| 161 | } |
||
| 162 | 28 | if (!$confirmation) { |
|
| 163 | 26 | $this->set_groups([User::USER_GROUP_ID], $this->reg_id); |
|
| 164 | } |
||
| 165 | 28 | unset($this->cache->$login_hash); |
|
| 166 | return [ |
||
| 167 | 28 | 'reg_key' => !$confirmation ? true : $reg_key, |
|
| 168 | 28 | 'password' => $password, |
|
| 169 | 28 | 'id' => $this->reg_id |
|
| 170 | ]; |
||
| 171 | } else { |
||
| 172 | return 'error'; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | /** |
||
| 176 | * @param string $login |
||
| 177 | * @param string $login_hash |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | 28 | protected function registration_login_already_occupied ($login, $login_hash) { |
|
| 182 | return |
||
| 183 | 28 | $this->get_id($login_hash) !== false || |
|
| 184 | ( |
||
| 185 | 28 | $login && |
|
| 186 | 28 | in_array($login, file_get_json(MODULES.'/System/index.json')['profile']) |
|
| 187 | ); |
||
| 188 | } |
||
| 189 | /** |
||
| 190 | * Confirmation of registration process |
||
| 191 | * |
||
| 192 | * @param string $reg_key |
||
| 193 | * |
||
| 194 | * @return array|false ['id' => <i>id</i>, 'email' => <i>email</i>, 'password' => <i>password</i>] or <b>false</b> on failure |
||
| 195 | */ |
||
| 196 | 2 | public function registration_confirmation ($reg_key) { |
|
| 197 | 2 | if (!is_md5($reg_key)) { |
|
| 198 | 2 | return false; |
|
| 199 | } |
||
| 200 | 2 | if (!Event::instance()->fire( |
|
| 201 | 2 | 'System/User/registration/confirmation/before', |
|
| 202 | [ |
||
| 203 | 2 | 'reg_key' => $reg_key |
|
| 204 | ] |
||
| 205 | ) |
||
| 206 | ) { |
||
| 207 | 2 | $this->registration_cancel(); |
|
| 208 | 2 | return false; |
|
| 209 | } |
||
| 210 | 2 | $this->delete_unconfirmed_users(); |
|
| 211 | 2 | $data = $this->db_prime()->qf( |
|
| 212 | "SELECT |
||
| 213 | `id`, |
||
| 214 | `login_hash`, |
||
| 215 | `email` |
||
| 216 | FROM `[prefix]users` |
||
| 217 | WHERE |
||
| 218 | `reg_key` = '%s' AND |
||
| 219 | `status` = '%s' |
||
| 220 | 2 | LIMIT 1", |
|
| 221 | $reg_key, |
||
| 222 | 2 | User::STATUS_NOT_ACTIVATED |
|
| 223 | ); |
||
| 224 | 2 | if (!$data) { |
|
| 225 | 2 | return false; |
|
| 226 | } |
||
| 227 | 2 | $this->reg_id = (int)$data['id']; |
|
| 228 | 2 | $Config = Config::instance(); |
|
| 229 | 2 | $password = ''; |
|
| 230 | 2 | if (!$this->get('password_hash', $this->reg_id)) { |
|
| 231 | 2 | $password = password_generate($Config->core['password_min_length'], $Config->core['password_min_strength']); |
|
| 232 | 2 | $this->set_password($password, $this->reg_id); |
|
| 233 | } |
||
| 234 | 2 | $this->set('status', User::STATUS_ACTIVE, $this->reg_id); |
|
| 235 | 2 | $this->set_groups([User::USER_GROUP_ID], $this->reg_id); |
|
| 236 | 2 | Session::instance()->add($this->reg_id); |
|
| 237 | 2 | if (!Event::instance()->fire( |
|
| 238 | 2 | 'System/User/registration/confirmation/after', |
|
| 239 | [ |
||
| 240 | 2 | 'id' => $this->reg_id |
|
| 241 | ] |
||
| 242 | ) |
||
| 243 | ) { |
||
| 244 | 2 | $this->registration_cancel(); |
|
| 245 | 2 | return false; |
|
| 246 | } |
||
| 247 | 2 | unset($this->cache->{$data['login_hash']}); |
|
| 248 | return [ |
||
| 249 | 2 | 'id' => $this->reg_id, |
|
| 250 | 2 | 'email' => $data['email'], |
|
| 251 | 2 | 'password' => $password |
|
| 252 | ]; |
||
| 253 | } |
||
| 254 | /** |
||
| 255 | * Canceling of bad/failed registration |
||
| 256 | */ |
||
| 257 | 6 | public function registration_cancel () { |
|
| 258 | 6 | if ($this->reg_id) { |
|
| 259 | 6 | Session::instance()->add(User::GUEST_ID); |
|
| 260 | 6 | $this->del_user($this->reg_id); |
|
| 261 | 6 | $this->reg_id = 0; |
|
| 262 | } |
||
| 263 | 6 | } |
|
| 264 | /** |
||
| 265 | * Checks for unconfirmed registrations and deletes expired |
||
| 266 | */ |
||
| 267 | 28 | protected function delete_unconfirmed_users () { |
|
| 281 | /** |
||
| 282 | * Proper password setting without any need to deal with low-level implementation |
||
| 283 | * |
||
| 284 | * @param string $new_password |
||
| 285 | * @param false|int $user |
||
| 286 | * @param bool $already_prepared If true - assumed that `sha512(sha512(password) + public_key)` was applied to password |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | 28 | public function set_password ($new_password, $user = false, $already_prepared = false) { |
|
| 303 | /** |
||
| 304 | * Proper password validation without any need to deal with low-level implementation |
||
| 305 | * |
||
| 306 | * @param string $password |
||
| 307 | * @param false|int $user |
||
| 308 | * @param bool $already_prepared If true - assumed that `sha512(sha512(password) + public_key)` was applied to password |
||
| 309 | * |
||
| 310 | * @return bool |
||
| 311 | */ |
||
| 312 | 8 | public function validate_password ($password, $user = false, $already_prepared = false) { |
|
| 333 | /** |
||
| 334 | * Restoring of password |
||
| 335 | * |
||
| 336 | * @param int $user |
||
| 337 | * |
||
| 338 | * @return false|string Key for confirmation or <b>false</b> on failure |
||
| 339 | */ |
||
| 340 | 4 | public function restore_password ($user) { |
|
| 352 | /** |
||
| 353 | * Confirmation of password restoring process |
||
| 354 | * |
||
| 355 | * @param string $key |
||
| 356 | * |
||
| 357 | * @return array|false ['id' => <i>id</i>, 'password' => <i>password</i>] or <b>false</b> on failure |
||
| 358 | */ |
||
| 359 | 2 | public function restore_password_confirmation ($key) { |
|
| 390 | /** |
||
| 391 | * Delete specified user or array of users |
||
| 392 | * |
||
| 393 | * @param int|int[] $user User id or array of users ids |
||
| 394 | */ |
||
| 395 | 16 | public function del_user ($user) { |
|
| 434 | } |
||
| 435 |
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.