Conditions | 7 |
Paths | 36 |
Total Lines | 56 |
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 |
||
57 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
58 | { |
||
59 | $tree = $request->getAttribute('tree'); |
||
60 | $user = $request->getAttribute('user'); |
||
61 | $params = $request->getParsedBody(); |
||
62 | |||
63 | $contact_method = $params['contact_method']; |
||
64 | $email = $params['email']; |
||
65 | $language = $params['language']; |
||
66 | $real_name = $params['real_name']; |
||
67 | $password = $params['password']; |
||
68 | $time_zone = $params['timezone']; |
||
69 | $user_name = $params['user_name']; |
||
70 | $visible_online = $params['visible_online'] ?? ''; |
||
71 | |||
72 | // Change the password |
||
73 | if ($password !== '') { |
||
74 | $user->setPassword($password); |
||
75 | } |
||
76 | |||
77 | // Change the username |
||
78 | if ($user_name !== $user->userName()) { |
||
79 | if ($this->user_service->findByUserName($user_name) === null) { |
||
80 | $user->setUserName($user_name); |
||
81 | } else { |
||
82 | FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.')); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | // Change the email |
||
87 | if ($email !== $user->email()) { |
||
88 | if ($this->user_service->findByEmail($email) === null) { |
||
89 | $user->setEmail($email); |
||
90 | } else { |
||
91 | FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.')); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | $user |
||
96 | ->setRealName($real_name) |
||
97 | ->setPreference('contactmethod', $contact_method) |
||
98 | ->setPreference('language', $language) |
||
99 | ->setPreference('TIMEZONE', $time_zone) |
||
100 | ->setPreference('visibleonline', $visible_online); |
||
101 | |||
102 | if ($tree instanceof Tree) { |
||
103 | $rootid = $params['root_id']; |
||
104 | $tree->setUserPreference($user, 'rootid', $rootid); |
||
105 | } |
||
106 | |||
107 | // Switch to the new language now |
||
108 | Session::put('language', $language); |
||
109 | |||
110 | FlashMessages::addMessage(I18N::translate('The details for ā%sā have been updated.', e($user->username())), 'success'); |
||
111 | |||
112 | return redirect(route(HomePage::class, ['tree' => $tree->name()])); |
||
113 | } |
||
115 |