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 OC_User 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 OC_User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 58 | class OC_User { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return \OC\User\Session |
||
| 62 | */ |
||
| 63 | public static function getUserSession() { |
||
| 66 | |||
| 67 | private static $_usedBackends = array(); |
||
| 68 | |||
| 69 | private static $_setupedBackends = array(); |
||
| 70 | |||
| 71 | // bool, stores if a user want to access a resource anonymously, e.g if they open a public link |
||
| 72 | private static $incognitoMode = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Adds the backend to the list of used backends |
||
| 76 | * |
||
| 77 | * @param string|\OCP\UserInterface $backend default: database The backend to use for user management |
||
| 78 | * @return bool |
||
| 79 | * |
||
| 80 | * Set the User Authentication Module |
||
| 81 | */ |
||
| 82 | public static function useBackend($backend = 'database') { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * remove all used backends |
||
| 118 | */ |
||
| 119 | public static function clearBackends() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * setup the configured backends in config.php |
||
| 126 | */ |
||
| 127 | public static function setupBackends() { |
||
| 157 | |||
| 158 | /** |
||
| 159 | |||
| 160 | * Try to login a user using the magic cookie (remember login) |
||
| 161 | * |
||
| 162 | * @deprecated use \OCP\IUserSession::loginWithCookie() |
||
| 163 | * @param string $uid The username of the user to log in |
||
| 164 | * @param string $token |
||
| 165 | * @param string $oldSessionId |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public static function loginWithCookie($uid, $token, $oldSessionId) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Try to login a user, assuming authentication |
||
| 174 | * has already happened (e.g. via Single Sign On). |
||
| 175 | * |
||
| 176 | * Log in a user and regenerate a new session. |
||
| 177 | * |
||
| 178 | * @param \OCP\Authentication\IApacheBackend $backend |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Verify with Apache whether user is authenticated. |
||
| 222 | * |
||
| 223 | * @return boolean|null |
||
| 224 | * true: authenticated |
||
| 225 | * false: not authenticated |
||
| 226 | * null: not handled / no backend available |
||
| 227 | */ |
||
| 228 | public static function handleApacheAuth() { |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * Sets user id for session and triggers emit |
||
| 246 | * |
||
| 247 | * @param string $uid |
||
| 248 | */ |
||
| 249 | public static function setUserId($uid) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Sets user display name for session |
||
| 261 | * |
||
| 262 | * @param string $uid |
||
| 263 | * @param string $displayName |
||
| 264 | * @return bool Whether the display name could get set |
||
| 265 | */ |
||
| 266 | View Code Duplication | public static function setDisplayName($uid, $displayName = null) { |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Check if the user is logged in, considers also the HTTP basic credentials |
||
| 280 | * |
||
| 281 | * @deprecated use \OC::$server->getUserSession()->isLoggedIn() |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public static function isLoggedIn() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * set incognito mode, e.g. if a user wants to open a public link |
||
| 290 | * |
||
| 291 | * @param bool $status |
||
| 292 | */ |
||
| 293 | public static function setIncognitoMode($status) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * get incognito mode status |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | public static function isIncognitoMode() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Supplies an attribute to the logout hyperlink. The default behaviour |
||
| 308 | * is to return an href with '?logout=true' appended. However, it can |
||
| 309 | * supply any attribute(s) which are valid for <a>. |
||
| 310 | * |
||
| 311 | * @return string with one or more HTML attributes. |
||
| 312 | */ |
||
| 313 | public static function getLogoutAttribute() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Check if the user is an admin user |
||
| 331 | * |
||
| 332 | * @param string $uid uid of the admin |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | public static function isAdminUser($uid) { |
||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * get the user id of the user currently logged in. |
||
| 347 | * |
||
| 348 | * @return string|bool uid or false |
||
| 349 | */ |
||
| 350 | public static function getUser() { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * get the display name of the user currently logged in. |
||
| 361 | * |
||
| 362 | * @param string $uid |
||
| 363 | * @return string uid or false |
||
| 364 | */ |
||
| 365 | public static function getDisplayName($uid = null) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Autogenerate a password |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | * |
||
| 388 | * generates a password |
||
| 389 | */ |
||
| 390 | public static function generatePassword() { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Set password |
||
| 396 | * |
||
| 397 | * @param string $uid The username |
||
| 398 | * @param string $password The new password |
||
| 399 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
| 400 | * @return bool |
||
| 401 | * |
||
| 402 | * Change the password of a user |
||
| 403 | */ |
||
| 404 | public static function setPassword($uid, $password, $recoveryPassword = null) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Check whether user can change his avatar |
||
| 415 | * |
||
| 416 | * @param string $uid The username |
||
| 417 | * @return bool |
||
| 418 | * |
||
| 419 | * Check whether a specified user can change his avatar |
||
| 420 | */ |
||
| 421 | public static function canUserChangeAvatar($uid) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Check whether user can change his password |
||
| 432 | * |
||
| 433 | * @param string $uid The username |
||
| 434 | * @return bool |
||
| 435 | * |
||
| 436 | * Check whether a specified user can change his password |
||
| 437 | */ |
||
| 438 | public static function canUserChangePassword($uid) { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Check whether user can change his display name |
||
| 449 | * |
||
| 450 | * @param string $uid The username |
||
| 451 | * @return bool |
||
| 452 | * |
||
| 453 | * Check whether a specified user can change his display name |
||
| 454 | */ |
||
| 455 | public static function canUserChangeDisplayName($uid) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Check if the password is correct |
||
| 466 | * |
||
| 467 | * @param string $uid The username |
||
| 468 | * @param string $password The password |
||
| 469 | * @return string|false user id a string on success, false otherwise |
||
| 470 | * |
||
| 471 | * Check if the password is correct without logging in the user |
||
| 472 | * returns the user id or false |
||
| 473 | */ |
||
| 474 | View Code Duplication | public static function checkPassword($uid, $password) { |
|
| 482 | |||
| 483 | /** |
||
| 484 | * @param string $uid The username |
||
| 485 | * @return string |
||
| 486 | * |
||
| 487 | * returns the path to the users home directory |
||
| 488 | * @deprecated Use \OC::$server->getUserManager->getHome() |
||
| 489 | */ |
||
| 490 | public static function getHome($uid) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get a list of all users |
||
| 501 | * |
||
| 502 | * @return array an array of all uids |
||
| 503 | * |
||
| 504 | * Get a list of all users. |
||
| 505 | * @param string $search |
||
| 506 | * @param integer $limit |
||
| 507 | * @param integer $offset |
||
| 508 | */ |
||
| 509 | View Code Duplication | public static function getUsers($search = '', $limit = null, $offset = null) { |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Get a list of all users display name |
||
| 520 | * |
||
| 521 | * @param string $search |
||
| 522 | * @param int $limit |
||
| 523 | * @param int $offset |
||
| 524 | * @return array associative array with all display names (value) and corresponding uids (key) |
||
| 525 | * |
||
| 526 | * Get a list of all display names and user ids. |
||
| 527 | * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead. |
||
| 528 | */ |
||
| 529 | View Code Duplication | public static function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 537 | |||
| 538 | /** |
||
| 539 | * check if a user exists |
||
| 540 | * |
||
| 541 | * @param string $uid the username |
||
| 542 | * @return boolean |
||
| 543 | */ |
||
| 544 | public static function userExists($uid) { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * disables a user |
||
| 550 | * |
||
| 551 | * @param string $uid the user to disable |
||
| 552 | */ |
||
| 553 | public static function disableUser($uid) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * enable a user |
||
| 562 | * |
||
| 563 | * @param string $uid |
||
| 564 | */ |
||
| 565 | public static function enableUser($uid) { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * checks if a user is enabled |
||
| 574 | * |
||
| 575 | * @param string $uid |
||
| 576 | * @return bool |
||
| 577 | */ |
||
| 578 | public static function isEnabled($uid) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Set cookie value to use in next page load |
||
| 589 | * |
||
| 590 | * @param string $username username to be set |
||
| 591 | * @param string $token |
||
| 592 | */ |
||
| 593 | public static function setMagicInCookie($username, $token) { |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Remove cookie for "remember username" |
||
| 599 | */ |
||
| 600 | public static function unsetMagicInCookie() { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Returns the first active backend from self::$_usedBackends. |
||
| 606 | * |
||
| 607 | * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
||
| 608 | */ |
||
| 609 | private static function findFirstActiveUsedBackend() { |
||
| 620 | } |
||
| 621 |
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.