| 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 | ||
| 69 | public static function authenticate($data, Form $form = null) | ||
| 70 |     { | ||
| 71 |         $service = Injector::inst()->get('LDAPService'); | ||
| 72 | $login = trim($data['Login']); | ||
| 73 |         if (Email::validEmailAddress($login)) { | ||
| 74 |             if (Config::inst()->get('LDAPAuthenticator', 'allow_email_login')!='yes') { | ||
| 75 | $form->sessionMessage( | ||
| 76 | _t( | ||
| 77 | 'LDAPAuthenticator.PLEASEUSEUSERNAME', | ||
| 78 | 'Please enter your username instead of your email to log in.' | ||
| 79 | ), | ||
| 80 | 'bad' | ||
| 81 | ); | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | $username = $service->getUsernameByEmail($login); | ||
| 86 | |||
| 87 | // No user found with this email. | ||
| 88 | View Code Duplication |             if (!$username) { | |
| 89 |                 if (Config::inst()->get('LDAPAuthenticator', 'fallback_authenticator') === 'yes') { | ||
| 90 | $fallbackMember = self::fallback_authenticate($data, $form); | ||
| 91 |                     if ($fallbackMember) { | ||
| 92 | return $fallbackMember; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 |                 $form->sessionMessage(_t('LDAPAuthenticator.INVALIDCREDENTIALS', 'Invalid credentials'), 'bad'); | ||
| 97 | return; | ||
| 98 | } | ||
| 99 |         } else { | ||
| 100 | $username = $login; | ||
| 101 | } | ||
| 102 | |||
| 103 | $result = $service->authenticate($username, $data['Password']); | ||
| 104 | $success = $result['success'] === true; | ||
| 105 | View Code Duplication |         if (!$success) { | |
| 106 |             if (Config::inst()->get('LDAPAuthenticator', 'fallback_authenticator') === 'yes') { | ||
| 107 | $fallbackMember = self::fallback_authenticate($data, $form); | ||
| 108 |                 if ($fallbackMember) { | ||
| 109 | return $fallbackMember; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 |             if ($form) { | ||
| 114 | $form->sessionMessage($result['message'], 'bad'); | ||
| 115 | } | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | $data = $service->getUserByUsername($result['identity']); | ||
| 120 |         if (!$data) { | ||
| 121 |             if ($form) { | ||
| 122 | $form->sessionMessage( | ||
| 123 |                     _t('LDAPAuthenticator.PROBLEMFINDINGDATA', 'There was a problem retrieving your user data'), | ||
| 124 | 'bad' | ||
| 125 | ); | ||
| 126 | } | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | // LDAPMemberExtension::memberLoggedIn() will update any other AD attributes mapped to Member fields | ||
| 131 |         $member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); | ||
| 132 | View Code Duplication |         if (!($member && $member->exists())) { | |
| 133 | $member = new Member(); | ||
| 134 | $member->GUID = $data['objectguid']; | ||
| 135 | } | ||
| 136 | |||
| 137 | // Update the users from LDAP so we are sure that the email is correct. | ||
| 138 | // This will also write the Member record. | ||
| 139 | $service->updateMemberFromLDAP($member); | ||
| 140 | |||
| 141 |         Session::clear('BackURL'); | ||
| 142 | |||
| 143 | return $member; | ||
| 144 | } | ||
| 145 | |||
| 163 | 
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.