| Conditions | 5 |
| Paths | 9 |
| Total Lines | 55 |
| 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 |
||
| 45 | public function register(Request $request, Response $response) |
||
| 46 | { |
||
| 47 | if ($request->isPost()) { |
||
| 48 | $username = $request->getParam('username'); |
||
| 49 | $email = $request->getParam('email'); |
||
| 50 | $password = $request->getParam('password'); |
||
| 51 | |||
| 52 | $this->validator->request($request, [ |
||
| 53 | 'username' => V::length(3, 25)->alnum('_')->noWhitespace(), |
||
| 54 | 'email' => V::noWhitespace()->email(), |
||
| 55 | 'password' => [ |
||
| 56 | 'rules' => V::noWhitespace()->length(6, 25), |
||
| 57 | 'messages' => [ |
||
| 58 | 'length' => 'The password length must be between {{minValue}} and {{maxValue}} characters' |
||
| 59 | ] |
||
| 60 | ], |
||
| 61 | 'password_confirm' => [ |
||
| 62 | 'rules' => V::equals($password), |
||
| 63 | 'messages' => [ |
||
| 64 | 'equals' => 'Passwords don\'t match' |
||
| 65 | ] |
||
| 66 | ] |
||
| 67 | ]); |
||
| 68 | |||
| 69 | if ($this->auth->findByCredentials(['login' => $username])) { |
||
| 70 | $this->validator->addError('username', 'This username is already used.'); |
||
| 71 | } |
||
| 72 | |||
| 73 | if ($this->auth->findByCredentials(['login' => $email])) { |
||
| 74 | $this->validator->addError('email', 'This email is already used.'); |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($this->validator->isValid()) { |
||
| 78 | /** @var EloquentRole $role */ |
||
| 79 | $role = $this->auth->findRoleByName('User'); |
||
| 80 | |||
| 81 | $user = $this->auth->registerAndActivate([ |
||
| 82 | 'username' => $username, |
||
| 83 | 'email' => $email, |
||
| 84 | 'password' => $password, |
||
| 85 | 'permissions' => [ |
||
| 86 | 'user.delete' => 0 |
||
| 87 | ] |
||
| 88 | ]); |
||
| 89 | |||
| 90 | $role->users()->attach($user); |
||
| 91 | |||
| 92 | $this->flash('success', 'Your account has been created.'); |
||
| 93 | |||
| 94 | return $this->redirect($response, 'login'); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | return $this->render($response, 'auth/register.twig'); |
||
| 99 | } |
||
| 100 | |||
| 108 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.