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 |
||
| 57 | class Manager extends PublicEmitter implements IUserManager { |
||
| 58 | /** |
||
| 59 | * @var \OCP\UserInterface[] $backends |
||
| 60 | */ |
||
| 61 | private $backends = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \OC\User\User[] $cachedUsers |
||
| 65 | */ |
||
| 66 | private $cachedUsers = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var \OCP\IConfig $config |
||
| 70 | */ |
||
| 71 | private $config; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param \OCP\IConfig $config |
||
| 75 | */ |
||
| 76 | public function __construct(IConfig $config) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get the active backends |
||
| 87 | * @return \OCP\UserInterface[] |
||
| 88 | */ |
||
| 89 | public function getBackends() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * register a user backend |
||
| 95 | * |
||
| 96 | * @param \OCP\UserInterface $backend |
||
| 97 | */ |
||
| 98 | public function registerBackend($backend) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * remove a user backend |
||
| 104 | * |
||
| 105 | * @param \OCP\UserInterface $backend |
||
| 106 | */ |
||
| 107 | public function removeBackend($backend) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * remove all user backends |
||
| 116 | */ |
||
| 117 | public function clearBackends() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * get a user by user id |
||
| 124 | * |
||
| 125 | * @param string $uid |
||
| 126 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 127 | */ |
||
| 128 | public function get($uid) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * get or construct the user object |
||
| 142 | * |
||
| 143 | * @param string $uid |
||
| 144 | * @param \OCP\UserInterface $backend |
||
| 145 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
| 146 | * @return \OC\User\User |
||
| 147 | */ |
||
| 148 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * check if a user exists |
||
| 172 | * |
||
| 173 | * @param string $uid |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function userExists($uid) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Check if the password is valid for the user |
||
| 183 | * |
||
| 184 | * @param string $loginName |
||
| 185 | * @param string $password |
||
| 186 | * @return mixed the User object on success, false otherwise |
||
| 187 | */ |
||
| 188 | public function checkPassword($loginName, $password) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Check if the password is valid for the user |
||
| 200 | * |
||
| 201 | * @internal |
||
| 202 | * @param string $loginName |
||
| 203 | * @param string $password |
||
| 204 | * @return mixed the User object on success, false otherwise |
||
| 205 | */ |
||
| 206 | public function checkPasswordNoLogging($loginName, $password) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * search by user id |
||
| 224 | * |
||
| 225 | * @param string $pattern |
||
| 226 | * @param int $limit |
||
| 227 | * @param int $offset |
||
| 228 | * @return \OC\User\User[] |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
| 250 | |||
| 251 | /** |
||
| 252 | * search by displayName |
||
| 253 | * |
||
| 254 | * @param string $pattern |
||
| 255 | * @param int $limit |
||
| 256 | * @param int $offset |
||
| 257 | * @return \OC\User\User[] |
||
| 258 | */ |
||
| 259 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $uid |
||
| 282 | * @param string $password |
||
| 283 | * @throws \InvalidArgumentException |
||
| 284 | * @return bool|IUser the created user or false |
||
| 285 | */ |
||
| 286 | public function createUser($uid, $password) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $uid |
||
| 298 | * @param string $password |
||
| 299 | * @param UserInterface $backend |
||
| 300 | * @return IUser|null |
||
| 301 | * @throws \InvalidArgumentException |
||
| 302 | */ |
||
| 303 | public function createUserFromBackend($uid, $password, UserInterface $backend) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * returns how many users per backend exist (if supported by backend) |
||
| 345 | * |
||
| 346 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
| 347 | * entry in the preferences table will be affected |
||
| 348 | * @return array|int an array of backend class as key and count number as value |
||
| 349 | * if $hasLoggedIn is true only an int is returned |
||
| 350 | */ |
||
| 351 | public function countUsers($hasLoggedIn = false) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * The callback is executed for each user on each backend. |
||
| 378 | * If the callback returns false no further users will be retrieved. |
||
| 379 | * |
||
| 380 | * @param \Closure $callback |
||
| 381 | * @param string $search |
||
| 382 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
| 383 | * in the preferences table will be affected |
||
| 384 | * @since 9.0.0 |
||
| 385 | */ |
||
| 386 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * returns how many users have logged in once |
||
| 413 | * |
||
| 414 | * @return int |
||
| 415 | * @since 12.0.0 |
||
| 416 | */ |
||
| 417 | View Code Duplication | public function countDisabledUsers() { |
|
| 432 | |||
| 433 | /** |
||
| 434 | * returns how many users have logged in once |
||
| 435 | * |
||
| 436 | * @return int |
||
| 437 | * @since 11.0.0 |
||
| 438 | */ |
||
| 439 | View Code Duplication | public function countSeenUsers() { |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @param \Closure $callback |
||
| 457 | * @since 11.0.0 |
||
| 458 | */ |
||
| 459 | public function callForSeenUsers(\Closure $callback) { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Getting all userIds that have a listLogin value requires checking the |
||
| 481 | * value in php because on oracle you cannot use a clob in a where clause, |
||
| 482 | * preventing us from doing a not null or length(value) > 0 check. |
||
| 483 | * |
||
| 484 | * @param int $limit |
||
| 485 | * @param int $offset |
||
| 486 | * @return string[] with user ids |
||
| 487 | */ |
||
| 488 | private function getSeenUserIds($limit = null, $offset = null) { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $email |
||
| 521 | * @return IUser[] |
||
| 522 | * @since 9.1.0 |
||
| 523 | */ |
||
| 524 | public function getByEmail($email) { |
||
| 531 | } |
||
| 532 |
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: