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 |
||
| 59 | class OC_User { |
||
|
|
|||
| 60 | |||
| 61 | private static $_usedBackends = array(); |
||
| 62 | |||
| 63 | private static $_setupedBackends = array(); |
||
| 64 | |||
| 65 | // bool, stores if a user want to access a resource anonymously, e.g if they open a public link |
||
| 66 | private static $incognitoMode = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Adds the backend to the list of used backends |
||
| 70 | * |
||
| 71 | * @param string|\OCP\UserInterface $backend default: database The backend to use for user management |
||
| 72 | * @return bool |
||
| 73 | * |
||
| 74 | * Set the User Authentication Module |
||
| 75 | * @suppress PhanDeprecatedFunction |
||
| 76 | */ |
||
| 77 | public static function useBackend($backend = 'database') { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * remove all used backends |
||
| 113 | */ |
||
| 114 | public static function clearBackends() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * setup the configured backends in config.php |
||
| 121 | * @suppress PhanDeprecatedFunction |
||
| 122 | */ |
||
| 123 | public static function setupBackends() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Try to login a user, assuming authentication |
||
| 156 | * has already happened (e.g. via Single Sign On). |
||
| 157 | * |
||
| 158 | * Log in a user and regenerate a new session. |
||
| 159 | * |
||
| 160 | * @param \OCP\Authentication\IApacheBackend $backend |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public static function loginWithApache(\OCP\Authentication\IApacheBackend $backend) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Verify with Apache whether user is authenticated. |
||
| 193 | * |
||
| 194 | * @return boolean|null |
||
| 195 | * true: authenticated |
||
| 196 | * false: not authenticated |
||
| 197 | * null: not handled / no backend available |
||
| 198 | */ |
||
| 199 | public static function handleApacheAuth() { |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Sets user id for session and triggers emit |
||
| 217 | * |
||
| 218 | * @param string $uid |
||
| 219 | */ |
||
| 220 | public static function setUserId($uid) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Check if the user is logged in, considers also the HTTP basic credentials |
||
| 232 | * |
||
| 233 | * @deprecated use \OC::$server->getUserSession()->isLoggedIn() |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public static function isLoggedIn() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * set incognito mode, e.g. if a user wants to open a public link |
||
| 242 | * |
||
| 243 | * @param bool $status |
||
| 244 | */ |
||
| 245 | public static function setIncognitoMode($status) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * get incognito mode status |
||
| 251 | * |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | public static function isIncognitoMode() { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns the current logout URL valid for the currently logged-in user |
||
| 260 | * |
||
| 261 | * @param \OCP\IURLGenerator $urlGenerator |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Check if the user is an admin user |
||
| 282 | * |
||
| 283 | * @param string $uid uid of the admin |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public static function isAdminUser($uid) { |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * get the user id of the user currently logged in. |
||
| 298 | * |
||
| 299 | * @return string|bool uid or false |
||
| 300 | */ |
||
| 301 | public static function getUser() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * get the display name of the user currently logged in. |
||
| 312 | * |
||
| 313 | * @param string $uid |
||
| 314 | * @return string|bool uid or false |
||
| 315 | */ |
||
| 316 | public static function getDisplayName($uid = null) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set password |
||
| 336 | * |
||
| 337 | * @param string $uid The username |
||
| 338 | * @param string $password The new password |
||
| 339 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
| 340 | * @return bool |
||
| 341 | * |
||
| 342 | * Change the password of a user |
||
| 343 | */ |
||
| 344 | public static function setPassword($uid, $password, $recoveryPassword = null) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $uid The username |
||
| 355 | * @return string |
||
| 356 | * |
||
| 357 | * returns the path to the users home directory |
||
| 358 | * @deprecated Use \OC::$server->getUserManager->getHome() |
||
| 359 | */ |
||
| 360 | public static function getHome($uid) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get a list of all users |
||
| 371 | * |
||
| 372 | * @return array an array of all uids |
||
| 373 | * |
||
| 374 | * Get a list of all users. |
||
| 375 | * @param string $search |
||
| 376 | * @param integer $limit |
||
| 377 | * @param integer $offset |
||
| 378 | */ |
||
| 379 | View Code Duplication | public static function getUsers($search = '', $limit = null, $offset = null) { |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Get a list of all users display name |
||
| 390 | * |
||
| 391 | * @param string $search |
||
| 392 | * @param int $limit |
||
| 393 | * @param int $offset |
||
| 394 | * @return array associative array with all display names (value) and corresponding uids (key) |
||
| 395 | * |
||
| 396 | * Get a list of all display names and user ids. |
||
| 397 | * @deprecated Use \OC::$server->getUserManager->searchDisplayName($search, $limit, $offset) instead. |
||
| 398 | */ |
||
| 399 | View Code Duplication | public static function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Returns the first active backend from self::$_usedBackends. |
||
| 410 | * |
||
| 411 | * @return OCP\Authentication\IApacheBackend|null if no backend active, otherwise OCP\Authentication\IApacheBackend |
||
| 412 | */ |
||
| 413 | private static function findFirstActiveUsedBackend() { |
||
| 424 | } |
||
| 425 |
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.