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) { |
||
| 286 | $l = \OC::$server->getL10N('lib'); |
||
| 287 | // Check the name for bad characters |
||
| 288 | // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
||
| 289 | if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
||
| 290 | throw new \Exception($l->t('Only the following characters are allowed in a username:' |
||
| 291 | . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
||
| 292 | } |
||
| 293 | // No empty username |
||
| 294 | if (trim($uid) == '') { |
||
| 295 | throw new \Exception($l->t('A valid username must be provided')); |
||
| 296 | } |
||
| 297 | // No whitespace at the beginning or at the end |
||
| 298 | if (trim($uid) !== $uid) { |
||
| 299 | throw new \Exception($l->t('Username contains whitespace at the beginning or at the end')); |
||
| 300 | } |
||
| 301 | // Username only consists of 1 or 2 dots (directory traversal) |
||
| 302 | if ($uid === '.' || $uid === '..') { |
||
| 303 | throw new \Exception($l->t('Username must not consist of dots only')); |
||
| 304 | } |
||
| 305 | // No empty password |
||
| 306 | if (trim($password) == '') { |
||
| 307 | throw new \Exception($l->t('A valid password must be provided')); |
||
| 308 | } |
||
| 309 | |||
| 310 | // Check if user already exists |
||
| 311 | if ($this->userExists($uid)) { |
||
| 312 | throw new \Exception($l->t('The username is already being used')); |
||
| 313 | } |
||
| 314 | |||
| 315 | $this->emit('\OC\User', 'preCreateUser', array($uid, $password)); |
||
| 316 | foreach ($this->backends as $backend) { |
||
| 317 | if ($backend->implementsActions(Backend::CREATE_USER)) { |
||
| 318 | $backend->createUser($uid, $password); |
||
| 319 | $user = $this->getUserObject($uid, $backend); |
||
| 320 | $this->emit('\OC\User', 'postCreateUser', array($user, $password)); |
||
| 321 | return $user; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | return false; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * returns how many users per backend exist (if supported by backend) |
||
| 329 | * |
||
| 330 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
| 331 | * entry in the preferences table will be affected |
||
| 332 | * @return array|int an array of backend class as key and count number as value |
||
| 333 | * if $hasLoggedIn is true only an int is returned |
||
| 334 | */ |
||
| 335 | public function countUsers($hasLoggedIn = false) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * The callback is executed for each user on each backend. |
||
| 362 | * If the callback returns false no further users will be retrieved. |
||
| 363 | * |
||
| 364 | * @param \Closure $callback |
||
| 365 | * @param string $search |
||
| 366 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
| 367 | * in the preferences table will be affected |
||
| 368 | * @since 9.0.0 |
||
| 369 | */ |
||
| 370 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * returns how many users have logged in once |
||
| 397 | * |
||
| 398 | * @return int |
||
| 399 | * @since 11.0.0 |
||
| 400 | */ |
||
| 401 | public function countSeenUsers() { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param \Closure $callback |
||
| 419 | * @since 11.0.0 |
||
| 420 | */ |
||
| 421 | public function callForSeenUsers(\Closure $callback) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Getting all userIds that have a listLogin value requires checking the |
||
| 443 | * value in php because on oracle you cannot use a clob in a where clause, |
||
| 444 | * preventing us from doing a not null or length(value) > 0 check. |
||
| 445 | * |
||
| 446 | * @param int $limit |
||
| 447 | * @param int $offset |
||
| 448 | * @return string[] with user ids |
||
| 449 | */ |
||
| 450 | private function getSeenUserIds($limit = null, $offset = null) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param string $email |
||
| 483 | * @return IUser[] |
||
| 484 | * @since 9.1.0 |
||
| 485 | */ |
||
| 486 | public function getByEmail($email) { |
||
| 493 | } |
||
| 494 |
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: