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) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get the active backends |
||
| 94 | * @return \OCP\UserInterface[] |
||
| 95 | */ |
||
| 96 | public function getBackends() { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * register a user backend |
||
| 102 | * |
||
| 103 | * @param \OCP\UserInterface $backend |
||
| 104 | */ |
||
| 105 | public function registerBackend($backend) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * remove a user backend |
||
| 111 | * |
||
| 112 | * @param \OCP\UserInterface $backend |
||
| 113 | */ |
||
| 114 | public function removeBackend($backend) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * remove all user backends |
||
| 123 | */ |
||
| 124 | public function clearBackends() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * get a user by user id |
||
| 131 | * |
||
| 132 | * @param string $uid |
||
| 133 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 134 | */ |
||
| 135 | public function get($uid) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * get or construct the user object |
||
| 149 | * |
||
| 150 | * @param string $uid |
||
| 151 | * @param \OCP\UserInterface $backend |
||
| 152 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
| 153 | * @return \OC\User\User |
||
| 154 | */ |
||
| 155 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
| 156 | if (isset($this->cachedUsers[$uid])) { |
||
| 157 | return $this->cachedUsers[$uid]; |
||
| 158 | } |
||
| 159 | |||
| 160 | if (method_exists($backend, 'loginName2UserName')) { |
||
| 161 | $loginName = $backend->loginName2UserName($uid); |
||
|
|
|||
| 162 | if ($loginName !== false) { |
||
| 163 | $uid = $loginName; |
||
| 164 | } |
||
| 165 | if (isset($this->cachedUsers[$uid])) { |
||
| 166 | return $this->cachedUsers[$uid]; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | $user = new User($uid, $backend, $this, $this->config); |
||
| 171 | if ($cacheUser) { |
||
| 172 | $this->cachedUsers[$uid] = $user; |
||
| 173 | } |
||
| 174 | return $user; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * check if a user exists |
||
| 179 | * |
||
| 180 | * @param string $uid |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function userExists($uid) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Check if the password is valid for the user |
||
| 190 | * |
||
| 191 | * @param string $loginName |
||
| 192 | * @param string $password |
||
| 193 | * @return mixed the User object on success, false otherwise |
||
| 194 | */ |
||
| 195 | public function checkPassword($loginName, $password) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * search by user id |
||
| 214 | * |
||
| 215 | * @param string $pattern |
||
| 216 | * @param int $limit |
||
| 217 | * @param int $offset |
||
| 218 | * @return \OC\User\User[] |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * search by displayName |
||
| 243 | * |
||
| 244 | * @param string $pattern |
||
| 245 | * @param int $limit |
||
| 246 | * @param int $offset |
||
| 247 | * @return \OC\User\User[] |
||
| 248 | */ |
||
| 249 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $uid |
||
| 272 | * @param string $password |
||
| 273 | * @throws \Exception |
||
| 274 | * @return bool|\OC\User\User the created user or false |
||
| 275 | */ |
||
| 276 | public function createUser($uid, $password) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * returns how many users per backend exist (if supported by backend) |
||
| 316 | * |
||
| 317 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
| 318 | * entry in the preferences table will be affected |
||
| 319 | * @return array|int an array of backend class as key and count number as value |
||
| 320 | * if $hasLoggedIn is true only an int is returned |
||
| 321 | */ |
||
| 322 | public function countUsers($hasLoggedIn = false) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * The callback is executed for each user on each backend. |
||
| 349 | * If the callback returns false no further users will be retrieved. |
||
| 350 | * |
||
| 351 | * @param \Closure $callback |
||
| 352 | * @param string $search |
||
| 353 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
| 354 | * in the preferences table will be affected |
||
| 355 | * @since 9.0.0 |
||
| 356 | */ |
||
| 357 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * returns how many users have logged in once |
||
| 384 | * |
||
| 385 | * @return int |
||
| 386 | * @since 9.2.0 |
||
| 387 | */ |
||
| 388 | public function countSeenUsers() { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param \Closure $callback |
||
| 406 | * @since 9.2.0 |
||
| 407 | */ |
||
| 408 | public function callForSeenUsers(\Closure $callback) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Getting all userIds that have a listLogin value requires checking the |
||
| 430 | * value in php because on oracle you cannot use a clob in a where clause, |
||
| 431 | * preventing us from doing a not null or length(value) > 0 check. |
||
| 432 | * |
||
| 433 | * @param int $limit |
||
| 434 | * @param int $offset |
||
| 435 | * @return string[] with user ids |
||
| 436 | */ |
||
| 437 | private function getSeenUserIds($limit = null, $offset = null) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param string $email |
||
| 470 | * @return IUser[] |
||
| 471 | * @since 9.1.0 |
||
| 472 | */ |
||
| 473 | public function getByEmail($email) { |
||
| 480 | } |
||
| 481 |
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: