| Conditions | 18 |
| Paths | 199 |
| Total Lines | 77 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 46 | static function user_registration () { |
||
| 47 | $Config = Config::instance(); |
||
| 48 | $L = new Prefix('system_profile_registration_'); |
||
| 49 | $Page = Page::instance(); |
||
| 50 | $Request = Request::instance(); |
||
| 51 | $User = User::instance(); |
||
| 52 | if (!isset($Request->data['email'])) { |
||
| 53 | throw new ExitException(400); |
||
| 54 | } elseif (!$User->guest()) { |
||
| 55 | $Page->json('reload'); |
||
| 56 | return; |
||
| 57 | } elseif (!$Config->core['allow_user_registration']) { |
||
| 58 | throw new ExitException($L->prohibited, 403); |
||
| 59 | } elseif (empty($Request->data['email'])) { |
||
| 60 | throw new ExitException($L->please_type_your_email, 400); |
||
| 61 | } |
||
| 62 | $email = mb_strtolower($Request->data['email']); |
||
| 63 | $result = $User->registration($email); |
||
| 64 | if ($result === false) { |
||
| 65 | throw new ExitException($L->please_type_correct_email, 400); |
||
| 66 | } elseif ($result == 'error') { |
||
| 67 | throw new ExitException($L->server_error, 500); |
||
| 68 | } elseif ($result == 'exists') { |
||
| 69 | throw new ExitException($L->error_exists, 400); |
||
| 70 | } |
||
| 71 | $confirm = $result['reg_key'] !== true; |
||
| 72 | if ($Request->data['username']) { |
||
| 73 | $User->set('username', $Request->data['username'], $result['id']); |
||
| 74 | } |
||
| 75 | // Actually `sha512(sha512(password) + public_key)` instead of plain password |
||
| 76 | if ($Request->data['password']) { |
||
| 77 | $User->set_password($Request->data['password'], $result['id'], true); |
||
| 78 | } |
||
| 79 | if ($Request->data['language']) { |
||
| 80 | $User->set('language', $Request->data['language'], $result['id']); |
||
| 81 | } |
||
| 82 | if ($Request->data['timezone']) { |
||
| 83 | $User->set('timezone', $Request->data['timezone'], $result['id']); |
||
| 84 | } |
||
| 85 | if ($Request->data['avatar']) { |
||
| 86 | $User->set('avatar', $Request->data['avatar'], $result['id']); |
||
| 87 | } |
||
| 88 | if ($confirm) { |
||
| 89 | $body = $L->need_confirmation_mail_body( |
||
| 90 | $User->username($result['id']), |
||
| 91 | get_core_ml_text('name'), |
||
| 92 | $Config->core_url()."/profile/registration_confirmation/$result[reg_key]", |
||
| 93 | $L->time($Config->core['registration_confirmation_time'], 'd') |
||
| 94 | ); |
||
| 95 | } elseif ($result['password']) { |
||
| 96 | $body = $L->success_mail_with_password_body( |
||
| 97 | $User->username($result['id']), |
||
| 98 | get_core_ml_text('name'), |
||
| 99 | $Config->core_url().'/profile/settings', |
||
| 100 | $User->get('login', $result['id']), |
||
| 101 | $result['password'] |
||
| 102 | ); |
||
| 103 | } else { |
||
| 104 | $body = $L->success_mail( |
||
| 105 | $User->username($result['id']), |
||
| 106 | get_core_ml_text('name'), |
||
| 107 | $Config->core_url().'/profile/settings', |
||
| 108 | $User->get('login', $result['id']) |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | if (Mail::instance()->send_to( |
||
| 112 | $email, |
||
| 113 | $L->{$confirm ? 'need_confirmation_mail' : 'success_mail'}(get_core_ml_text('name')), |
||
| 114 | $body |
||
| 115 | ) |
||
| 116 | ) { |
||
| 117 | $Page->json($confirm ? 'registration_confirmation' : 'registration_success'); |
||
| 118 | } else { |
||
| 119 | $User->registration_cancel(); |
||
| 120 | throw new ExitException($L->mail_sending_error, 500); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | static function user_restore_password () { |
||
| 214 |