Conditions | 6 |
Paths | 7 |
Total Lines | 63 |
Code Lines | 44 |
Lines | 22 |
Ratio | 34.92 % |
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 |
||
78 | public function onChangePassword($uid) { |
||
79 | $user = $this->userManager->get($uid); |
||
80 | |||
81 | if (!$user instanceof IUser || $user->getLastLogin() === 0) { |
||
82 | // User didn't login, so don't create activities and emails. |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | $event = $this->activityManager->generateEvent(); |
||
87 | $event->setApp('settings') |
||
88 | ->setType('personal_settings') |
||
89 | ->setAffectedUser($user->getUID()); |
||
90 | |||
91 | $instanceUrl = $this->urlGenerator->getAbsoluteURL('/'); |
||
92 | |||
93 | $actor = $this->userSession->getUser(); |
||
94 | View Code Duplication | if ($actor instanceof IUser) { |
|
95 | if ($actor->getUID() !== $user->getUID()) { |
||
96 | $this->l = $this->languageFactory->get( |
||
97 | 'settings', |
||
98 | $this->config->getUserValue( |
||
99 | $user->getUID(), 'core', 'lang', |
||
100 | $this->config->getSystemValue('default_language', 'en') |
||
101 | ) |
||
102 | ); |
||
103 | |||
104 | $text = $this->l->t('%1$s changed your password on %2$s.', [$actor->getDisplayName(), $instanceUrl]); |
||
105 | $event->setAuthor($actor->getUID()) |
||
106 | ->setSubject(Provider::PASSWORD_CHANGED_BY, [$actor->getUID()]); |
||
107 | } else { |
||
108 | $text = $this->l->t('Your password on %s was changed.', [$instanceUrl]); |
||
109 | $event->setAuthor($actor->getUID()) |
||
110 | ->setSubject(Provider::PASSWORD_CHANGED_SELF); |
||
111 | } |
||
112 | } else { |
||
113 | $text = $this->l->t('Your password on %s was reset by an administrator.', [$instanceUrl]); |
||
114 | $event->setSubject(Provider::PASSWORD_RESET); |
||
115 | } |
||
116 | |||
117 | $this->activityManager->publish($event); |
||
118 | |||
119 | if ($user->getEMailAddress() !== null) { |
||
120 | $template = $this->mailer->createEMailTemplate(); |
||
121 | $template->setMetaData('settings.PasswordChanged', [ |
||
122 | 'displayname' => $user->getDisplayName(), |
||
123 | 'emailAddress' => $user->getEMailAddress(), |
||
124 | 'instanceUrl' => $instanceUrl, |
||
125 | ]); |
||
126 | $template->addHeader(); |
||
127 | $template->addHeading($this->l->t('Password changed for %s', [$user->getDisplayName()]), false); |
||
128 | $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); |
||
129 | $template->addFooter(); |
||
130 | |||
131 | |||
132 | $message = $this->mailer->createMessage(); |
||
133 | $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); |
||
134 | $message->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); |
||
135 | $message->setBody($template->renderText(), 'text/plain'); |
||
136 | $message->setHtmlBody($template->renderHtml()); |
||
137 | |||
138 | $this->mailer->send($message); |
||
139 | } |
||
140 | } |
||
141 | |||
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: