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 | * @suppress PhanDeprecatedFunction |
||
| 82 | */ |
||
| 83 | public static function useBackend($backend = 'database') { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * remove all used backends |
||
| 119 | */ |
||
| 120 | public static function clearBackends() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * setup the configured backends in config.php |
||
| 127 | * @suppress PhanDeprecatedFunction |
||
| 128 | */ |
||
| 129 | public static function setupBackends() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Try to login a user, assuming authentication |
||
| 162 | * has already happened (e.g. via Single Sign On). |
||
| 163 | * |
||
| 164 | * Log in a user and regenerate a new session. |
||
| 165 | * |
||
| 166 | * @param \OCP\Authentication\IApacheBackend $backend |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Verify with Apache whether user is authenticated. |
||
| 199 | * |
||
| 200 | * @return boolean|null |
||
| 201 | * true: authenticated |
||
| 202 | * false: not authenticated |
||
| 203 | * null: not handled / no backend available |
||
| 204 | */ |
||
| 205 | public static function handleApacheAuth() { |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Sets user id for session and triggers emit |
||
| 223 | * |
||
| 224 | * @param string $uid |
||
| 225 | */ |
||
| 226 | public static function setUserId($uid) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Sets user display name for session |
||
| 238 | * |
||
| 239 | * @param string $uid |
||
| 240 | * @param string $displayName |
||
| 241 | * @return bool Whether the display name could get set |
||
| 242 | */ |
||
| 243 | View Code Duplication | public static function setDisplayName($uid, $displayName = null) { |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Check if the user is logged in, considers also the HTTP basic credentials |
||
| 257 | * |
||
| 258 | * @deprecated use \OC::$server->getUserSession()->isLoggedIn() |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | public static function isLoggedIn() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * set incognito mode, e.g. if a user wants to open a public link |
||
| 267 | * |
||
| 268 | * @param bool $status |
||
| 269 | */ |
||
| 270 | public static function setIncognitoMode($status) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * get incognito mode status |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | public static function isIncognitoMode() { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns the current logout URL valid for the currently logged-in user |
||
| 285 | * |
||
| 286 | * @param \OCP\IURLGenerator $urlGenerator |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Check if the user is an admin user |
||
| 307 | * |
||
| 308 | * @param string $uid uid of the admin |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | public static function isAdminUser($uid) { |
||
| 319 | |||
| 320 | |||
| 321 | /** |
||
| 322 | * get the user id of the user currently logged in. |
||
| 323 | * |
||
| 324 | * @return string|bool uid or false |
||
| 325 | */ |
||
| 326 | public static function getUser() { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * get the display name of the user currently logged in. |
||
| 337 | * |
||
| 338 | * @param string $uid |
||
| 339 | * @return string|bool uid or false |
||
| 340 | */ |
||
| 341 | public static function getDisplayName($uid = null) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set password |
||
| 361 | * |
||
| 362 | * @param string $uid The username |
||
| 363 | * @param string $password The new password |
||
| 364 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
| 365 | * @return bool |
||
| 366 | * |
||
| 367 | * Change the password of a user |
||
| 368 | */ |
||
| 369 | public static function setPassword($uid, $password, $recoveryPassword = null) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Check if the password is correct |
||
| 380 | * |
||
| 381 | * @param string $uid The username |
||
| 382 | * @param string $password The password |
||
| 383 | * @return string|false user id a string on success, false otherwise |
||
| 384 | * |
||
| 385 | * Check if the password is correct without logging in the user |
||
| 386 | * returns the user id or false |
||
| 387 | */ |
||
| 388 | View Code Duplication | public static function checkPassword($uid, $password) { |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $uid The username |
||
| 399 | * @return string |
||
| 400 | * |
||
| 401 | * returns the path to the users home directory |
||
| 402 | * @deprecated Use \OC::$server->getUserManager->getHome() |
||
| 403 | */ |
||
| 404 | public static function getHome($uid) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get a list of all users |
||
| 415 | * |
||
| 416 | * @return array an array of all uids |
||
| 417 | * |
||
| 418 | * Get a list of all users. |
||
| 419 | * @param string $search |
||
| 420 | * @param integer $limit |
||
| 421 | * @param integer $offset |
||
| 422 | */ |
||
| 423 | View Code Duplication | public static function getUsers($search = '', $limit = null, $offset = null) { |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Get a list of all users display name |
||
| 434 | * |
||
| 435 | * @param string $search |
||
| 436 | * @param int $limit |
||
| 437 | * @param int $offset |
||
| 438 | * @return array associative array with all display names (value) and corresponding uids (key) |
||
| 439 | * |
||
| 440 | * Get a list of all display names and user ids. |
||
| 441 | * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead. |
||
| 442 | */ |
||
| 443 | View Code Duplication | public static function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 451 | |||
| 452 | /** |
||
| 453 | * check if a user exists |
||
| 454 | * |
||
| 455 | * @param string $uid the username |
||
| 456 | * @return boolean |
||
| 457 | */ |
||
| 458 | public static function userExists($uid) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * checks if a user is enabled |
||
| 464 | * |
||
| 465 | * @param string $uid |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | public static function isEnabled($uid) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns the first active backend from self::$_usedBackends. |
||
| 479 | * |
||
| 480 | * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
||
| 481 | */ |
||
| 482 | private static function findFirstActiveUsedBackend() { |
||
| 493 | } |
||
| 494 |
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.