Conditions | 7 |
Paths | 10 |
Total Lines | 68 |
Code Lines | 47 |
Lines | 22 |
Ratio | 32.35 % |
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 |
||
148 | public function onChangeEmail(IUser $user, $oldMailAddress) { |
||
149 | |||
150 | if ($oldMailAddress === $user->getEMailAddress() || |
||
151 | $user->getLastLogin() === 0) { |
||
152 | // Email didn't really change or user didn't login, |
||
153 | // so don't create activities and emails. |
||
154 | return; |
||
155 | } |
||
156 | |||
157 | $event = $this->activityManager->generateEvent(); |
||
158 | $event->setApp('settings') |
||
159 | ->setType('personal_settings') |
||
160 | ->setAffectedUser($user->getUID()); |
||
161 | |||
162 | $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
||
163 | |||
164 | $actor = $this->userSession->getUser(); |
||
165 | View Code Duplication | if ($actor instanceof IUser) { |
|
166 | if ($actor->getUID() !== $user->getUID()) { |
||
167 | $this->l = $this->languageFactory->get( |
||
168 | 'settings', |
||
169 | $this->config->getUserValue( |
||
170 | $user->getUID(), 'core', 'lang', |
||
171 | $this->config->getSystemValue('default_language', 'en') |
||
172 | ) |
||
173 | ); |
||
174 | |||
175 | $text = $this->l->t('%1$s changed your email address on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
||
176 | $event->setAuthor($actor->getUID()) |
||
177 | ->setSubject(Provider::EMAIL_CHANGED_BY, [$actor->getUID()]); |
||
178 | } else { |
||
179 | $text = $this->l->t('Your email address on %s was changed.', [$instanceUrl]); |
||
180 | $event->setAuthor($actor->getUID()) |
||
181 | ->setSubject(Provider::EMAIL_CHANGED_SELF); |
||
182 | } |
||
183 | } else { |
||
184 | $text = $this->l->t('Your email address on %s was changed by an administrator.', [$instanceUrl]); |
||
185 | $event->setSubject(Provider::EMAIL_CHANGED); |
||
186 | } |
||
187 | $this->activityManager->publish($event); |
||
188 | |||
189 | |||
190 | if ($oldMailAddress !== null) { |
||
191 | $template = $this->mailer->createEMailTemplate(); |
||
192 | $template->setMetaData('settings.EmailChanged', [ |
||
193 | 'displayname' => $user->getDisplayName(), |
||
194 | 'newEMailAddress' => $user->getEMailAddress(), |
||
195 | 'oldEMailAddress' => $oldMailAddress, |
||
196 | 'instanceUrl' => $instanceUrl, |
||
197 | ]); |
||
198 | $template->addHeader(); |
||
199 | $template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false); |
||
200 | $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
||
201 | if ($user->getEMailAddress()) { |
||
|
|||
202 | $template->addBodyText($this->l->t('The new email address is %s', [$user->getEMailAddress()])); |
||
203 | } |
||
204 | $template->addFooter(); |
||
205 | |||
206 | |||
207 | $message = $this->mailer->createMessage(); |
||
208 | $message->setTo([$oldMailAddress => $user->getDisplayName()]); |
||
209 | $message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
||
210 | $message->setBody($template->renderText(), 'text/plain'); |
||
211 | $message->setHtmlBody($template->renderHtml()); |
||
212 | |||
213 | $this->mailer->send($message); |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: