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