| Conditions | 14 | 
| Paths | 22 | 
| Total Lines | 76 | 
| Code Lines | 45 | 
| Lines | 28 | 
| Ratio | 36.84 % | 
| 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 | ||
| 83 | public static function authenticate($data, Form $form = null) | ||
| 84 |     { | ||
| 85 |         $service = Injector::inst()->get('SilverStripe\\ActiveDirectory\\Services\\LDAPService'); | ||
| 86 | $login = trim($data['Login']); | ||
| 87 |         if (Email::is_valid_address($login)) { | ||
| 88 |             if (Config::inst()->get('SilverStripe\\ActiveDirectory\\Authenticators\\LDAPAuthenticator', 'allow_email_login') != 'yes') { | ||
| 89 | $form->sessionMessage( | ||
| 90 | _t( | ||
| 91 | 'LDAPAuthenticator.PLEASEUSEUSERNAME', | ||
| 92 | 'Please enter your username instead of your email to log in.' | ||
| 93 | ), | ||
| 94 | 'bad' | ||
| 95 | ); | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | $username = $service->getUsernameByEmail($login); | ||
| 100 | |||
| 101 | // No user found with this email. | ||
| 102 | View Code Duplication |             if (!$username) { | |
| 103 |                 if (Config::inst()->get('SilverStripe\\ActiveDirectory\\Authenticators\\LDAPAuthenticator', 'fallback_authenticator') === 'yes') { | ||
| 104 | $fallbackMember = self::fallback_authenticate($data, $form); | ||
| 105 |                     if ($fallbackMember) { | ||
| 106 | return $fallbackMember; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 |                 $form->sessionMessage(_t('LDAPAuthenticator.INVALIDCREDENTIALS', 'Invalid credentials'), 'bad'); | ||
| 111 | return; | ||
| 112 | } | ||
| 113 |         } else { | ||
| 114 | $username = $login; | ||
| 115 | } | ||
| 116 | |||
| 117 | $result = $service->authenticate($username, $data['Password']); | ||
| 118 | $success = $result['success'] === true; | ||
| 119 | View Code Duplication |         if (!$success) { | |
| 120 |             if (Config::inst()->get('SilverStripe\\ActiveDirectory\\Authenticators\\LDAPAuthenticator', 'fallback_authenticator') === 'yes') { | ||
| 121 | $fallbackMember = self::fallback_authenticate($data, $form); | ||
| 122 |                 if ($fallbackMember) { | ||
| 123 | return $fallbackMember; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 |             if ($form) { | ||
| 128 | $form->sessionMessage($result['message'], 'bad'); | ||
| 129 | } | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | $data = $service->getUserByUsername($result['identity']); | ||
| 134 |         if (!$data) { | ||
| 135 |             if ($form) { | ||
| 136 | $form->sessionMessage( | ||
| 137 |                     _t('LDAPAuthenticator.PROBLEMFINDINGDATA', 'There was a problem retrieving your user data'), | ||
| 138 | 'bad' | ||
| 139 | ); | ||
| 140 | } | ||
| 141 | return; | ||
| 142 | } | ||
| 143 | |||
| 144 | // LDAPMemberExtension::memberLoggedIn() will update any other AD attributes mapped to Member fields | ||
| 145 |         $member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); | ||
| 146 | View Code Duplication |         if (!($member && $member->exists())) { | |
| 147 | $member = new Member(); | ||
| 148 | $member->GUID = $data['objectguid']; | ||
| 149 | } | ||
| 150 | |||
| 151 | // Update the users from LDAP so we are sure that the email is correct. | ||
| 152 | // This will also write the Member record. | ||
| 153 | $service->updateMemberFromLDAP($member); | ||
| 154 | |||
| 155 |         Session::clear('BackURL'); | ||
| 156 | |||
| 157 | return $member; | ||
| 158 | } | ||
| 159 | |||
| 182 | 
This check marks private properties in classes that are never used. Those properties can be removed.