| Conditions | 4 |
| Paths | 4 |
| Total Lines | 59 |
| Code Lines | 35 |
| 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 |
||
| 45 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 46 | { |
||
| 47 | if ($request->getMethod() === 'GET') { |
||
| 48 | return new TemplateResponse( |
||
| 49 | $this->template, |
||
| 50 | 'user/create', |
||
| 51 | [ |
||
| 52 | 'param' => new UserParam([]) |
||
| 53 | ] |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | $param = new RequestData($request); |
||
| 58 | $formParam = new UserParam($param->posts()); |
||
| 59 | $validator = new UserValidator($formParam); |
||
| 60 | |||
| 61 | if (!$validator->validate()) { |
||
| 62 | return new TemplateResponse( |
||
| 63 | $this->template, |
||
| 64 | 'user/create', |
||
| 65 | [ |
||
| 66 | 'errors' => $validator->getErrors(), |
||
| 67 | 'param' => $formParam |
||
| 68 | ] |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | |||
| 72 | $username = $param->post('username'); |
||
| 73 | $userExist = $this->userRepository->findBy(['username' => $username]); |
||
| 74 | |||
| 75 | if ($userExist) { |
||
| 76 | $this->logger->error('User with username {username} already exists', ['username' => $username]); |
||
| 77 | return new TemplateResponse( |
||
| 78 | $this->template, |
||
| 79 | 'user/create', |
||
| 80 | [ |
||
| 81 | 'param' => $formParam |
||
| 82 | ] |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | |||
| 86 | $password = $param->post('password'); |
||
| 87 | |||
| 88 | $hash = new BcryptHash(); |
||
| 89 | $passwordHash = $hash->hash($password); |
||
|
|
|||
| 90 | |||
| 91 | $user = $this->userRepository->create([ |
||
| 92 | 'username' => $formParam->getUsername(), |
||
| 93 | 'fname' => $formParam->getFirstname(), |
||
| 94 | 'lname' => $formParam->getLastname(), |
||
| 95 | 'password' => $passwordHash, |
||
| 96 | 'status' => 1, |
||
| 97 | 'age' => (int) $formParam->getAge(), |
||
| 98 | 'deleted' => 0, |
||
| 99 | ]); |
||
| 100 | |||
| 101 | $this->userRepository->save($user); |
||
| 102 | |||
| 103 | return (new RedirectResponse('list'))->redirect(); |
||
| 104 | } |
||
| 106 |