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 |
||
| 55 | class Manager extends PublicEmitter implements IUserManager { |
||
| 56 | /** |
||
| 57 | * @var \OCP\UserInterface[] $backends |
||
| 58 | */ |
||
| 59 | private $backends = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var \OC\User\User[] $cachedUsers |
||
| 63 | */ |
||
| 64 | private $cachedUsers = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var \OCP\IConfig $config |
||
| 68 | */ |
||
| 69 | private $config; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param \OCP\IConfig $config |
||
| 73 | */ |
||
| 74 | public function __construct(IConfig $config = null) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get the active backends |
||
| 85 | * @return \OCP\UserInterface[] |
||
| 86 | */ |
||
| 87 | public function getBackends() { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * register a user backend |
||
| 93 | * |
||
| 94 | * @param \OCP\UserInterface $backend |
||
| 95 | */ |
||
| 96 | public function registerBackend($backend) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * remove a user backend |
||
| 102 | * |
||
| 103 | * @param \OCP\UserInterface $backend |
||
| 104 | */ |
||
| 105 | public function removeBackend($backend) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * remove all user backends |
||
| 114 | */ |
||
| 115 | public function clearBackends() { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * get a user by user id |
||
| 122 | * |
||
| 123 | * @param string $uid |
||
| 124 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 125 | */ |
||
| 126 | public function get($uid) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * get or construct the user object |
||
| 140 | * |
||
| 141 | * @param string $uid |
||
| 142 | * @param \OCP\UserInterface $backend |
||
| 143 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
| 144 | * @return \OC\User\User |
||
| 145 | */ |
||
| 146 | protected function getUserObject($uid, $backend, $cacheUser = true) { |
||
| 147 | if (isset($this->cachedUsers[$uid])) { |
||
| 148 | return $this->cachedUsers[$uid]; |
||
| 149 | } |
||
| 150 | |||
| 151 | if (method_exists($backend, 'loginName2UserName')) { |
||
| 152 | $loginName = $backend->loginName2UserName($uid); |
||
|
|
|||
| 153 | if ($loginName !== false) { |
||
| 154 | $uid = $loginName; |
||
| 155 | } |
||
| 156 | if (isset($this->cachedUsers[$uid])) { |
||
| 157 | return $this->cachedUsers[$uid]; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | $user = new User($uid, $backend, $this, $this->config); |
||
| 162 | if ($cacheUser) { |
||
| 163 | $this->cachedUsers[$uid] = $user; |
||
| 164 | } |
||
| 165 | return $user; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * check if a user exists |
||
| 170 | * |
||
| 171 | * @param string $uid |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function userExists($uid) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check if the password is valid for the user |
||
| 181 | * |
||
| 182 | * @param string $loginName |
||
| 183 | * @param string $password |
||
| 184 | * @return mixed the User object on success, false otherwise |
||
| 185 | */ |
||
| 186 | public function checkPassword($loginName, $password) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * search by user id |
||
| 205 | * |
||
| 206 | * @param string $pattern |
||
| 207 | * @param int $limit |
||
| 208 | * @param int $offset |
||
| 209 | * @return \OC\User\User[] |
||
| 210 | */ |
||
| 211 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
| 231 | |||
| 232 | /** |
||
| 233 | * search by displayName |
||
| 234 | * |
||
| 235 | * @param string $pattern |
||
| 236 | * @param int $limit |
||
| 237 | * @param int $offset |
||
| 238 | * @return \OC\User\User[] |
||
| 239 | */ |
||
| 240 | View Code Duplication | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $uid |
||
| 263 | * @param string $password |
||
| 264 | * @throws \Exception |
||
| 265 | * @return bool|\OC\User\User the created user or false |
||
| 266 | */ |
||
| 267 | public function createUser($uid, $password) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * returns how many users per backend exist (if supported by backend) |
||
| 307 | * |
||
| 308 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
| 309 | * entry in the preferences table will be affected |
||
| 310 | * @return array|int an array of backend class as key and count number as value |
||
| 311 | * if $hasLoggedIn is true only an int is returned |
||
| 312 | */ |
||
| 313 | public function countUsers($hasLoggedIn = false) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * The callback is executed for each user on each backend. |
||
| 340 | * If the callback returns false no further users will be retrieved. |
||
| 341 | * |
||
| 342 | * @param \Closure $callback |
||
| 343 | * @param string $search |
||
| 344 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
| 345 | * in the preferences table will be affected |
||
| 346 | * @since 9.0.0 |
||
| 347 | */ |
||
| 348 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * returns how many users have logged in once |
||
| 375 | * |
||
| 376 | * @return int |
||
| 377 | * @since 9.2.0 |
||
| 378 | */ |
||
| 379 | public function countSeenUsers() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param \Closure $callback |
||
| 398 | * @param string $search |
||
| 399 | * @since 9.2.0 |
||
| 400 | */ |
||
| 401 | public function callForSeenUsers (\Closure $callback) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Getting all userIds that have a listLogin value requires checking the |
||
| 423 | * value in php because on oracle you cannot use a clob in a where clause, |
||
| 424 | * preventing us from doing a not null or length(value) > 0 check. |
||
| 425 | * |
||
| 426 | * @param int $limit |
||
| 427 | * @param int $offset |
||
| 428 | * @return string[] with user ids |
||
| 429 | */ |
||
| 430 | private function getSeenUserIds($limit = null, $offset = null) { |
||
| 458 | /** |
||
| 459 | * @param string $email |
||
| 460 | * @return IUser[] |
||
| 461 | * @since 9.1.0 |
||
| 462 | */ |
||
| 463 | public function getByEmail($email) { |
||
| 470 | } |
||
| 471 |
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: