Conditions | 12 |
Paths | 66 |
Total Lines | 58 |
Code Lines | 32 |
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, &$result = null, $member = null) |
||
59 | { |
||
60 | // Default success to false |
||
61 | $email = !empty($data['Email']) ? $data['Email'] : null; |
||
62 | $result = new ValidationResult(); |
||
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 = Security::check_default_admin($email, $data['Password']); |
||
70 | $result = $member->canLogIn(); |
||
71 | //protect against failed login |
||
72 | if ($success && $result->isValid()) { |
||
73 | return $member; |
||
74 | } else { |
||
75 | $result->addError(_t( |
||
76 | 'SilverStripe\\Security\\Member.ERRORWRONGCRED', |
||
77 | "The provided details don't seem to be correct. Please try again." |
||
78 | )); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | // Attempt to identify user by email |
||
83 | if (!$member && $email) { |
||
84 | // Find user by email |
||
85 | /** @var Member $member */ |
||
86 | $member = Member::get() |
||
87 | ->filter([Member::config()->get('unique_identifier_field') => $email]) |
||
88 | ->first(); |
||
89 | } |
||
90 | |||
91 | // Validate against member if possible |
||
92 | if ($member && !$asDefaultAdmin) { |
||
93 | $result = $member->checkPassword($data['Password']); |
||
94 | } |
||
95 | |||
96 | // Emit failure to member and form (if available) |
||
97 | if (!$result->isValid()) { |
||
98 | if ($member) { |
||
99 | $member->registerFailedLogin(); |
||
100 | } |
||
101 | } else { |
||
102 | if ($member) { |
||
103 | $member->registerSuccessfulLogin(); |
||
104 | } else { |
||
105 | // A non-existing member occurred. This will make the result "valid" so let's invalidate |
||
106 | $result->addError(_t( |
||
107 | 'SilverStripe\\Security\\Member.ERRORWRONGCRED', |
||
108 | "The provided details don't seem to be correct. Please try again." |
||
109 | )); |
||
110 | $member = null; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | return $member; |
||
115 | } |
||
116 | |||
200 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: