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 |
||
| 56 | class Manager extends PublicEmitter implements IUserManager { |
||
| 57 | /** |
||
| 58 | * @var \OCP\UserInterface[] $backends |
||
| 59 | */ |
||
| 60 | private $backends = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \OC\User\User[] $cachedUsers |
||
| 64 | */ |
||
| 65 | private $cachedUsers = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \OCP\IConfig $config |
||
| 69 | */ |
||
| 70 | private $config; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param \OCP\IConfig $config |
||
| 74 | */ |
||
| 75 | public function __construct(IConfig $config) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get the active backends |
||
| 86 | * @return \OCP\UserInterface[] |
||
| 87 | */ |
||
| 88 | public function getBackends() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * register a user backend |
||
| 94 | * |
||
| 95 | * @param \OCP\UserInterface $backend |
||
| 96 | */ |
||
| 97 | public function registerBackend($backend) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * remove a user backend |
||
| 103 | * |
||
| 104 | * @param \OCP\UserInterface $backend |
||
| 105 | */ |
||
| 106 | public function removeBackend($backend) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * remove all user backends |
||
| 115 | */ |
||
| 116 | public function clearBackends() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * get a user by user id |
||
| 123 | * |
||
| 124 | * @param string $uid |
||
| 125 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 126 | */ |
||
| 127 | public function get($uid) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * get or construct the user object |
||
| 144 | * |
||
| 145 | * @param string $uid |
||
| 146 | * @param \OCP\UserInterface $backend |
||
| 147 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
| 148 | * @return \OC\User\User |
||
| 149 | */ |
||
| 150 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * check if a user exists |
||
| 164 | * |
||
| 165 | * @param string $uid |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function userExists($uid) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Check if the password is valid for the user |
||
| 175 | * |
||
| 176 | * @param string $loginName |
||
| 177 | * @param string $password |
||
| 178 | * @return mixed the User object on success, false otherwise |
||
| 179 | */ |
||
| 180 | public function checkPassword($loginName, $password) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Check if the password is valid for the user |
||
| 192 | * |
||
| 193 | * @internal |
||
| 194 | * @param string $loginName |
||
| 195 | * @param string $password |
||
| 196 | * @return mixed the User object on success, false otherwise |
||
| 197 | */ |
||
| 198 | public function checkPasswordNoLogging($loginName, $password) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * search by user id |
||
| 216 | * |
||
| 217 | * @param string $pattern |
||
| 218 | * @param int $limit |
||
| 219 | * @param int $offset |
||
| 220 | * @return \OC\User\User[] |
||
| 221 | */ |
||
| 222 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
| 242 | |||
| 243 | /** |
||
| 244 | * search by displayName |
||
| 245 | * |
||
| 246 | * @param string $pattern |
||
| 247 | * @param int $limit |
||
| 248 | * @param int $offset |
||
| 249 | * @return \OC\User\User[] |
||
| 250 | */ |
||
| 251 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $uid |
||
| 274 | * @param string $password |
||
| 275 | * @throws \InvalidArgumentException |
||
| 276 | * @return bool|IUser the created user or false |
||
| 277 | */ |
||
| 278 | public function createUser($uid, $password) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $uid |
||
| 303 | * @param string $password |
||
| 304 | * @param UserInterface $backend |
||
| 305 | * @return IUser|null |
||
| 306 | * @throws \InvalidArgumentException |
||
| 307 | */ |
||
| 308 | public function createUserFromBackend($uid, $password, UserInterface $backend) { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * returns how many users per backend exist (if supported by backend) |
||
| 353 | * |
||
| 354 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
| 355 | * entry in the preferences table will be affected |
||
| 356 | * @return array|int an array of backend class as key and count number as value |
||
| 357 | * if $hasLoggedIn is true only an int is returned |
||
| 358 | */ |
||
| 359 | public function countUsers($hasLoggedIn = false) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * The callback is executed for each user on each backend. |
||
| 386 | * If the callback returns false no further users will be retrieved. |
||
| 387 | * |
||
| 388 | * @param \Closure $callback |
||
| 389 | * @param string $search |
||
| 390 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
| 391 | * in the preferences table will be affected |
||
| 392 | * @since 9.0.0 |
||
| 393 | */ |
||
| 394 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * returns how many users have logged in once |
||
| 421 | * |
||
| 422 | * @return int |
||
| 423 | * @since 12.0.0 |
||
| 424 | */ |
||
| 425 | View Code Duplication | public function countDisabledUsers() { |
|
| 440 | |||
| 441 | /** |
||
| 442 | * returns how many users have logged in once |
||
| 443 | * |
||
| 444 | * @return int |
||
| 445 | * @since 11.0.0 |
||
| 446 | */ |
||
| 447 | View Code Duplication | public function countSeenUsers() { |
|
| 462 | |||
| 463 | /** |
||
| 464 | * @param \Closure $callback |
||
| 465 | * @since 11.0.0 |
||
| 466 | */ |
||
| 467 | public function callForSeenUsers(\Closure $callback) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Getting all userIds that have a listLogin value requires checking the |
||
| 489 | * value in php because on oracle you cannot use a clob in a where clause, |
||
| 490 | * preventing us from doing a not null or length(value) > 0 check. |
||
| 491 | * |
||
| 492 | * @param int $limit |
||
| 493 | * @param int $offset |
||
| 494 | * @return string[] with user ids |
||
| 495 | */ |
||
| 496 | private function getSeenUserIds($limit = null, $offset = null) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param string $email |
||
| 529 | * @return IUser[] |
||
| 530 | * @since 9.1.0 |
||
| 531 | */ |
||
| 532 | public function getByEmail($email) { |
||
| 539 | } |
||
| 540 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: