| Conditions | 12 |
| Paths | 23 |
| Total Lines | 90 |
| Lines | 4 |
| Ratio | 4.44 % |
| 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 |
||
| 111 | public function forgotPassword($data) |
||
| 112 | { |
||
| 113 | // Passing true returns a float rather than a string |
||
| 114 | $startTime = microtime(true); |
||
| 115 | // No need to protect against injections, LDAPService will ensure that this is safe |
||
| 116 | $login = trim($data['Login']); |
||
| 117 | |||
| 118 | $service = Injector::inst()->get('LDAPService'); |
||
| 119 | if (Email::validEmailAddress($login)) { |
||
| 120 | if (Config::inst()->get('LDAPAuthenticator', 'allow_email_login')!='yes') { |
||
| 121 | $this->sessionMessage( |
||
| 122 | _t( |
||
| 123 | 'LDAPLoginForm.USERNAMEINSTEADOFEMAIL', |
||
| 124 | 'Please enter your username instead of your email to get a password reset link.' |
||
| 125 | ), |
||
| 126 | 'bad' |
||
| 127 | ); |
||
| 128 | $this->consistentResponseTime($startTime); |
||
| 129 | $this->controller->redirect($this->controller->Link('lostpassword')); |
||
| 130 | return; |
||
| 131 | } |
||
| 132 | $userData = $service->getUserByEmail($login); |
||
| 133 | } else { |
||
| 134 | $userData = $service->getUserByUsername($login); |
||
| 135 | } |
||
| 136 | |||
| 137 | // Avoid information disclosure by displaying the same status, |
||
| 138 | // regardless whether the email address actually exists |
||
| 139 | if (!isset($userData['objectguid'])) { |
||
| 140 | $this->consistentResponseTime($startTime); |
||
| 141 | return $this->controller->redirect($this->controller->Link('passwordsent/') |
||
| 142 | . urlencode($data['Login'])); |
||
| 143 | } |
||
| 144 | |||
| 145 | $member = Member::get()->filter('GUID', $userData['objectguid'])->limit(1)->first(); |
||
| 146 | // User haven't been imported yet so do that now |
||
| 147 | View Code Duplication | if (!($member && $member->exists())) { |
|
| 148 | $member = new Member(); |
||
| 149 | $member->GUID = $userData['objectguid']; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Update the users from LDAP so we are sure that the email is correct. |
||
| 153 | // This will also write the Member record. |
||
| 154 | $service->updateMemberFromLDAP($member); |
||
| 155 | |||
| 156 | // Allow vetoing forgot password requests |
||
| 157 | $results = $this->extend('forgotPassword', $member); |
||
| 158 | if ($results && is_array($results) && in_array(false, $results, true)) { |
||
| 159 | $this->consistentResponseTime($startTime); |
||
| 160 | return $this->controller->redirect($this->ldapSecController->Link('lostpassword')); |
||
| 161 | } |
||
| 162 | |||
| 163 | if ($member) { |
||
| 164 | $token = $member->generateAutologinTokenAndStoreHash(); |
||
| 165 | $e = Member_ForgotPasswordEmail::create(); |
||
| 166 | $e->populateTemplate($member); |
||
| 167 | $e->populateTemplate([ |
||
| 168 | 'PasswordResetLink' => LDAPSecurityController::getPasswordResetLink($member, $token) |
||
| 169 | ]); |
||
| 170 | $e->setTo($member->Email); |
||
| 171 | $e->send(); |
||
| 172 | $this->consistentResponseTime($startTime); |
||
| 173 | $this->controller->redirect($this->controller->Link('passwordsent/') . urlencode($data['Login'])); |
||
| 174 | } elseif ($data['Login']) { |
||
| 175 | // Avoid information disclosure by displaying the same status, |
||
| 176 | // regardless whether the email address actually exists |
||
| 177 | $this->consistentResponseTime($startTime); |
||
| 178 | $this->controller->redirect($this->controller->Link('passwordsent/') . urlencode($data['Login'])); |
||
| 179 | } else { |
||
| 180 | if (Config::inst()->get('LDAPAuthenticator', 'allow_email_login')==='yes') { |
||
| 181 | $this->sessionMessage( |
||
| 182 | _t( |
||
| 183 | 'LDAPLoginForm.ENTERUSERNAMEOREMAIL', |
||
| 184 | 'Please enter your username or your email address to get a password reset link.' |
||
| 185 | ), |
||
| 186 | 'bad' |
||
| 187 | ); |
||
| 188 | } else { |
||
| 189 | $this->sessionMessage( |
||
| 190 | _t( |
||
| 191 | 'LDAPLoginForm.ENTERUSERNAME', |
||
| 192 | 'Please enter your username to get a password reset link.' |
||
| 193 | ), |
||
| 194 | 'bad' |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | $this->consistentResponseTime($startTime); |
||
| 198 | $this->controller->redirect($this->controller->Link('lostpassword')); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 223 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.