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) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * get or construct the user object |
||
| 141 | * |
||
| 142 | * @param string $uid |
||
| 143 | * @param \OCP\UserInterface $backend |
||
| 144 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
| 145 | * @return \OC\User\User |
||
| 146 | */ |
||
| 147 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * check if a user exists |
||
| 171 | * |
||
| 172 | * @param string $uid |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public function userExists($uid) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Check if the password is valid for the user |
||
| 182 | * |
||
| 183 | * @param string $loginName |
||
| 184 | * @param string $password |
||
| 185 | * @return mixed the User object on success, false otherwise |
||
| 186 | */ |
||
| 187 | public function checkPassword($loginName, $password) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Check if the password is valid for the user |
||
| 199 | * |
||
| 200 | * @internal |
||
| 201 | * @param string $loginName |
||
| 202 | * @param string $password |
||
| 203 | * @return mixed the User object on success, false otherwise |
||
| 204 | */ |
||
| 205 | public function checkPasswordNoLogging($loginName, $password) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * search by user id |
||
| 223 | * |
||
| 224 | * @param string $pattern |
||
| 225 | * @param int $limit |
||
| 226 | * @param int $offset |
||
| 227 | * @return \OC\User\User[] |
||
| 228 | */ |
||
| 229 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
| 249 | |||
| 250 | /** |
||
| 251 | * search by displayName |
||
| 252 | * |
||
| 253 | * @param string $pattern |
||
| 254 | * @param int $limit |
||
| 255 | * @param int $offset |
||
| 256 | * @return \OC\User\User[] |
||
| 257 | */ |
||
| 258 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $uid |
||
| 281 | * @param string $password |
||
| 282 | * @throws \Exception |
||
| 283 | * @return bool|\OC\User\User the created user or false |
||
| 284 | */ |
||
| 285 | public function createUser($uid, $password) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * returns how many users per backend exist (if supported by backend) |
||
| 325 | * |
||
| 326 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
| 327 | * entry in the preferences table will be affected |
||
| 328 | * @return array|int an array of backend class as key and count number as value |
||
| 329 | * if $hasLoggedIn is true only an int is returned |
||
| 330 | */ |
||
| 331 | public function countUsers($hasLoggedIn = false) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * The callback is executed for each user on each backend. |
||
| 358 | * If the callback returns false no further users will be retrieved. |
||
| 359 | * |
||
| 360 | * @param \Closure $callback |
||
| 361 | * @param string $search |
||
| 362 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
| 363 | * in the preferences table will be affected |
||
| 364 | * @since 9.0.0 |
||
| 365 | */ |
||
| 366 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * returns how many users have logged in once |
||
| 393 | * |
||
| 394 | * @return int |
||
| 395 | * @since 11.0.0 |
||
| 396 | */ |
||
| 397 | public function countSeenUsers() { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param \Closure $callback |
||
| 415 | * @since 11.0.0 |
||
| 416 | */ |
||
| 417 | public function callForSeenUsers(\Closure $callback) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Getting all userIds that have a listLogin value requires checking the |
||
| 439 | * value in php because on oracle you cannot use a clob in a where clause, |
||
| 440 | * preventing us from doing a not null or length(value) > 0 check. |
||
| 441 | * |
||
| 442 | * @param int $limit |
||
| 443 | * @param int $offset |
||
| 444 | * @return string[] with user ids |
||
| 445 | */ |
||
| 446 | private function getSeenUserIds($limit = null, $offset = null) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $email |
||
| 479 | * @return IUser[] |
||
| 480 | * @since 9.1.0 |
||
| 481 | */ |
||
| 482 | public function getByEmail($email) { |
||
| 489 | } |
||
| 490 |
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: