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) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Verify with Apache whether user is authenticated. |
||
| 223 | * |
||
| 224 | * @return boolean|null |
||
| 225 | * true: authenticated |
||
| 226 | * false: not authenticated |
||
| 227 | * null: not handled / no backend available |
||
| 228 | */ |
||
| 229 | public static function handleApacheAuth() { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * Sets user id for session and triggers emit |
||
| 247 | * |
||
| 248 | * @param string $uid |
||
| 249 | */ |
||
| 250 | public static function setUserId($uid) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Sets user display name for session |
||
| 262 | * |
||
| 263 | * @param string $uid |
||
| 264 | * @param string $displayName |
||
| 265 | * @return bool Whether the display name could get set |
||
| 266 | */ |
||
| 267 | View Code Duplication | public static function setDisplayName($uid, $displayName = null) { |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Check if the user is logged in, considers also the HTTP basic credentials |
||
| 281 | * |
||
| 282 | * @deprecated use \OC::$server->getUserSession()->isLoggedIn() |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | public static function isLoggedIn() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * set incognito mode, e.g. if a user wants to open a public link |
||
| 291 | * |
||
| 292 | * @param bool $status |
||
| 293 | */ |
||
| 294 | public static function setIncognitoMode($status) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * get incognito mode status |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public static function isIncognitoMode() { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Supplies an attribute to the logout hyperlink. The default behaviour |
||
| 309 | * is to return an href with '?logout=true' appended. However, it can |
||
| 310 | * supply any attribute(s) which are valid for <a>. |
||
| 311 | * |
||
| 312 | * @return string with one or more HTML attributes. |
||
| 313 | */ |
||
| 314 | public static function getLogoutAttribute() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Check if the user is an admin user |
||
| 332 | * |
||
| 333 | * @param string $uid uid of the admin |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public static function isAdminUser($uid) { |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * get the user id of the user currently logged in. |
||
| 348 | * |
||
| 349 | * @return string|bool uid or false |
||
| 350 | */ |
||
| 351 | public static function getUser() { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * get the display name of the user currently logged in. |
||
| 362 | * |
||
| 363 | * @param string $uid |
||
| 364 | * @return string uid or false |
||
| 365 | */ |
||
| 366 | public static function getDisplayName($uid = null) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Autogenerate a password |
||
| 386 | * |
||
| 387 | * @return string |
||
| 388 | * |
||
| 389 | * generates a password |
||
| 390 | */ |
||
| 391 | public static function generatePassword() { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Set password |
||
| 397 | * |
||
| 398 | * @param string $uid The username |
||
| 399 | * @param string $password The new password |
||
| 400 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
| 401 | * @return bool |
||
| 402 | * |
||
| 403 | * Change the password of a user |
||
| 404 | */ |
||
| 405 | public static function setPassword($uid, $password, $recoveryPassword = null) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Check if the password is correct |
||
| 416 | * |
||
| 417 | * @param string $uid The username |
||
| 418 | * @param string $password The password |
||
| 419 | * @return string|false user id a string on success, false otherwise |
||
| 420 | * |
||
| 421 | * Check if the password is correct without logging in the user |
||
| 422 | * returns the user id or false |
||
| 423 | */ |
||
| 424 | View Code Duplication | public static function checkPassword($uid, $password) { |
|
| 432 | |||
| 433 | /** |
||
| 434 | * @param string $uid The username |
||
| 435 | * @return string |
||
| 436 | * |
||
| 437 | * returns the path to the users home directory |
||
| 438 | * @deprecated Use \OC::$server->getUserManager->getHome() |
||
| 439 | */ |
||
| 440 | public static function getHome($uid) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Get a list of all users |
||
| 451 | * |
||
| 452 | * @return array an array of all uids |
||
| 453 | * |
||
| 454 | * Get a list of all users. |
||
| 455 | * @param string $search |
||
| 456 | * @param integer $limit |
||
| 457 | * @param integer $offset |
||
| 458 | */ |
||
| 459 | View Code Duplication | public static function getUsers($search = '', $limit = null, $offset = null) { |
|
| 467 | |||
| 468 | /** |
||
| 469 | * Get a list of all users display name |
||
| 470 | * |
||
| 471 | * @param string $search |
||
| 472 | * @param int $limit |
||
| 473 | * @param int $offset |
||
| 474 | * @return array associative array with all display names (value) and corresponding uids (key) |
||
| 475 | * |
||
| 476 | * Get a list of all display names and user ids. |
||
| 477 | * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead. |
||
| 478 | */ |
||
| 479 | View Code Duplication | public static function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 487 | |||
| 488 | /** |
||
| 489 | * check if a user exists |
||
| 490 | * |
||
| 491 | * @param string $uid the username |
||
| 492 | * @return boolean |
||
| 493 | */ |
||
| 494 | public static function userExists($uid) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * disables a user |
||
| 500 | * |
||
| 501 | * @param string $uid the user to disable |
||
| 502 | */ |
||
| 503 | public static function disableUser($uid) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * enable a user |
||
| 512 | * |
||
| 513 | * @param string $uid |
||
| 514 | */ |
||
| 515 | public static function enableUser($uid) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * checks if a user is enabled |
||
| 524 | * |
||
| 525 | * @param string $uid |
||
| 526 | * @return bool |
||
| 527 | */ |
||
| 528 | public static function isEnabled($uid) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Set cookie value to use in next page load |
||
| 539 | * |
||
| 540 | * @param string $username username to be set |
||
| 541 | * @param string $token |
||
| 542 | */ |
||
| 543 | public static function setMagicInCookie($username, $token) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Remove cookie for "remember username" |
||
| 549 | */ |
||
| 550 | public static function unsetMagicInCookie() { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Returns the first active backend from self::$_usedBackends. |
||
| 556 | * |
||
| 557 | * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
||
| 558 | */ |
||
| 559 | private static function findFirstActiveUsedBackend() { |
||
| 570 | } |
||
| 571 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.