| Conditions | 12 | 
| Paths | 23 | 
| Total Lines | 85 | 
| Code Lines | 52 | 
| Lines | 4 | 
| Ratio | 4.71 % | 
| 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  | 
            ||
| 64 | public function forgotPassword($data, $form)  | 
            ||
| 65 |     { | 
            ||
| 66 | /** @var Controller $controller */  | 
            ||
| 67 | $controller = $form->getController();  | 
            ||
| 68 | |||
| 69 | // No need to protect against injections, LDAPService will ensure that this is safe  | 
            ||
| 70 | $login = trim($data['Login']);  | 
            ||
| 71 | |||
| 72 | $service = Injector::inst()->get(LDAPService::class);  | 
            ||
| 73 |         if (Email::is_valid_address($login)) { | 
            ||
| 74 |             if (Config::inst()->get(LDAPAuthenticator::class, 'allow_email_login') != 'yes') { | 
            ||
| 75 | $form->sessionMessage(  | 
            ||
| 76 | _t(  | 
            ||
| 77 | 'LDAPLoginForm.USERNAMEINSTEADOFEMAIL',  | 
            ||
| 78 | 'Please enter your username instead of your email to get a password reset link.'  | 
            ||
| 79 | ),  | 
            ||
| 80 | 'bad'  | 
            ||
| 81 | );  | 
            ||
| 82 |                 return $controller->redirect($controller->Link('lostpassword')); | 
            ||
| 83 | }  | 
            ||
| 84 | $userData = $service->getUserByEmail($login);  | 
            ||
| 85 |         } else { | 
            ||
| 86 | $userData = $service->getUserByUsername($login);  | 
            ||
| 87 | }  | 
            ||
| 88 | |||
| 89 | // Avoid information disclosure by displaying the same status,  | 
            ||
| 90 | // regardless whether the email address actually exists  | 
            ||
| 91 |         if (!isset($userData['objectguid'])) { | 
            ||
| 92 |             return $controller->redirect($controller->Link('passwordsent/') | 
            ||
| 93 | . urlencode($data['Login']));  | 
            ||
| 94 | }  | 
            ||
| 95 | |||
| 96 |         $member = Member::get()->filter('GUID', $userData['objectguid'])->limit(1)->first(); | 
            ||
| 97 | // User haven't been imported yet so do that now  | 
            ||
| 98 | View Code Duplication |         if (!($member && $member->exists())) { | 
            |
| 99 | $member = new Member();  | 
            ||
| 100 | $member->GUID = $userData['objectguid'];  | 
            ||
| 101 | }  | 
            ||
| 102 | |||
| 103 | // Update the users from LDAP so we are sure that the email is correct.  | 
            ||
| 104 | // This will also write the Member record.  | 
            ||
| 105 | $service->updateMemberFromLDAP($member);  | 
            ||
| 106 | |||
| 107 | // Allow vetoing forgot password requests  | 
            ||
| 108 |         $results = $this->extend('forgotPassword', $member); | 
            ||
| 109 |         if ($results && is_array($results) && in_array(false, $results, true)) { | 
            ||
| 110 |             return $controller->redirect('lostpassword'); | 
            ||
| 111 | }  | 
            ||
| 112 | |||
| 113 |         if ($member) { | 
            ||
| 114 | /** @see MemberLoginForm::forgotPassword */  | 
            ||
| 115 | $token = $member->generateAutologinTokenAndStoreHash();  | 
            ||
| 116 | $e = Email::create()  | 
            ||
| 117 |                 ->setSubject(_t('Member.SUBJECTPASSWORDRESET', 'Your password reset link', 'Email subject')) | 
            ||
| 118 |                 ->setHTMLTemplate('ForgotPasswordEmail') | 
            ||
| 119 | ->setData($member)  | 
            ||
| 120 | ->setData(['PasswordResetLink' => Security::getPasswordResetLink($member, $token)]);  | 
            ||
| 121 | $e->setTo($member->Email);  | 
            ||
| 122 | $e->send();  | 
            ||
| 123 |             return $controller->redirect($controller->Link('passwordsent/') . urlencode($data['Login'])); | 
            ||
| 124 |         } elseif ($data['Login']) { | 
            ||
| 125 | // Avoid information disclosure by displaying the same status,  | 
            ||
| 126 | // regardless whether the email address actually exists  | 
            ||
| 127 |             return $controller->redirect($controller->Link('passwordsent/') . urlencode($data['Login'])); | 
            ||
| 128 |         } else { | 
            ||
| 129 |             if (Config::inst()->get(LDAPAuthenticator::class, 'allow_email_login') === 'yes') { | 
            ||
| 130 | $form->sessionMessage(  | 
            ||
| 131 | _t(  | 
            ||
| 132 | 'LDAPLoginForm.ENTERUSERNAMEOREMAIL',  | 
            ||
| 133 | 'Please enter your username or your email address to get a password reset link.'  | 
            ||
| 134 | ),  | 
            ||
| 135 | 'bad'  | 
            ||
| 136 | );  | 
            ||
| 137 |             } else { | 
            ||
| 138 | $form->sessionMessage(  | 
            ||
| 139 | _t(  | 
            ||
| 140 | 'LDAPLoginForm.ENTERUSERNAME',  | 
            ||
| 141 | 'Please enter your username to get a password reset link.'  | 
            ||
| 142 | ),  | 
            ||
| 143 | 'bad'  | 
            ||
| 144 | );  | 
            ||
| 145 | }  | 
            ||
| 146 |             return $controller->redirect($controller->Link('lostpassword')); | 
            ||
| 147 | }  | 
            ||
| 148 | }  | 
            ||
| 149 | |||
| 221 |