Conditions | 17 |
Paths | 654 |
Total Lines | 64 |
Code Lines | 37 |
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 |
||
31 | protected static function authenticate_member($data, $form, &$success) { |
||
32 | // Default success to false |
||
33 | $success = false; |
||
34 | |||
35 | // Attempt to identify by temporary ID |
||
36 | $member = null; |
||
37 | $email = null; |
||
38 | if(!empty($data['tempid'])) { |
||
39 | // Find user by tempid, in case they are re-validating an existing session |
||
40 | $member = Member::member_from_tempid($data['tempid']); |
||
41 | if($member) $email = $member->Email; |
||
42 | } |
||
43 | |||
44 | // Otherwise, get email from posted value instead |
||
45 | if(!$member && !empty($data['Email'])) { |
||
46 | $email = $data['Email']; |
||
47 | } |
||
48 | |||
49 | // Check default login (see Security::setDefaultAdmin()) |
||
|
|||
50 | $asDefaultAdmin = $email === Security::default_admin_username(); |
||
51 | if($asDefaultAdmin) { |
||
52 | // If logging is as default admin, ensure record is setup correctly |
||
53 | $member = Member::default_admin(); |
||
54 | $success = !$member->isLockedOut() && Security::check_default_admin($email, $data['Password']); |
||
55 | //protect against failed login |
||
56 | if($success) { |
||
57 | return $member; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | // Attempt to identify user by email |
||
62 | if(!$member && $email) { |
||
63 | // Find user by email |
||
64 | $member = Member::get() |
||
65 | ->filter(Member::config()->unique_identifier_field, $email) |
||
66 | ->first(); |
||
67 | } |
||
68 | |||
69 | // Validate against member if possible |
||
70 | if($member && !$asDefaultAdmin) { |
||
71 | $result = $member->checkPassword($data['Password']); |
||
72 | $success = $result->valid(); |
||
73 | } elseif (!$asDefaultAdmin) { |
||
74 | // spoof a login attempt |
||
75 | $member = Member::create(); |
||
76 | $member->Email = $email; |
||
77 | $member->{Member::config()->unique_identifier_field} = $data['Password'] . '-wrong'; |
||
78 | $member->PasswordEncryption = 'none'; |
||
79 | $result = $member->checkPassword($data['Password']); |
||
80 | $member = null; |
||
81 | } else { |
||
82 | $result = new ValidationResult(false, _t('Member.ERRORWRONGCRED')); |
||
83 | } |
||
84 | |||
85 | // Emit failure to member and form (if available) |
||
86 | if(!$success) { |
||
87 | if($member) $member->registerFailedLogin(); |
||
88 | if($form) $form->sessionMessage($result->message(), 'bad'); |
||
89 | } else { |
||
90 | if($member) $member->registerSuccessfulLogin(); |
||
91 | } |
||
92 | |||
93 | return $member; |
||
94 | } |
||
95 | |||
208 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.