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 |
||
27 | class LDAPAuthenticator implements Authenticator |
||
28 | { |
||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $name = 'LDAP'; |
||
|
|||
33 | |||
34 | /** |
||
35 | * Set to 'yes' to indicate if this module should look up usernames in LDAP by matching the email addresses. |
||
36 | * |
||
37 | * CAVEAT #1: only set to 'yes' for systems that enforce email uniqueness. |
||
38 | * Otherwise only the first LDAP user with matching email will be accessible. |
||
39 | * |
||
40 | * CAVEAT #2: this is untested for systems that use LDAP with principal style usernames (i.e. [email protected]). |
||
41 | * The system will misunderstand emails for usernames with uncertain outcome. |
||
42 | * |
||
43 | * @var string 'no' or 'yes' |
||
44 | */ |
||
45 | private static $allow_email_login = 'no'; |
||
46 | |||
47 | /** |
||
48 | * Set to 'yes' to fallback login attempts to {@link $fallback_authenticator}. |
||
49 | * This will occur if LDAP fails to authenticate the user. |
||
50 | * |
||
51 | * @var string 'no' or 'yes' |
||
52 | */ |
||
53 | private static $fallback_authenticator = 'no'; |
||
54 | |||
55 | /** |
||
56 | * The class of {@link Authenticator} to use as the fallback authenticator. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private static $fallback_authenticator_class = MemberAuthenticator::class; |
||
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | public static function get_name() |
||
69 | |||
70 | /** |
||
71 | * @param Controller $controller |
||
72 | * @return LDAPLoginForm |
||
73 | */ |
||
74 | public static function get_login_form(Controller $controller) |
||
78 | |||
79 | /** |
||
80 | * Performs the login, but will also create and sync the Member record on-the-fly, if not found. |
||
81 | * |
||
82 | * @param array $data |
||
83 | * @param HTTPRequest $request |
||
84 | * @param ValidationResult|null $result |
||
85 | * @return bool|Member |
||
86 | * @internal param Form $form |
||
87 | */ |
||
88 | public function authenticate(array $data, HTTPRequest $request, ValidationResult &$result = null) |
||
163 | |||
164 | /** |
||
165 | * Try to authenticate using the fallback authenticator. |
||
166 | * |
||
167 | * @param array $data |
||
168 | * @param HTTPRequest $request |
||
169 | * @return null|Member |
||
170 | * @internal param null|Form $form |
||
171 | */ |
||
172 | protected function fallback_authenticate($data, HTTPRequest $request) |
||
186 | |||
187 | /** |
||
188 | * Returns the services supported by this authenticator |
||
189 | * |
||
190 | * The number should be a bitwise-OR of 1 or more of the following constants: |
||
191 | * Authenticator::LOGIN, Authenticator::LOGOUT, Authenticator::CHANGE_PASSWORD, |
||
192 | * Authenticator::RESET_PASSWORD, or Authenticator::CMS_LOGIN |
||
193 | * |
||
194 | * @return int |
||
195 | */ |
||
196 | public function supportedServices() |
||
200 | |||
201 | /** |
||
202 | * Return RequestHandler to manage the log-in process. |
||
203 | * |
||
204 | * The default URL of the RequestHandler should return the initial log-in form, any other |
||
205 | * URL may be added for other steps & processing. |
||
206 | * |
||
207 | * URL-handling methods may return an array [ "Form" => (form-object) ] which can then |
||
208 | * be merged into a default controller. |
||
209 | * |
||
210 | * @param string $link The base link to use for this RequestHandler |
||
211 | * @return LoginHandler |
||
212 | */ |
||
213 | public function getLoginHandler($link) |
||
217 | |||
218 | /** |
||
219 | * Return the RequestHandler to manage the log-out process. |
||
220 | * |
||
221 | * The default URL of the RequestHandler should log the user out immediately and destroy the session. |
||
222 | * |
||
223 | * @param string $link The base link to use for this RequestHandler |
||
224 | * @return LogoutHandler |
||
225 | */ |
||
226 | public function getLogOutHandler($link) |
||
230 | |||
231 | /** |
||
232 | * Return RequestHandler to manage the change-password process. |
||
233 | * |
||
234 | * The default URL of the RequetHandler should return the initial change-password form, |
||
235 | * any other URL may be added for other steps & processing. |
||
236 | * |
||
237 | * URL-handling methods may return an array [ "Form" => (form-object) ] which can then |
||
238 | * be merged into a default controller. |
||
239 | * |
||
240 | * @param string $link The base link to use for this RequestHnadler |
||
241 | */ |
||
242 | public function getChangePasswordHandler($link) |
||
246 | |||
247 | /** |
||
248 | * @param string $link |
||
249 | * @return mixed |
||
250 | */ |
||
251 | public function getLostPasswordHandler($link) |
||
255 | |||
256 | /** |
||
257 | * Check if the passed password matches the stored one (if the member is not locked out). |
||
258 | * |
||
259 | * Note, we don't return early, to prevent differences in timings to give away if a member |
||
260 | * password is invalid. |
||
261 | * |
||
262 | * @param Member $member |
||
263 | * @param string $password |
||
264 | * @param ValidationResult $result |
||
265 | * @return ValidationResult |
||
266 | */ |
||
267 | public function checkPassword(Member $member, $password, ValidationResult &$result = null) |
||
271 | } |
||
272 |
This check marks private properties in classes that are never used. Those properties can be removed.