| Conditions | 12 |
| Paths | 98 |
| Total Lines | 57 |
| Code Lines | 31 |
| 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 |
||
| 58 | protected function authenticateMember($data, &$message, &$success, $member = null) |
||
| 59 | { |
||
| 60 | // Default success to false |
||
| 61 | $success = false; |
||
| 62 | $email = !empty($data['Email']) ? $data['Email'] : null ; |
||
| 63 | |||
| 64 | // Check default login (see Security::setDefaultAdmin()) |
||
| 65 | $asDefaultAdmin = $email === Security::default_admin_username(); |
||
| 66 | if ($asDefaultAdmin) { |
||
| 67 | // If logging is as default admin, ensure record is setup correctly |
||
| 68 | $member = Member::default_admin(); |
||
| 69 | $success = $member->canLogin()->isValid() && Security::check_default_admin($email, $data['Password']); |
||
| 70 | //protect against failed login |
||
| 71 | if ($success) { |
||
| 72 | return $member; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | // Attempt to identify user by email |
||
| 77 | if (!$member && $email) { |
||
| 78 | // Find user by email |
||
| 79 | /** @var Member $member */ |
||
| 80 | $member = Member::get() |
||
| 81 | ->filter([Member::config()->get('unique_identifier_field') => $email]) |
||
| 82 | ->first(); |
||
| 83 | } |
||
| 84 | |||
| 85 | // Validate against member if possible |
||
| 86 | if ($member && !$asDefaultAdmin) { |
||
| 87 | $result = $member->checkPassword($data['Password']); |
||
| 88 | $success = $result->isValid(); |
||
| 89 | } else { |
||
| 90 | $result = ValidationResult::create()->addError(_t( |
||
| 91 | 'SilverStripe\\Security\\Member.ERRORWRONGCRED', |
||
| 92 | 'The provided details don\'t seem to be correct. Please try again.' |
||
| 93 | )); |
||
| 94 | } |
||
| 95 | |||
| 96 | // Emit failure to member and form (if available) |
||
| 97 | if (!$success) { |
||
| 98 | if ($member) { |
||
| 99 | $member->registerFailedLogin(); |
||
| 100 | } |
||
| 101 | $message = implode("; ", array_map( |
||
| 102 | function ($message) { |
||
| 103 | return $message['message']; |
||
| 104 | }, |
||
| 105 | $result->getMessages() |
||
| 106 | )); |
||
| 107 | } else { |
||
| 108 | if ($member) { // How can success be true and member false? |
||
| 109 | $member->registerSuccessfulLogin(); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return $member; |
||
| 114 | } |
||
| 115 | |||
| 186 |
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: