Conditions | 17 |
Paths | 14 |
Total Lines | 82 |
Code Lines | 56 |
Lines | 17 |
Ratio | 20.73 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
28 | public static function authenticate($data, Form $form = null) |
||
29 | { |
||
30 | Config::inst()->update('Security', 'login_recording', false); // Disable login_recording for this auth. |
||
31 | // First, let's see if we know the member |
||
32 | $member = parent::authenticate($data, $form); |
||
33 | Config::inst()->update('Security', 'login_recording', true); |
||
34 | $validationError = ValidationResult::create(false, |
||
35 | _t('YubikeyAuthenticator.ERRORYUBIKEY', 'Yubikey authentication error')); |
||
36 | if ($member && $member instanceof Member) { |
||
37 | // If we know the member, and it's YubiAuth enabled, continue. |
||
38 | if ($member && |
||
39 | ($member->YubiAuthEnabled || $data['Yubikey'] !== '') |
||
40 | ) { |
||
41 | $data['Yubikey'] = strtolower($data['Yubikey']); |
||
42 | $yubiCode = QwertyConvertor::convertString($data['Yubikey']); |
||
43 | $yubiFingerprint = substr($yubiCode, 0, -32); |
||
44 | // If the member has a yubikey ID set, compare it to the fingerprint. |
||
45 | if ($member->Yubikey && strpos($yubiFingerprint, $member->Yubikey) !== 0) { |
||
46 | self::updateForm($validationError, $form); |
||
47 | |||
48 | return null; // Yubikey id doesn't match the member. |
||
49 | } |
||
50 | $clientID = YUBIAUTH_CLIENTID; |
||
51 | $apiKey = YUBIAUTH_APIKEY; |
||
52 | $service = new \Yubikey\Validate($apiKey, $clientID); |
||
53 | if ($url = self::config()->get('AuthURL')) { |
||
54 | $service->setHost($url); |
||
55 | } |
||
56 | $result = $service->check($yubiCode); |
||
57 | |||
58 | if ($result->success() === true) { |
||
59 | self::updateMember($member, $yubiFingerprint); |
||
60 | if ($member) { |
||
61 | $member->registerSuccessfulLogin(); |
||
62 | $member->MaxNoYubiLogins = 0; |
||
63 | $member->write(); |
||
64 | } |
||
65 | |||
66 | return $member; |
||
67 | } else { |
||
68 | self::updateForm($validationError, $form); |
||
69 | |||
70 | return null; |
||
71 | } |
||
72 | } elseif (!$member->YubiAuthEnabled) { // We do not have to check the YubiAuth for now. |
||
73 | $member->NoYubikeyCount += 1; |
||
74 | $member->write(); |
||
75 | $maxNoYubi = Config::inst()->get('YubikeyAuthenticator', 'MaxNoYubiLogin'); |
||
76 | View Code Duplication | if ($maxNoYubi > 0 && $maxNoYubi <= $member->NoYubikeyCount) { |
|
|
|||
77 | $validationError = ValidationResult::create(false, |
||
78 | _t('YubikeyAuthenticator.ERRORMAXYUBIKEY', 'Maximum login without yubikey exceeded')); |
||
79 | self::updateForm($validationError, $form); |
||
80 | $member->registerFailedLogin(); |
||
81 | |||
82 | return null; |
||
83 | } |
||
84 | $date1 = new DateTime($member->Created); |
||
85 | $date2 = new DateTime(date('Y-m-d')); |
||
86 | |||
87 | $diff = $date2->diff($date1)->format("%a"); |
||
88 | $maxNoYubiDays = Config::inst()->get('YubikeyAuthenticator', 'MaxNoYubiLoginDays'); |
||
89 | |||
90 | View Code Duplication | if ($maxNoYubiDays > 0 && $diff >= $maxNoYubiDays) { |
|
91 | $validationError = ValidationResult::create(false, |
||
92 | _t('YubikeyAuthenticator.ERRORMAXYUBIKEYDAYS', 'Maximum days without yubikey exceeded')); |
||
93 | self::updateForm($validationError, $form); |
||
94 | $member->registerFailedLogin(); |
||
95 | |||
96 | return null; |
||
97 | |||
98 | } |
||
99 | |||
100 | return $member; |
||
101 | } |
||
102 | } |
||
103 | if ($member) { |
||
104 | $member->registerFailedLogin(); |
||
105 | } |
||
106 | self::updateForm($validationError, $form); |
||
107 | |||
108 | return null; |
||
109 | } |
||
110 | |||
158 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.