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 Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | class Manager extends PublicEmitter implements IUserManager { |
||
|
|
|||
| 60 | /** |
||
| 61 | * @var \OCP\UserInterface[] $backends |
||
| 62 | */ |
||
| 63 | private $backends = array(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var \OC\User\User[] $cachedUsers |
||
| 67 | */ |
||
| 68 | private $cachedUsers = array(); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var \OCP\IConfig $config |
||
| 72 | */ |
||
| 73 | private $config; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param \OCP\IConfig $config |
||
| 77 | */ |
||
| 78 | public function __construct(IConfig $config) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get the active backends |
||
| 89 | * @return \OCP\UserInterface[] |
||
| 90 | */ |
||
| 91 | public function getBackends() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * register a user backend |
||
| 97 | * |
||
| 98 | * @param \OCP\UserInterface $backend |
||
| 99 | */ |
||
| 100 | public function registerBackend($backend) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * remove a user backend |
||
| 106 | * |
||
| 107 | * @param \OCP\UserInterface $backend |
||
| 108 | */ |
||
| 109 | public function removeBackend($backend) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * remove all user backends |
||
| 118 | */ |
||
| 119 | public function clearBackends() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * get a user by user id |
||
| 126 | * |
||
| 127 | * @param string $uid |
||
| 128 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 129 | */ |
||
| 130 | public function get($uid) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * get or construct the user object |
||
| 147 | * |
||
| 148 | * @param string $uid |
||
| 149 | * @param \OCP\UserInterface $backend |
||
| 150 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
| 151 | * @return \OC\User\User |
||
| 152 | */ |
||
| 153 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * check if a user exists |
||
| 167 | * |
||
| 168 | * @param string $uid |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | public function userExists($uid) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Check if the password is valid for the user |
||
| 178 | * |
||
| 179 | * @param string $loginName |
||
| 180 | * @param string $password |
||
| 181 | * @return mixed the User object on success, false otherwise |
||
| 182 | */ |
||
| 183 | public function checkPassword($loginName, $password) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Check if the password is valid for the user |
||
| 195 | * |
||
| 196 | * @internal |
||
| 197 | * @param string $loginName |
||
| 198 | * @param string $password |
||
| 199 | * @return mixed the User object on success, false otherwise |
||
| 200 | */ |
||
| 201 | public function checkPasswordNoLogging($loginName, $password) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * search by user id |
||
| 219 | * |
||
| 220 | * @param string $pattern |
||
| 221 | * @param int $limit |
||
| 222 | * @param int $offset |
||
| 223 | * @return \OC\User\User[] |
||
| 224 | */ |
||
| 225 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
| 245 | |||
| 246 | /** |
||
| 247 | * search by displayName |
||
| 248 | * |
||
| 249 | * @param string $pattern |
||
| 250 | * @param int $limit |
||
| 251 | * @param int $offset |
||
| 252 | * @return \OC\User\User[] |
||
| 253 | */ |
||
| 254 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $uid |
||
| 277 | * @param string $password |
||
| 278 | * @throws \InvalidArgumentException |
||
| 279 | * @return bool|IUser the created user or false |
||
| 280 | */ |
||
| 281 | public function createUser($uid, $password) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $uid |
||
| 306 | * @param string $password |
||
| 307 | * @param UserInterface $backend |
||
| 308 | * @return IUser|null |
||
| 309 | * @throws \InvalidArgumentException |
||
| 310 | */ |
||
| 311 | public function createUserFromBackend($uid, $password, UserInterface $backend) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * returns how many users per backend exist (if supported by backend) |
||
| 356 | * |
||
| 357 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
| 358 | * entry in the preferences table will be affected |
||
| 359 | * @return array|int an array of backend class as key and count number as value |
||
| 360 | * if $hasLoggedIn is true only an int is returned |
||
| 361 | */ |
||
| 362 | public function countUsers($hasLoggedIn = false) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * The callback is executed for each user on each backend. |
||
| 389 | * If the callback returns false no further users will be retrieved. |
||
| 390 | * |
||
| 391 | * @param \Closure $callback |
||
| 392 | * @param string $search |
||
| 393 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
| 394 | * in the preferences table will be affected |
||
| 395 | * @since 9.0.0 |
||
| 396 | */ |
||
| 397 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * returns how many users are disabled in the requested groups |
||
| 424 | * |
||
| 425 | * @param array $groups groupids to search |
||
| 426 | * @return int |
||
| 427 | * @since 14.0.0 |
||
| 428 | */ |
||
| 429 | public function countDisabledUsersOfGroups(array $groups):int { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * returns how many users have logged in once |
||
| 449 | * |
||
| 450 | * @return int |
||
| 451 | * @since 11.0.0 |
||
| 452 | */ |
||
| 453 | public function countSeenUsers() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param \Closure $callback |
||
| 471 | * @since 11.0.0 |
||
| 472 | */ |
||
| 473 | public function callForSeenUsers(\Closure $callback) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Getting all userIds that have a listLogin value requires checking the |
||
| 496 | * value in php because on oracle you cannot use a clob in a where clause, |
||
| 497 | * preventing us from doing a not null or length(value) > 0 check. |
||
| 498 | * |
||
| 499 | * @param int $limit |
||
| 500 | * @param int $offset |
||
| 501 | * @return string[] with user ids |
||
| 502 | */ |
||
| 503 | private function getSeenUserIds($limit = null, $offset = null) { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $email |
||
| 536 | * @return IUser[] |
||
| 537 | * @since 9.1.0 |
||
| 538 | */ |
||
| 539 | public function getByEmail($email) { |
||
| 546 | } |
||
| 547 |