Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
20 | class YubikeyAuthenticator extends MemberAuthenticator |
||
21 | { |
||
22 | /** |
||
23 | * @var null|Form |
||
24 | */ |
||
25 | protected static $form; |
||
26 | |||
27 | /** |
||
28 | * @var Validate |
||
29 | */ |
||
30 | protected static $yubiService; |
||
31 | |||
32 | /** |
||
33 | * @inheritdoc |
||
34 | * |
||
35 | * @param array $data |
||
36 | * @param Form|null $form |
||
37 | * |
||
38 | * @return null|Member |
||
39 | */ |
||
40 | public static function authenticate($data, Form $form = null) |
||
69 | |||
70 | /** |
||
71 | * @param Controller $controller |
||
72 | * |
||
73 | * @return Form |
||
74 | */ |
||
75 | public static function get_login_form(Controller $controller) |
||
79 | |||
80 | /** |
||
81 | * Name of this authenticator |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | public static function get_name() |
||
89 | |||
90 | /** |
||
91 | * Update the member to forcefully enable YubiAuth |
||
92 | * Also, register the Yubikey to the member. |
||
93 | * Documentation: |
||
94 | * https://developers.yubico.com/yubikey-val/Getting_Started_Writing_Clients.html |
||
95 | * |
||
96 | * @param Member $member |
||
97 | * @param string $yubiString The Identifier String of the Yubikey |
||
98 | */ |
||
99 | private static function updateMember($member, $yubiString) |
||
112 | |||
113 | /** |
||
114 | * @param null|ValidationResult $validation |
||
115 | */ |
||
116 | private static function updateForm($validation = null) |
||
129 | |||
130 | /** |
||
131 | * Validate a member plus it's yubikey login. It compares the fingerprintt and after that, tries to validate the Yubikey string |
||
132 | * @param array $data |
||
133 | * @param Member $member |
||
134 | * @return null|Member |
||
135 | */ |
||
136 | private static function authenticate_yubikey($data, $member) |
||
159 | |||
160 | /** |
||
161 | * Check if the yubikey is unique and linked to the member trying to logon |
||
162 | * |
||
163 | * @param Member $member |
||
164 | * @param string $yubiFingerprint |
||
165 | * @return boolean |
||
166 | */ |
||
167 | private static function validateYubikey($member, $yubiFingerprint) |
||
189 | |||
190 | |||
191 | /** |
||
192 | * Handle login if the user did not enter a Yubikey string. |
||
193 | * Will break out and return NULL if the member should use their Yubikey |
||
194 | * |
||
195 | * @param Member $member |
||
196 | * @return null|Member |
||
197 | */ |
||
198 | private static function authenticate_noyubikey($member) |
||
208 | |||
209 | /** |
||
210 | * Check if a member is allowed to login without a yubikey |
||
211 | * |
||
212 | * @param Member $member |
||
213 | * @return bool|Member |
||
214 | */ |
||
215 | private static function checkNoYubiLogins($member) |
||
229 | |||
230 | /** |
||
231 | * Check if the member is allowed login after so many days of not using a yubikey |
||
232 | * |
||
233 | * @param Member $member |
||
234 | * @return bool|Member |
||
235 | */ |
||
236 | private static function checkNoYubiDays($member) |
||
256 | } |
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.