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