| Conditions | 12 |
| Paths | 320 |
| Total Lines | 67 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 91 | public function updateUser() |
||
| 92 | { |
||
| 93 | $this->onlyUser(); |
||
| 94 | $this->onlyPost(); |
||
| 95 | |||
| 96 | $user = (object)$this->request->getDataFull(); |
||
| 97 | $redirectUrl = "/admin"; |
||
| 98 | |||
| 99 | if ($user->userId !== $this->session->get("userId") || isset($user->userRoleSelector)) { |
||
| 100 | //an admin is trying to update a user or form tampered with |
||
| 101 | $this->onlyAdmin(); |
||
| 102 | $redirectUrl = "/admin/home/view-user/" . $user->userId; |
||
| 103 | } else { |
||
| 104 | //set the role to the original state for update |
||
| 105 | $beforeUser = $this->userModel->getUserDetailsById($user->userId); |
||
| 106 | $user->userRoleSelector = $beforeUser->roles_idroles; |
||
| 107 | } |
||
| 108 | |||
| 109 | $userId = $user->userId; |
||
| 110 | $password = $user->forgotPassword ?? ""; |
||
| 111 | $confirm = $user->forgotConfirm ?? ""; |
||
| 112 | $resetPassword = false; |
||
| 113 | $error = false; |
||
| 114 | $registerErrors = new \stdClass(); |
||
| 115 | |||
| 116 | if ($password !== "" || $confirm !== "") { |
||
| 117 | //we are resetting the password |
||
| 118 | $resetPassword = true; |
||
| 119 | if ($password !== $confirm) { |
||
| 120 | $error = true; |
||
| 121 | $registerErrors->forgotPassword = "password and confirmation do not match"; |
||
| 122 | $registerErrors->forgotConfirm = "password and confirmation do not match"; |
||
| 123 | } |
||
| 124 | |||
| 125 | $passwordError = $this->isPasswordComplex($password); |
||
| 126 | if (!$passwordError["success"]) { |
||
| 127 | $error = true; |
||
| 128 | $registerErrors->forgotPassword = $passwordError["message"]; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($user->userName == "") { |
||
| 133 | $error = true; |
||
| 134 | $registerErrors->userName = "name must not be empty"; |
||
| 135 | } |
||
| 136 | if ($user->userSurname == "") { |
||
| 137 | $error = true; |
||
| 138 | $registerErrors->userSurname = "surname must not be empty"; |
||
| 139 | } |
||
| 140 | if ($user->userUsername == "") { |
||
| 141 | $error = true; |
||
| 142 | $registerErrors->userUsername = "username must not be empty"; |
||
| 143 | } |
||
| 144 | |||
| 145 | if ($error) { |
||
| 146 | $this->session->set("registrationErrors", $registerErrors); |
||
| 147 | $this->response->redirect($redirectUrl); |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($resetPassword) { |
||
| 151 | $this->userModel->resetPassword($userId, $password); |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->userModel->updateUser($user); |
||
| 155 | |||
| 156 | $this->alertBox->setAlert('User details updated'); |
||
| 157 | $this->response->redirect($redirectUrl); |
||
| 158 | } |
||
| 159 | } |