Conditions | 17 |
Paths | 1280 |
Total Lines | 80 |
Code Lines | 50 |
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 |
||
100 | public function updateUser() |
||
101 | { |
||
102 | $this->onlyUser(); |
||
103 | $this->onlyPost(); |
||
104 | |||
105 | $user = (object)$this->request->getDataFull(); |
||
106 | $redirectUrl = "/admin"; |
||
107 | |||
108 | if ($user->userId !== $this->session->get("userId") || isset($user->userRoleSelector) || isset($user->locked_out)) { |
||
109 | //an admin is trying to update a user or form tampered with |
||
110 | $this->onlyAdmin(); |
||
111 | $redirectUrl = "/admin/home/view-user/" . $user->userId; |
||
112 | } else { |
||
113 | //set the role to the original state for update |
||
114 | $beforeUser = $this->userModel->getUserDetailsById($user->userId); |
||
115 | $user->userRoleSelector = $beforeUser->roles_idroles; |
||
116 | $user->userLockedOut = $beforeUser->locked_out; |
||
117 | } |
||
118 | |||
119 | $userId = $user->userId; |
||
120 | $password = $user->forgotPassword ?? ""; |
||
121 | $confirm = $user->forgotConfirm ?? ""; |
||
122 | $resetPassword = false; |
||
123 | $error = false; |
||
124 | $registerErrors = new \stdClass(); |
||
125 | |||
126 | if($userId == 1 && $user->userLockedOut == 1) |
||
127 | { |
||
128 | $error = true; |
||
129 | $this->alertBox->setAlert("Original admin may not be deactivated", "error"); |
||
130 | } |
||
131 | |||
132 | if($userId == 1 && $user->userRoleSelector != 2) |
||
133 | { |
||
134 | $error = true; |
||
135 | $this->alertBox->setAlert("Original admin must stay admin", "error"); |
||
136 | } |
||
137 | |||
138 | if ($password !== "" || $confirm !== "") { |
||
139 | //we are resetting the password |
||
140 | $resetPassword = true; |
||
141 | if ($password !== $confirm) { |
||
142 | $error = true; |
||
143 | $registerErrors->forgotPassword = "password and confirmation do not match"; |
||
144 | $registerErrors->forgotConfirm = "password and confirmation do not match"; |
||
145 | } |
||
146 | |||
147 | $passwordError = $this->isPasswordComplex($password); |
||
148 | if (!$passwordError["success"]) { |
||
149 | $error = true; |
||
150 | $registerErrors->forgotPassword = $passwordError["message"]; |
||
151 | } |
||
152 | } |
||
153 | |||
154 | if ($user->userName == "") { |
||
155 | $error = true; |
||
156 | $registerErrors->userName = "name must not be empty"; |
||
157 | } |
||
158 | if ($user->userSurname == "") { |
||
159 | $error = true; |
||
160 | $registerErrors->userSurname = "surname must not be empty"; |
||
161 | } |
||
162 | if ($user->userUsername == "") { |
||
163 | $error = true; |
||
164 | $registerErrors->userUsername = "username must not be empty"; |
||
165 | } |
||
166 | |||
167 | if ($error) { |
||
168 | $this->session->set("registrationErrors", $registerErrors); |
||
169 | $this->response->redirect($redirectUrl); |
||
170 | } |
||
171 | |||
172 | if ($resetPassword) { |
||
173 | $this->userModel->resetPassword($userId, $password); |
||
174 | } |
||
175 | |||
176 | $this->userModel->updateUser($user); |
||
177 | |||
178 | $this->alertBox->setAlert('User details updated'); |
||
179 | $this->response->redirect($redirectUrl); |
||
180 | } |
||
229 | } |