| Conditions | 10 |
| Paths | 13 |
| Total Lines | 103 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 158 | public function update(ServerRequestInterface $request): ResponseInterface |
||
| 159 | { |
||
| 160 | $context = []; |
||
| 161 | $param = new RequestData($request); |
||
| 162 | |||
| 163 | $id = $this->authentication->getUser()->getId(); |
||
| 164 | |||
| 165 | /** @var User|null $user */ |
||
| 166 | $user = $this->userRepository->find($id); |
||
| 167 | |||
| 168 | if ($user === null) { |
||
| 169 | $this->authentication->logout(); |
||
| 170 | |||
| 171 | $this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
||
| 172 | |||
| 173 | return new RedirectResponse( |
||
| 174 | $this->routeHelper->generateUrl('user_login') |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | $context['user'] = $user; |
||
| 178 | $context['param'] = (new UserParam())->fromEntity($user); |
||
| 179 | |||
| 180 | if ($request->getMethod() === 'GET') { |
||
| 181 | return new TemplateResponse( |
||
| 182 | $this->template, |
||
| 183 | 'user/update_profile', |
||
| 184 | $context |
||
| 185 | ); |
||
| 186 | } |
||
| 187 | $formParam = new UserParam($param->posts()); |
||
| 188 | $formParam->setStatus($user->status); |
||
| 189 | |||
| 190 | $context['param'] = $formParam; |
||
| 191 | |||
| 192 | $validator = new UserValidator($formParam, $this->lang, true); |
||
| 193 | if ($validator->validate() === false) { |
||
| 194 | $context['errors'] = $validator->getErrors(); |
||
| 195 | |||
| 196 | return new TemplateResponse( |
||
| 197 | $this->template, |
||
| 198 | 'user/update_profile', |
||
| 199 | $context |
||
| 200 | ); |
||
| 201 | } |
||
| 202 | |||
| 203 | $usernameExist = $this->userRepository->findBy([ |
||
| 204 | 'username' => $formParam->getUsername(), |
||
| 205 | ]); |
||
| 206 | |||
| 207 | if ($usernameExist !== null && $usernameExist->id !== $id) { |
||
| 208 | $this->flash->setError($this->lang->tr('This username already exist')); |
||
| 209 | |||
| 210 | return new TemplateResponse( |
||
| 211 | $this->template, |
||
| 212 | 'user/update_profile', |
||
| 213 | $context |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | $emailExist = $this->userRepository->findBy([ |
||
| 218 | 'email' => $formParam->getEmail(), |
||
| 219 | ]); |
||
| 220 | |||
| 221 | if ($emailExist !== null && $emailExist->id !== $id) { |
||
| 222 | $this->flash->setError($this->lang->tr('This email already exist')); |
||
| 223 | |||
| 224 | return new TemplateResponse( |
||
| 225 | $this->template, |
||
| 226 | 'user/update_profile', |
||
| 227 | $context |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | |||
| 231 | $user->username = $formParam->getUsername(); |
||
| 232 | $user->lastname = $formParam->getLastname(); |
||
| 233 | $user->firstname = $formParam->getFirstname(); |
||
| 234 | $user->email = $formParam->getEmail(); |
||
| 235 | $user->status = $formParam->getStatus(); |
||
| 236 | |||
| 237 | $password = $formParam->getPassword(); |
||
| 238 | if (!empty($password)) { |
||
| 239 | $passwordHash = $this->hash->hash($password); |
||
| 240 | |||
| 241 | $user->password = $passwordHash; |
||
| 242 | } |
||
| 243 | |||
| 244 | try { |
||
| 245 | $this->userRepository->save($user); |
||
| 246 | |||
| 247 | $this->flash->setSuccess($this->lang->tr('Data successfully updated')); |
||
| 248 | |||
| 249 | return new RedirectResponse( |
||
| 250 | $this->routeHelper->generateUrl('user_profile') |
||
| 251 | ); |
||
| 252 | } catch (Exception $ex) { |
||
| 253 | $this->logger->error('Error when saved the data {error}', ['error' => $ex->getMessage()]); |
||
| 254 | |||
| 255 | $this->flash->setError($this->lang->tr('Data processing error')); |
||
| 256 | |||
| 257 | return new TemplateResponse( |
||
| 258 | $this->template, |
||
| 259 | 'user/update_profile', |
||
| 260 | $context |
||
| 261 | ); |
||
| 265 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.