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 |
||
| 64 | class Manager extends PublicEmitter implements IUserManager { |
||
| 65 | use EventEmitterTrait; |
||
| 66 | /** @var UserInterface[] $backends */ |
||
| 67 | private $backends = []; |
||
| 68 | |||
| 69 | /** @var CappedMemoryCache $cachedUsers */ |
||
| 70 | private $cachedUsers; |
||
| 71 | |||
| 72 | /** @var IConfig $config */ |
||
| 73 | private $config; |
||
| 74 | |||
| 75 | /** @var ILogger $logger */ |
||
| 76 | private $logger; |
||
| 77 | |||
| 78 | /** @var AccountMapper */ |
||
| 79 | private $accountMapper; |
||
| 80 | |||
| 81 | /** @var SyncService */ |
||
| 82 | private $syncService; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param IConfig $config |
||
| 86 | * @param ILogger $logger |
||
| 87 | * @param AccountMapper $accountMapper |
||
| 88 | * @param SyncService $syncService |
||
| 89 | */ |
||
| 90 | public function __construct(IConfig $config, ILogger $logger, AccountMapper $accountMapper, SyncService $syncService) { |
||
| 91 | $this->config = $config; |
||
| 92 | $this->logger = $logger; |
||
| 93 | $this->accountMapper = $accountMapper; |
||
| 94 | $this->cachedUsers = new CappedMemoryCache(); |
||
| 95 | $this->syncService = $syncService; |
||
| 96 | $cachedUsers = &$this->cachedUsers; |
||
| 97 | $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
||
| 98 | /** @var \OC\User\User $user */ |
||
| 99 | unset($cachedUsers[$user->getUID()]); |
||
| 100 | }); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * only used for unit testing |
||
| 105 | * |
||
| 106 | * @param AccountMapper $mapper |
||
| 107 | * @param array $backends |
||
| 108 | * @param SyncService $syncService |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | public function reset(AccountMapper $mapper, $backends, $syncService) { |
||
| 112 | $return = [$this->accountMapper, $this->backends, $this->syncService]; |
||
| 113 | $this->accountMapper = $mapper; |
||
| 114 | $this->backends = $backends; |
||
| 115 | $this->syncService = $syncService; |
||
| 116 | $this->cachedUsers->clear(); |
||
| 117 | |||
| 118 | return $return; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the active backends |
||
| 123 | * @return \OCP\UserInterface[] |
||
| 124 | */ |
||
| 125 | public function getBackends() { |
||
| 126 | return array_values($this->backends); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * register a user backend |
||
| 131 | * |
||
| 132 | * @param \OCP\UserInterface $backend |
||
| 133 | */ |
||
| 134 | public function registerBackend($backend) { |
||
| 135 | $this->backends[get_class($backend)] = $backend; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * remove a user backend |
||
| 140 | * |
||
| 141 | * @param \OCP\UserInterface $backend |
||
| 142 | */ |
||
| 143 | public function removeBackend($backend) { |
||
| 144 | $this->cachedUsers->clear(); |
||
| 145 | unset($this->backends[get_class($backend)]); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * remove all user backends |
||
| 150 | */ |
||
| 151 | public function clearBackends() { |
||
| 152 | $this->cachedUsers->clear(); |
||
| 153 | $this->backends = []; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * get a user by user id |
||
| 158 | * |
||
| 159 | * @param string $uid |
||
| 160 | * @return \OC\User\User|null Either the user or null if the specified user does not exist |
||
| 161 | */ |
||
| 162 | public function get($uid) { |
||
| 163 | if (is_null($uid) || !is_string($uid)) { |
||
| 164 | return null; |
||
| 165 | } |
||
| 166 | if ($this->cachedUsers->hasKey($uid)) { //check the cache first to prevent having to loop over the backends |
||
| 167 | return $this->cachedUsers->get($uid); |
||
| 168 | } |
||
| 169 | try { |
||
| 170 | $account = $this->accountMapper->getByUid($uid); |
||
| 171 | if (is_null($account)) { |
||
| 172 | $this->cachedUsers->set($uid, null); |
||
| 173 | return null; |
||
| 174 | } |
||
| 175 | return $this->getUserObject($account); |
||
| 176 | } catch (DoesNotExistException $ex) { |
||
| 177 | return null; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * get or construct the user object |
||
| 183 | * |
||
| 184 | * @param Account $account |
||
| 185 | * @param bool $cacheUser If false the newly created user object will not be cached |
||
| 186 | * @return \OC\User\User |
||
| 187 | */ |
||
| 188 | protected function getUserObject(Account $account, $cacheUser = true) { |
||
| 189 | if ($this->cachedUsers->hasKey($account->getUserId())) { |
||
| 190 | return $this->cachedUsers->get($account->getUserId()); |
||
| 191 | } |
||
| 192 | |||
| 193 | $user = new User($account, $this->accountMapper, $this, $this->config, null, \OC::$server->getEventDispatcher() ); |
||
|
|
|||
| 194 | if ($cacheUser) { |
||
| 195 | $this->cachedUsers->set($account->getUserId(), $user); |
||
| 196 | } |
||
| 197 | return $user; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * check if a user exists |
||
| 202 | * |
||
| 203 | * @param string $uid |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function userExists($uid) { |
||
| 207 | $user = $this->get($uid); |
||
| 208 | return ($user !== null); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Check if the password is valid for the user |
||
| 213 | * |
||
| 214 | * @param string $loginName |
||
| 215 | * @param string $password |
||
| 216 | * @return mixed the User object on success, false otherwise |
||
| 217 | */ |
||
| 218 | public function checkPassword($loginName, $password) { |
||
| 219 | $loginName = str_replace("\0", '', $loginName); |
||
| 220 | $password = str_replace("\0", '', $password); |
||
| 221 | |||
| 222 | if (empty($this->backends)) { |
||
| 223 | $this->registerBackend(new Database()); |
||
| 224 | } |
||
| 225 | |||
| 226 | foreach ($this->backends as $backend) { |
||
| 227 | if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
||
| 228 | $uid = $backend->checkPassword($loginName, $password); |
||
| 229 | if ($uid !== false) { |
||
| 230 | $account = $this->syncService->createOrSyncAccount($uid, $backend); |
||
| 231 | return $this->getUserObject($account); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | $this->logger->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
||
| 237 | return false; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * search by user id |
||
| 242 | * |
||
| 243 | * @param string $pattern |
||
| 244 | * @param int $limit |
||
| 245 | * @param int $offset |
||
| 246 | * @return \OC\User\User[] |
||
| 247 | */ |
||
| 248 | View Code Duplication | public function search($pattern, $limit = null, $offset = null) { |
|
| 258 | |||
| 259 | /** |
||
| 260 | * find a user account by checking user_id, display name and email fields |
||
| 261 | * |
||
| 262 | * @param string $pattern |
||
| 263 | * @param int $limit |
||
| 264 | * @param int $offset |
||
| 265 | * @return \OC\User\User[] |
||
| 266 | */ |
||
| 267 | View Code Duplication | public function find($pattern, $limit = null, $offset = null) { |
|
| 276 | |||
| 277 | /** |
||
| 278 | * search by displayName |
||
| 279 | * |
||
| 280 | * @param string $pattern |
||
| 281 | * @param int $limit |
||
| 282 | * @param int $offset |
||
| 283 | * @return \OC\User\User[] |
||
| 284 | */ |
||
| 285 | public function searchDisplayName($pattern, $limit = null, $offset = null) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $uid |
||
| 294 | * @param string $password |
||
| 295 | * @throws \Exception |
||
| 296 | * @return bool|IUser the created user or false |
||
| 297 | */ |
||
| 298 | public function createUser($uid, $password) { |
||
| 299 | return $this->emittingCall(function () use (&$uid, &$password) { |
||
| 300 | $l = \OC::$server->getL10N('lib'); |
||
| 301 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $uid |
||
| 357 | * @param UserInterface $backend |
||
| 358 | * @return IUser | null |
||
| 359 | * @deprecated core is responsible for creating accounts, see user_ldap how it is done |
||
| 360 | */ |
||
| 361 | public function createUserFromBackend($uid, $password, $backend) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * returns how many users per backend exist (if supported by backend) |
||
| 377 | * |
||
| 378 | * @param boolean $hasLoggedIn when true only users that have a lastLogin |
||
| 379 | * entry in the preferences table will be affected |
||
| 380 | * @return array|int an array of backend class as key and count number as value |
||
| 381 | * if $hasLoggedIn is true only an int is returned |
||
| 382 | */ |
||
| 383 | public function countUsers($hasLoggedIn = false) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * The callback is executed for each user on each backend. |
||
| 392 | * If the callback returns false no further users will be retrieved. |
||
| 393 | * |
||
| 394 | * @param \Closure $callback |
||
| 395 | * @param string $search |
||
| 396 | * @param boolean $onlySeen when true only users that have a lastLogin entry |
||
| 397 | * in the preferences table will be affected |
||
| 398 | * @since 9.0.0 |
||
| 399 | */ |
||
| 400 | public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * returns how many users have logged in once |
||
| 409 | * |
||
| 410 | * @return int |
||
| 411 | * @since 10.0 |
||
| 412 | */ |
||
| 413 | public function countSeenUsers() { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param \Closure $callback |
||
| 419 | * @since 10.0 |
||
| 420 | */ |
||
| 421 | public function callForSeenUsers (\Closure $callback) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param string $email |
||
| 427 | * @return IUser[] |
||
| 428 | * @since 9.1.0 |
||
| 429 | */ |
||
| 430 | public function getByEmail($email) { |
||
| 439 | |||
| 440 | public function getBackend($backendClass) { |
||
| 446 | |||
| 447 | } |
||
| 448 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: