| Conditions | 5 |
| Paths | 5 |
| Total Lines | 57 |
| 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 |
||
| 95 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 96 | { |
||
| 97 | $context = []; |
||
| 98 | $param = new RequestData($request); |
||
| 99 | |||
| 100 | $formParam = new AuthParam($param->posts()); |
||
| 101 | $context['param'] = $formParam; |
||
| 102 | |||
| 103 | if ($request->getMethod() === 'GET') { |
||
| 104 | return new TemplateResponse( |
||
| 105 | $this->template, |
||
| 106 | 'user/login', |
||
| 107 | $context |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | $validator = new AuthValidator($formParam, $this->lang); |
||
| 112 | if ($validator->validate() === false) { |
||
| 113 | $context['errors'] = $validator->getErrors(); |
||
| 114 | |||
| 115 | return new TemplateResponse( |
||
| 116 | $this->template, |
||
| 117 | 'user/login', |
||
| 118 | $context |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | $username = $formParam->getUsername(); |
||
| 123 | $password = $formParam->getPassword(); |
||
| 124 | |||
| 125 | $credentials = [ |
||
| 126 | 'username' => $username, |
||
| 127 | 'password' => $password, |
||
| 128 | ]; |
||
| 129 | |||
| 130 | try { |
||
| 131 | $this->authentication->login($credentials); |
||
| 132 | } catch (AuthenticationException $ex) { |
||
| 133 | $this->logger->error('Authentication error: {error}', [ |
||
| 134 | 'error' => $ex->getMessage() |
||
| 135 | ]); |
||
| 136 | |||
| 137 | $this->flash->setError('Authentication error. Please check your login and/or password.'); |
||
| 138 | |||
| 139 | return new TemplateResponse( |
||
| 140 | $this->template, |
||
| 141 | 'user/login', |
||
| 142 | $context |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | $returnUrl = $this->routeHelper->generateUrl('home'); |
||
| 147 | if ($param->get('next')) { |
||
| 148 | $returnUrl = $param->get('next'); |
||
| 149 | } |
||
| 150 | |||
| 151 | return new RedirectResponse($returnUrl); |
||
| 152 | } |
||
| 154 |