| Conditions | 15 | 
| Paths | 36 | 
| Total Lines | 83 | 
| Code Lines | 44 | 
| Lines | 4 | 
| Ratio | 4.82 % | 
| 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  | 
            ||
| 88 | public function authenticate(array $data, HTTPRequest $request, ValidationResult &$result = null)  | 
            ||
| 89 |     { | 
            ||
| 90 | $result = $result ?: ValidationResult::create();  | 
            ||
| 91 | /** @var LDAPService $service */  | 
            ||
| 92 | $service = Injector::inst()->get(LDAPService::class);  | 
            ||
| 93 | $login = trim($data['Login']);  | 
            ||
| 94 |         if (Email::is_valid_address($login)) { | 
            ||
| 95 |             if (Config::inst()->get(self::class, 'allow_email_login') != 'yes') { | 
            ||
| 96 | $result->addError(  | 
            ||
| 97 | _t(  | 
            ||
| 98 | 'LDAPAuthenticator.PLEASEUSEUSERNAME',  | 
            ||
| 99 | 'Please enter your username instead of your email to log in.'  | 
            ||
| 100 | )  | 
            ||
| 101 | );  | 
            ||
| 102 | return null;  | 
            ||
| 103 | }  | 
            ||
| 104 | $username = $service->getUsernameByEmail($login);  | 
            ||
| 105 | |||
| 106 | // No user found with this email.  | 
            ||
| 107 |             if (!$username) { | 
            ||
| 108 |                 if (Config::inst()->get(self::class, 'fallback_authenticator') === 'yes') { | 
            ||
| 109 |                     if ($fallbackMember = $this->fallbackAuthenticate($data, $request)) { | 
            ||
| 110 |                         { | 
            ||
| 111 | return $fallbackMember;  | 
            ||
| 112 | }  | 
            ||
| 113 | }  | 
            ||
| 114 | }  | 
            ||
| 115 | |||
| 116 |                 $result->addError(_t('LDAPAuthenticator.INVALIDCREDENTIALS', 'Invalid credentials')); | 
            ||
| 117 | return null;  | 
            ||
| 118 | }  | 
            ||
| 119 |         } else { | 
            ||
| 120 | $username = $login;  | 
            ||
| 121 | }  | 
            ||
| 122 | |||
| 123 | $serviceAuthenticationResult = $service->authenticate($username, $data['Password']);  | 
            ||
| 124 | $success = $serviceAuthenticationResult['success'] === true;  | 
            ||
| 125 | |||
| 126 |         if (!$success) { | 
            ||
| 127 | /*  | 
            ||
| 128 | * Try the fallback method if admin or it failed for anything other than invalid credentials  | 
            ||
| 129 | * This is to avoid having an unhandled exception error thrown by PasswordEncryptor::create_for_algorithm()  | 
            ||
| 130 | */  | 
            ||
| 131 |             if (Config::inst()->get(self::class, 'fallback_authenticator') === 'yes') { | 
            ||
| 132 | if (!in_array($serviceAuthenticationResult['code'], [Result::FAILURE_CREDENTIAL_INVALID])  | 
            ||
| 133 | || $username === 'admin'  | 
            ||
| 134 |                 ) { | 
            ||
| 135 |                     if ($fallbackMember = $this->fallbackAuthenticate($data, $request)) { | 
            ||
| 136 | return $fallbackMember;  | 
            ||
| 137 | }  | 
            ||
| 138 | }  | 
            ||
| 139 | }  | 
            ||
| 140 | |||
| 141 | $result->addError($serviceAuthenticationResult['message']);  | 
            ||
| 142 | |||
| 143 | return null;  | 
            ||
| 144 | }  | 
            ||
| 145 | $data = $service->getUserByUsername($serviceAuthenticationResult['identity']);  | 
            ||
| 146 |         if (!$data) { | 
            ||
| 147 | $result->addError(  | 
            ||
| 148 | _t(  | 
            ||
| 149 | 'LDAPAuthenticator.PROBLEMFINDINGDATA',  | 
            ||
| 150 | 'There was a problem retrieving your user data'  | 
            ||
| 151 | )  | 
            ||
| 152 | );  | 
            ||
| 153 | return null;  | 
            ||
| 154 | }  | 
            ||
| 155 | |||
| 156 | // LDAPMemberExtension::memberLoggedIn() will update any other AD attributes mapped to Member fields  | 
            ||
| 157 |         $member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); | 
            ||
| 158 | View Code Duplication |         if (!($member && $member->exists())) { | 
            |
| 159 | $member = new Member();  | 
            ||
| 160 | $member->GUID = $data['objectguid'];  | 
            ||
| 161 | }  | 
            ||
| 162 | |||
| 163 | // Update the users from LDAP so we are sure that the email is correct.  | 
            ||
| 164 | // This will also write the Member record.  | 
            ||
| 165 | $service->updateMemberFromLDAP($member, $data, false);  | 
            ||
| 166 | |||
| 167 |         $request->getSession()->clear('BackURL'); | 
            ||
| 168 | |||
| 169 | return $member;  | 
            ||
| 170 | }  | 
            ||
| 171 | |||
| 219 | 
This check marks private properties in classes that are never used. Those properties can be removed.