| Conditions | 15 |
| Paths | 294 |
| Total Lines | 72 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 56 | protected function authenticateMember($data, &$message, &$success) |
||
| 57 | { |
||
| 58 | // Default success to false |
||
| 59 | $success = false; |
||
| 60 | |||
| 61 | // Attempt to identify by temporary ID |
||
| 62 | $member = null; |
||
| 63 | $email = null; |
||
| 64 | if (!empty($data['tempid'])) { |
||
| 65 | // Find user by tempid, in case they are re-validating an existing session |
||
| 66 | $member = Member::member_from_tempid($data['tempid']); |
||
| 67 | if ($member) { |
||
| 68 | $email = $member->Email; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | // Otherwise, get email from posted value instead |
||
| 73 | /** @skipUpgrade */ |
||
| 74 | if (!$member && !empty($data['Email'])) { |
||
| 75 | $email = $data['Email']; |
||
| 76 | } |
||
| 77 | |||
| 78 | // Check default login (see Security::setDefaultAdmin()) |
||
| 79 | $asDefaultAdmin = $email === Security::default_admin_username(); |
||
| 80 | if ($asDefaultAdmin) { |
||
| 81 | // If logging is as default admin, ensure record is setup correctly |
||
| 82 | $member = Member::default_admin(); |
||
| 83 | $success = !$member->isLockedOut() && Security::check_default_admin($email, $data['Password']); |
||
| 84 | //protect against failed login |
||
| 85 | if ($success) { |
||
| 86 | return $member; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | // Attempt to identify user by email |
||
| 91 | if (!$member && $email) { |
||
| 92 | // Find user by email |
||
| 93 | $member = Member::get() |
||
| 94 | ->filter(Member::config()->unique_identifier_field, $email) |
||
| 95 | ->first(); |
||
| 96 | } |
||
| 97 | |||
| 98 | // Validate against member if possible |
||
| 99 | if ($member && !$asDefaultAdmin) { |
||
| 100 | $result = $member->checkPassword($data['Password']); |
||
| 101 | $success = $result->isValid(); |
||
| 102 | } else { |
||
| 103 | $result = ValidationResult::create()->addError(_t( |
||
| 104 | 'Member.ERRORWRONGCRED', |
||
| 105 | 'The provided details don\'t seem to be correct. Please try again.' |
||
| 106 | )); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Emit failure to member and form (if available) |
||
| 110 | if (!$success) { |
||
| 111 | if ($member) { |
||
| 112 | $member->registerFailedLogin(); |
||
| 113 | } |
||
| 114 | $message = implode("; ", array_map( |
||
| 115 | function ($message) { |
||
| 116 | return $message['message']; |
||
| 117 | }, |
||
| 118 | $result->getMessages() |
||
| 119 | )); |
||
| 120 | } else { |
||
| 121 | if ($member) { |
||
| 122 | $member->registerSuccessfulLogin(); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | return $member; |
||
| 127 | } |
||
| 128 | |||
| 204 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: