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:
Complex classes like AbstractAuthenticator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractAuthenticator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | abstract class AbstractAuthenticator implements |
||
35 | AuthenticatorInterface, |
||
36 | LoggerAwareInterface |
||
37 | { |
||
38 | use LoggerAwareTrait; |
||
39 | |||
40 | const AUTH_BY_PASSWORD = 'password'; |
||
41 | const AUTH_BY_SESSION = 'session'; |
||
42 | const AUTH_BY_TOKEN = 'token'; |
||
43 | |||
44 | /** |
||
45 | * The user that was last authenticated. |
||
46 | * |
||
47 | * @var AuthenticatableInterface |
||
48 | */ |
||
49 | private $authenticatedUser; |
||
50 | |||
51 | /** |
||
52 | * The token that was last authenticated. |
||
53 | * |
||
54 | * @var \Charcoal\User\AuthTokenInterface |
||
55 | */ |
||
56 | private $authenticatedToken; |
||
57 | |||
58 | /** |
||
59 | * The authentication method of the user that was last authenticated. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | private $authenticatedMethod; |
||
64 | |||
65 | /** |
||
66 | * Indicates if the logout method has been called. |
||
67 | * |
||
68 | * @var boolean |
||
69 | */ |
||
70 | private $isLoggedOut = false; |
||
71 | |||
72 | /** |
||
73 | * The user object type. |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | private $userType; |
||
78 | |||
79 | /** |
||
80 | * Store the user model factory instance for the current class. |
||
81 | * |
||
82 | * @var FactoryInterface |
||
83 | */ |
||
84 | private $userFactory; |
||
85 | |||
86 | /** |
||
87 | * The auth-token object type. |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | private $tokenType; |
||
92 | |||
93 | /** |
||
94 | * Store the auth-token model factory instance for the current class. |
||
95 | * |
||
96 | * @var FactoryInterface |
||
97 | */ |
||
98 | private $tokenFactory; |
||
99 | |||
100 | /** |
||
101 | * @param array $data Class dependencies. |
||
102 | */ |
||
103 | public function __construct(array $data) |
||
111 | |||
112 | /** |
||
113 | * Retrieve the user object type. |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | public function userType() |
||
121 | |||
122 | /** |
||
123 | * Retrieve the user model factory. |
||
124 | * |
||
125 | * @throws RuntimeException If the model factory was not previously set. |
||
126 | * @return FactoryInterface |
||
127 | */ |
||
128 | public function userFactory() |
||
132 | |||
133 | /** |
||
134 | * Create a new user model. |
||
135 | * |
||
136 | * @return \Charcoal\User\Access\AuthenticatableInterface |
||
137 | */ |
||
138 | public function createUser() |
||
142 | |||
143 | /** |
||
144 | * Retrieve the auth-token object type. |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function tokenType() |
||
152 | |||
153 | /** |
||
154 | * Retrieve the auth-token model factory. |
||
155 | * |
||
156 | * @throws RuntimeException If the token factory was not previously set. |
||
157 | * @return FactoryInterface |
||
158 | */ |
||
159 | public function tokenFactory() |
||
163 | |||
164 | /** |
||
165 | * Create a new auth-token model. |
||
166 | * |
||
167 | * @return \Charcoal\User\AuthTokenInterface |
||
168 | */ |
||
169 | public function createToken() |
||
173 | |||
174 | /** |
||
175 | * Set the user object type (model). |
||
176 | * |
||
177 | * @param string $type The user object type. |
||
178 | * @throws InvalidArgumentException If the user object type parameter is not a string. |
||
179 | * @return void |
||
180 | */ |
||
181 | protected function setUserType($type) |
||
191 | |||
192 | /** |
||
193 | * Set a user model factory. |
||
194 | * |
||
195 | * @param FactoryInterface $factory The factory used to create new user instances. |
||
196 | * @return void |
||
197 | */ |
||
198 | protected function setUserFactory(FactoryInterface $factory) |
||
202 | |||
203 | /** |
||
204 | * Set the authorization token type (model). |
||
205 | * |
||
206 | * @param string $type The auth-token object type. |
||
207 | * @throws InvalidArgumentException If the token object type parameter is not a string. |
||
208 | * @return void |
||
209 | */ |
||
210 | protected function setTokenType($type) |
||
220 | |||
221 | /** |
||
222 | * Set a model factory for token-based authentication. |
||
223 | * |
||
224 | * @param FactoryInterface $factory The factory used to create new auth-token instances. |
||
225 | * @return void |
||
226 | */ |
||
227 | protected function setTokenFactory(FactoryInterface $factory) |
||
231 | |||
232 | /** |
||
233 | * Retrieve the currently authenticated user. |
||
234 | * |
||
235 | * The method will attempt to authenticate a user. |
||
236 | * |
||
237 | * @return AuthenticatableInterface|null |
||
238 | */ |
||
239 | public function user() |
||
251 | |||
252 | /** |
||
253 | * Retrieve the ID for the currently authenticated user. |
||
254 | * |
||
255 | * The method will attempt to authenticate a user. |
||
256 | * |
||
257 | * @return mixed |
||
258 | */ |
||
259 | public function userId() |
||
272 | |||
273 | /** |
||
274 | * Retrieve the currently cached user. |
||
275 | * |
||
276 | * @return AuthenticatableInterface|null |
||
277 | */ |
||
278 | public function getUser() |
||
282 | |||
283 | /** |
||
284 | * Retrieve the ID for the currently cached user. |
||
285 | * |
||
286 | * @return mixed |
||
287 | */ |
||
288 | public function getUserId() |
||
297 | |||
298 | /** |
||
299 | * Set the authenticated user. |
||
300 | * |
||
301 | * @param AuthenticatableInterface $user The authenticated user. |
||
302 | * @return void |
||
303 | */ |
||
304 | public function setUser(AuthenticatableInterface $user) |
||
309 | |||
310 | /** |
||
311 | * Determine if the current user is authenticated. |
||
312 | * |
||
313 | * @return boolean |
||
314 | */ |
||
315 | public function check() |
||
319 | |||
320 | /** |
||
321 | * Determines if the logout method has been called. |
||
322 | * |
||
323 | * @return boolean TRUE if the logout method has been called, FALSE otherwise. |
||
324 | */ |
||
325 | protected function isLoggedOut() |
||
329 | |||
330 | /** |
||
331 | * Retrieve the authentication method of the current user. |
||
332 | * |
||
333 | * If the current user is authenticated, one of the |
||
334 | * `self::AUTH_BY_*` constants is returned. |
||
335 | * |
||
336 | * @return string|null |
||
337 | */ |
||
338 | public function getAuthenticationMethod() |
||
342 | |||
343 | /** |
||
344 | * Retrieve the authentication token of the current user. |
||
345 | * |
||
346 | * If the current user was authenticated by token, |
||
347 | * the auth token instance is returned. |
||
348 | * |
||
349 | * @return AuthTokenInterface|null |
||
350 | */ |
||
351 | public function getAuthenticationToken() |
||
355 | |||
356 | /** |
||
357 | * Log a user into the application. |
||
358 | * |
||
359 | * @param AuthenticatableInterface $user The authenticated user to log in. |
||
360 | * @param boolean $remember Whether to "remember" the user or not. |
||
361 | * @return void |
||
362 | */ |
||
363 | public function login(AuthenticatableInterface $user, $remember = false) |
||
377 | |||
378 | /** |
||
379 | * Log the user out of the application. |
||
380 | * |
||
381 | * @return void |
||
382 | */ |
||
383 | public function logout() |
||
392 | |||
393 | /** |
||
394 | * Attempt to authenticate a user by session or token. |
||
395 | * |
||
396 | * The user is authenticated via _session ID_ or _auth token_. |
||
397 | * |
||
398 | * @return AuthenticatableInterface|null Returns the authenticated user object |
||
399 | * or NULL if not authenticated. |
||
400 | */ |
||
401 | public function authenticate() |
||
423 | |||
424 | /** |
||
425 | * Attempt to authenticate a user using the given credentials. |
||
426 | * |
||
427 | * @param string $identifier The login ID, part of necessary credentials. |
||
428 | * @param string $password The password, part of necessary credentials. |
||
429 | * @throws InvalidArgumentException If the credentials are invalid or missing. |
||
430 | * @return AuthenticatableInterface|null Returns the authenticated user object |
||
431 | * or NULL if not authenticated. |
||
432 | */ |
||
433 | public function authenticateByPassword($identifier, $password) |
||
479 | |||
480 | /** |
||
481 | * Attempt to authenticate a user using their session ID. |
||
482 | * |
||
483 | * @return AuthenticatableInterface|null Returns the authenticated user object |
||
484 | * or NULL if not authenticated. |
||
485 | */ |
||
486 | protected function authenticateBySession() |
||
514 | |||
515 | /** |
||
516 | * Attempt to authenticate a user using their auth token. |
||
517 | * |
||
518 | * @return AuthenticatableInterface|null Returns the authenticated user object |
||
519 | * or NULL if not authenticated. |
||
520 | */ |
||
521 | protected function authenticateByToken() |
||
559 | |||
560 | /** |
||
561 | * Delete the user data from the session. |
||
562 | * |
||
563 | * @param AuthenticatableInterface|null $user The authenticated user to forget. |
||
564 | * @return void |
||
565 | */ |
||
566 | protected function deleteUserSession(AuthenticatableInterface $user = null) |
||
577 | |||
578 | /** |
||
579 | * Delete the user data from the cookie. |
||
580 | * |
||
581 | * @param AuthenticatableInterface|null $user The authenticated user to forget. |
||
582 | * @return void |
||
583 | */ |
||
584 | protected function deleteUserTokens(AuthenticatableInterface $user = null) |
||
605 | |||
606 | /** |
||
607 | * Delete the user data from the cookie. |
||
608 | * |
||
609 | * @throws InvalidArgumentException If trying to save a user to cookies without an ID. |
||
610 | * @return void |
||
611 | */ |
||
612 | protected function deleteCurrentToken() |
||
628 | |||
629 | /** |
||
630 | * Update the session with the given user. |
||
631 | * |
||
632 | * @param AuthenticatableInterface $user The authenticated user to remember. |
||
633 | * @throws InvalidArgumentException If trying to save a user to session without an ID. |
||
634 | * @return void |
||
635 | */ |
||
636 | protected function updateUserSession(AuthenticatableInterface $user) |
||
647 | |||
648 | /** |
||
649 | * Store the auth token for the given user in a cookie. |
||
650 | * |
||
651 | * @param AuthenticatableInterface $user The authenticated user to remember. |
||
652 | * @throws InvalidArgumentException If trying to save a user to cookies without an ID. |
||
653 | * @return void |
||
654 | */ |
||
655 | protected function updateCurrentToken(AuthenticatableInterface $user) |
||
676 | |||
677 | /** |
||
678 | * Validate the user login credentials are acceptable. |
||
679 | * |
||
680 | * @param string $identifier The user identifier to check. |
||
681 | * @param string $password The user password to check. |
||
682 | * @return boolean Returns TRUE if the credentials are acceptable, or FALSE otherwise. |
||
683 | */ |
||
684 | public function validateLogin($identifier, $password) |
||
688 | |||
689 | /** |
||
690 | * Validate the user identifier is acceptable. |
||
691 | * |
||
692 | * @param string $identifier The login ID. |
||
693 | * @return boolean Returns TRUE if the identifier is acceptable, or FALSE otherwise. |
||
694 | */ |
||
695 | public function validateAuthIdentifier($identifier) |
||
699 | |||
700 | /** |
||
701 | * Validate the user password is acceptable. |
||
702 | * |
||
703 | * @param string $password The password. |
||
704 | * @return boolean Returns TRUE if the password is acceptable, or FALSE otherwise. |
||
705 | */ |
||
706 | public function validateAuthPassword($password) |
||
710 | |||
711 | /** |
||
712 | * Validate the user authentication state is okay. |
||
713 | * |
||
714 | * For example, inactive users can not authenticate. |
||
715 | * |
||
716 | * @param AuthenticatableInterface $user The user to validate. |
||
717 | * @return boolean |
||
718 | */ |
||
719 | public function validateAuthentication(AuthenticatableInterface $user) |
||
723 | |||
724 | /** |
||
725 | * Updates the user's password hash. |
||
726 | * |
||
727 | * Assumes that the existing hash needs to be rehashed. |
||
728 | * |
||
729 | * @param AuthenticatableInterface $user The user to update. |
||
730 | * @param string $password The plain-text password to hash. |
||
731 | * @param boolean $update Whether to persist changes to storage. |
||
732 | * @throws InvalidArgumentException If the password is invalid. |
||
733 | * @return boolean Returns TRUE if the password was changed, or FALSE otherwise. |
||
734 | */ |
||
735 | View Code Duplication | protected function rehashUserPassword(AuthenticatableInterface $user, $password, $update = true) |
|
783 | |||
784 | /** |
||
785 | * Updates the user's password hash. |
||
786 | * |
||
787 | * @param AuthenticatableInterface $user The user to update. |
||
788 | * @param string $password The plain-text password to hash. |
||
789 | * @param boolean $update Whether to persist changes to storage. |
||
790 | * @throws InvalidArgumentException If the password is invalid. |
||
791 | * @return boolean Returns TRUE if the password was changed, or FALSE otherwise. |
||
792 | */ |
||
793 | View Code Duplication | protected function changeUserPassword(AuthenticatableInterface $user, $password, $update = true) |
|
841 | |||
842 | /** |
||
843 | * Clear the authenticator's internal cache. |
||
844 | * |
||
845 | * @return void |
||
846 | */ |
||
847 | protected function clearAuthenticator() |
||
854 | } |
||
855 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: