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 AuthProvider 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 AuthProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class AuthProvider extends Provider |
||
26 | { |
||
27 | /** |
||
28 | * Load the authentrication providers specified in the Settings $authProviders array |
||
29 | * |
||
30 | * @SuppressWarnings("StaticAccess") |
||
31 | */ |
||
32 | protected function __construct() |
||
37 | |||
38 | /** |
||
39 | * Get the Auth\User class instance for the specified login |
||
40 | * |
||
41 | * Unlike the AuthProvider::login() function. This function will not impact the SESSION |
||
42 | * |
||
43 | * @param string $username The username of the User |
||
44 | * @param string $password The password of the User |
||
45 | * |
||
46 | * @return Auth\User|false The User with the specified credentials or false if the credentials are not valid |
||
47 | */ |
||
48 | public function getUserByLogin($username, $password) |
||
62 | |||
63 | /** |
||
64 | * Use the provided credetials to log the user on |
||
65 | * |
||
66 | * @param string $username The username of the User |
||
67 | * @param string $password The password of the User |
||
68 | * |
||
69 | * @return true|false true if the login was successful, false otherwise |
||
70 | */ |
||
71 | public function login($username, $password) |
||
91 | |||
92 | /** |
||
93 | * Determine if the user is still logged on from the session data |
||
94 | * |
||
95 | * @param stdClass $data The AuthData from the session |
||
96 | * @param string $methodName The AuthMethod from the session |
||
97 | * |
||
98 | * @return true|false true if user is logged on, false otherwise |
||
99 | */ |
||
100 | public function isLoggedIn($data, $methodName) |
||
105 | |||
106 | /** |
||
107 | * Obtain the currently logged in user from the session data |
||
108 | * |
||
109 | * @param stdClass $data The AuthData from the session |
||
110 | * @param string $methodName The AuthMethod from the session |
||
111 | * |
||
112 | * @return Auth\User|false The User instance if user is logged on, false otherwise |
||
113 | */ |
||
114 | public function getUser($data, $methodName) |
||
119 | |||
120 | /** |
||
121 | * Merge or set the returnValue as appropriate |
||
122 | * |
||
123 | * @param false|Auth\Group|Auth\User $returnValue The value to merge to |
||
124 | * @param Auth\Group|Auth\User $res The value to merge from |
||
125 | * |
||
126 | * @return Auth\Group|false The merged returnValue |
||
127 | */ |
||
128 | public function mergeResult(&$returnValue, $res) |
||
141 | |||
142 | /** |
||
143 | * Get an Auth\Group by its name |
||
144 | * |
||
145 | * @param string $name The name of the group |
||
146 | * @param string $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
147 | * |
||
148 | * @return Auth\Group|false The Group instance if a group with that name exists, false otherwise |
||
149 | */ |
||
150 | View Code Duplication | public function getGroupByName($name, $methodName = false) |
|
159 | |||
160 | /** |
||
161 | * Get an array of Auth\User from a filtered set |
||
162 | * |
||
163 | * @param Data\Filter|boolean $filter The filter conditions or false to retreive all |
||
164 | * @param array|boolean $select The user fields to obtain or false to obtain all |
||
165 | * @param integer|boolean $top The number of users to obtain or false to obtain all |
||
166 | * @param integer|boolean $skip The number of users to skip or false to skip none |
||
167 | * @param array|boolean $orderby The field to sort by and the method to sort or false to not sort |
||
168 | * @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
169 | * |
||
170 | * @return array|boolean An array of Auth\User objects or false if no users were found |
||
171 | */ |
||
172 | public function getUsersByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false, $methodName = false) |
||
177 | |||
178 | /** |
||
179 | * Get an array of Auth\PendingUser from a filtered set |
||
180 | * |
||
181 | * @param Data\Filter|boolean $filter The filter conditions or false to retreive all |
||
182 | * @param array|boolean $select The user fields to obtain or false to obtain all |
||
183 | * @param integer|boolean $top The number of users to obtain or false to obtain all |
||
184 | * @param integer|boolean $skip The number of users to skip or false to skip none |
||
185 | * @param array|boolean $orderby The field to sort by and the method to sort or false to not sort |
||
186 | * @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
187 | * |
||
188 | * @return array|boolean An array of Auth\PendingUser objects or false if no pending users were found |
||
189 | */ |
||
190 | public function getPendingUsersByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false, $methodName = false) |
||
195 | |||
196 | /** |
||
197 | * Get an array of Auth\Group from a filtered set |
||
198 | * |
||
199 | * @param Data\Filter|false $filter The filter conditions or false to retreive all |
||
200 | * @param array|false $select The group fields to obtain or false to obtain all |
||
201 | * @param integer|false $top The number of groups to obtain or false to obtain all |
||
202 | * @param integer|false $skip The number of groups to skip or false to skip none |
||
203 | * @param array|false $orderby The field to sort by and the method to sort or false to not sort |
||
204 | * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
205 | * |
||
206 | * @return array|false An array of Auth\Group objects or false if no pending users were found |
||
207 | */ |
||
208 | public function getGroupsByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false, $methodName = false) |
||
213 | |||
214 | /** |
||
215 | * Get the number of currently active users on the system |
||
216 | * |
||
217 | * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
218 | * |
||
219 | * @return integer The number of currently active users on the system |
||
220 | */ |
||
221 | public function getActiveUserCount($methodName = false) |
||
230 | |||
231 | /** |
||
232 | * Get the number of currently pending users on the system |
||
233 | * |
||
234 | * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
235 | * |
||
236 | * @return integer The number of currently pending users on the system |
||
237 | */ |
||
238 | public function getPendingUserCount($methodName = false) |
||
247 | |||
248 | /** |
||
249 | * Get the number of current groups on the system |
||
250 | * |
||
251 | * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
252 | * |
||
253 | * @return integer The number of current groups on the system |
||
254 | */ |
||
255 | public function getGroupCount($methodName = false) |
||
264 | |||
265 | /** |
||
266 | * Get the login links for all supplementary Authenitcation mechanisms |
||
267 | * |
||
268 | * This will return an array of links to any supplementary authentication mechanims. For example, Goodle is |
||
269 | * a supplementary authentication mechanism. |
||
270 | * |
||
271 | * @return array An array of suppmentary authentication mechanism links |
||
272 | */ |
||
273 | public function getSupplementaryLinks() |
||
288 | |||
289 | /** |
||
290 | * Impersonate the user specified |
||
291 | * |
||
292 | * This will replace the user in the session with the specified user. In order |
||
293 | * to undo this operation a user must logout. |
||
294 | * |
||
295 | * @param array|Auth\User $userArray Data representing the user |
||
296 | */ |
||
297 | public function impersonateUser($userArray) |
||
305 | |||
306 | /** |
||
307 | * Get the pending user reresented by the supplied hash |
||
308 | * |
||
309 | * @param string $hash The hash value representing the Penging User |
||
310 | * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
311 | * |
||
312 | * @return Auth\PendingUser|false The Auth\PendingUser instance or false if no user is matched by the provided hash |
||
313 | */ |
||
314 | View Code Duplication | public function getTempUserByHash($hash, $methodName = false) |
|
323 | |||
324 | /** |
||
325 | * Create a pending user |
||
326 | * |
||
327 | * @param array $user An array of information about the user to create |
||
328 | * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
329 | * |
||
330 | * @return boolean true if the user was successfully created. Otherwise false. |
||
331 | */ |
||
332 | View Code Duplication | public function createPendingUser($user, $methodName = false) |
|
355 | |||
356 | /** |
||
357 | * Convert a Auth\PendingUser into an Auth\User |
||
358 | * |
||
359 | * This will allow a previously pending user the ability to log on in the future as an active user. It will also |
||
360 | * have the side effect of logging the user on now. |
||
361 | * |
||
362 | * @param Auth\PendingUser $user The user to turn into a current user |
||
363 | * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
364 | * |
||
365 | * @return boolean true if the user was successfully created. Otherwise false. |
||
366 | */ |
||
367 | View Code Duplication | public function activatePendingUser($user, $methodName = false) |
|
391 | |||
392 | /** |
||
393 | * Get a current user by a password reset hash |
||
394 | * |
||
395 | * @param string $hash The current password reset hash for the user |
||
396 | * @param string|false $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
397 | * |
||
398 | * @return Auth\User|false The user if the password reset hash is valid. Otherwise false. |
||
399 | */ |
||
400 | public function getUserByResetHash($hash, $methodName = false) |
||
413 | |||
414 | /** |
||
415 | * Get the Auth\Authenticator by host name |
||
416 | * |
||
417 | * @param string $host The host name used by the supplemental authentication mechanism |
||
418 | * |
||
419 | * @return Auth\Authenticator|false The Authenticator if the host is supported by a loaded Authenticator. Otherwise false. |
||
420 | */ |
||
421 | public function getSuplementalProviderByHost($host) |
||
438 | |||
439 | /** |
||
440 | * Delete any pending users that match the filter |
||
441 | * |
||
442 | * @param \Data\Filter|boolean $filter The filter to delete with or false to delete all |
||
443 | * @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
444 | * |
||
445 | * @return boolean True if the users were deleted, false otherwise |
||
446 | */ |
||
447 | public function deletePendingUsersByFilter($filter, $methodName = false) |
||
461 | |||
462 | /** |
||
463 | * Get the user by the one time access code |
||
464 | * |
||
465 | * @param string $key The user's access code |
||
466 | * @param string|boolean $methodName The AuthMethod if information is desired only from a particular Auth\Authenticator |
||
467 | * |
||
468 | * @return boolean|\Auth\User The User specified by the access code or false otherwise |
||
469 | */ |
||
470 | View Code Duplication | public function getUserByAccessCode($key, $methodName = false) |
|
479 | } |
||
480 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
481 |
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.