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) { |
|
| 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) { |
|
| 165 | /** |
||
| 166 | * @param string $email |
||
| 167 | * |
||
| 168 | * @return string[] |
||
| 169 | */ |
||
| 170 | 28 | protected function registration_get_login_login_hash ($email) { |
|
| 179 | /** |
||
| 180 | * @param string $login |
||
| 181 | * @param string $login_hash |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | 28 | protected function registration_login_already_occupied ($login, $login_hash) { |
|
| 193 | /** |
||
| 194 | * @param Config $Config |
||
| 195 | * @param int $id |
||
| 196 | * @param bool $create_session |
||
| 197 | * |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | 28 | protected function activate_registered_user ($Config, $id, $create_session) { |
|
| 213 | /** |
||
| 214 | * Confirmation of registration process |
||
| 215 | * |
||
| 216 | * @param string $reg_key |
||
| 217 | * |
||
| 218 | * @return array|false ['id' => <i>id</i>, 'email' => <i>email</i>, 'password' => <i>password</i>] or <b>false</b> on failure |
||
|
|
|||
| 219 | */ |
||
| 220 | 2 | public function registration_confirmation ($reg_key) { |
|
| 221 | 2 | if (!is_md5($reg_key)) { |
|
| 222 | 2 | return false; |
|
| 223 | } |
||
| 224 | 2 | if (!Event::instance()->fire( |
|
| 225 | 2 | 'System/User/registration/confirmation/before', |
|
| 226 | [ |
||
| 227 | 2 | 'reg_key' => $reg_key |
|
| 228 | ] |
||
| 229 | ) |
||
| 230 | ) { |
||
| 231 | 2 | $this->registration_cancel(); |
|
| 232 | 2 | return false; |
|
| 233 | } |
||
| 234 | 2 | $this->delete_unconfirmed_users(); |
|
| 235 | 2 | $data = $this->db_prime()->qf( |
|
| 236 | "SELECT |
||
| 237 | `id`, |
||
| 238 | `login_hash`, |
||
| 239 | `email` |
||
| 240 | FROM `[prefix]users` |
||
| 241 | WHERE |
||
| 242 | `reg_key` = '%s' AND |
||
| 243 | `status` = '%s' |
||
| 244 | 2 | LIMIT 1", |
|
| 245 | $reg_key, |
||
| 246 | 2 | User::STATUS_NOT_ACTIVATED |
|
| 247 | ); |
||
| 248 | 2 | if (!$data) { |
|
| 249 | 2 | return false; |
|
| 250 | } |
||
| 251 | 2 | $this->reg_id = (int)$data['id']; |
|
| 252 | 2 | $Config = Config::instance(); |
|
| 253 | 2 | $password = $this->activate_registered_user($Config, $this->reg_id, true); |
|
| 254 | 2 | if (!Event::instance()->fire( |
|
| 255 | 2 | 'System/User/registration/confirmation/after', |
|
| 256 | [ |
||
| 257 | 2 | 'id' => $this->reg_id |
|
| 258 | ] |
||
| 259 | ) |
||
| 260 | ) { |
||
| 261 | 2 | $this->registration_cancel(); |
|
| 262 | 2 | return false; |
|
| 263 | } |
||
| 264 | 2 | unset($this->cache->{$data['login_hash']}); |
|
| 265 | return [ |
||
| 266 | 2 | 'id' => $this->reg_id, |
|
| 267 | 2 | 'email' => $data['email'], |
|
| 268 | 2 | 'password' => $password |
|
| 269 | ]; |
||
| 270 | } |
||
| 271 | /** |
||
| 272 | * Canceling of bad/failed registration |
||
| 273 | */ |
||
| 274 | 6 | public function registration_cancel () { |
|
| 281 | /** |
||
| 282 | * Checks for unconfirmed registrations and deletes expired |
||
| 283 | */ |
||
| 284 | 28 | protected function delete_unconfirmed_users () { |
|
| 298 | /** |
||
| 299 | * Proper password setting without any need to deal with low-level implementation |
||
| 300 | * |
||
| 301 | * @param string $new_password |
||
| 302 | * @param false|int $user |
||
| 303 | * @param bool $already_prepared If true - assumed that `sha512(sha512(password) + public_key)` was applied to password |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | 28 | public function set_password ($new_password, $user = false, $already_prepared = false) { |
|
| 320 | /** |
||
| 321 | * Proper password validation without any need to deal with low-level implementation |
||
| 322 | * |
||
| 323 | * @param string $password |
||
| 324 | * @param false|int $user |
||
| 325 | * @param bool $already_prepared If true - assumed that `sha512(sha512(password) + public_key)` was applied to password |
||
| 326 | * |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | 8 | public function validate_password ($password, $user = false, $already_prepared = false) { |
|
| 350 | /** |
||
| 351 | * Restoring of password |
||
| 352 | * |
||
| 353 | * @param int $user |
||
| 354 | * |
||
| 355 | * @return false|string Key for confirmation or <b>false</b> on failure |
||
| 356 | */ |
||
| 357 | 4 | public function restore_password ($user) { |
|
| 369 | /** |
||
| 370 | * Confirmation of password restoring process |
||
| 371 | * |
||
| 372 | * @param string $key |
||
| 373 | * |
||
| 374 | * @return array|false ['id' => <i>id</i>, 'password' => <i>password</i>] or <b>false</b> on failure |
||
| 375 | */ |
||
| 376 | 2 | public function restore_password_confirmation ($key) { |
|
| 407 | /** |
||
| 408 | * Delete specified user or array of users |
||
| 409 | * |
||
| 410 | * @param int|int[] $user User id or array of users ids |
||
| 411 | */ |
||
| 412 | 16 | public function del_user ($user) { |
|
| 451 | } |
||
| 452 |
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.