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 User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class User implements IUser { |
||
| 50 | use EventEmitterTrait; |
||
| 51 | |||
| 52 | /** @var Account */ |
||
| 53 | private $account; |
||
| 54 | |||
| 55 | /** @var Emitter|Manager $emitter */ |
||
| 56 | private $emitter; |
||
| 57 | |||
| 58 | /** @var \OCP\IConfig $config */ |
||
| 59 | private $config; |
||
| 60 | |||
| 61 | /** @var IAvatarManager */ |
||
| 62 | private $avatarManager; |
||
| 63 | |||
| 64 | /** @var IURLGenerator */ |
||
| 65 | private $urlGenerator; |
||
| 66 | |||
| 67 | /** @var EventDispatcher */ |
||
| 68 | private $eventDispatcher; |
||
| 69 | |||
| 70 | /** @var AccountMapper */ |
||
| 71 | private $mapper; |
||
| 72 | |||
| 73 | /** @var \OC\Group\Manager */ |
||
| 74 | private $groupManager; |
||
| 75 | |||
| 76 | /** @var Session */ |
||
| 77 | private $userSession; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * User constructor. |
||
| 81 | * |
||
| 82 | * @param Account $account |
||
| 83 | * @param AccountMapper $mapper |
||
| 84 | * @param null $emitter |
||
| 85 | * @param IConfig|null $config |
||
| 86 | * @param null $urlGenerator |
||
| 87 | * @param EventDispatcher|null $eventDispatcher |
||
| 88 | * @param \OC\Group\Manager|null $groupManager |
||
| 89 | * @param Session|null $userSession |
||
| 90 | */ |
||
| 91 | public function __construct(Account $account, AccountMapper $mapper, $emitter = null, IConfig $config = null, |
||
| 116 | |||
| 117 | /** |
||
| 118 | * get the user id |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function getUID() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * get the display name for the user, if no specific display name is set it will fallback to the user id |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function getDisplayName() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * set the displayname for the user |
||
| 141 | * |
||
| 142 | * @param string $displayName |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function setDisplayName($displayName) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * set the email address of the user |
||
| 168 | * |
||
| 169 | * @param string|null $mailAddress |
||
| 170 | * @return void |
||
| 171 | * @since 9.0.0 |
||
| 172 | */ |
||
| 173 | public function setEMailAddress($mailAddress) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * returns the timestamp of the user's last login or 0 if the user did never |
||
| 185 | * login |
||
| 186 | * |
||
| 187 | * @return int |
||
| 188 | */ |
||
| 189 | public function getLastLogin() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * updates the timestamp of the most recent login of this user |
||
| 195 | */ |
||
| 196 | public function updateLastLoginTimestamp() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Delete the user |
||
| 205 | * |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | public function delete() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set the user's password |
||
| 256 | * |
||
| 257 | * @param string $password |
||
| 258 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
| 259 | * @return bool |
||
| 260 | * @throws \InvalidArgumentException |
||
| 261 | */ |
||
| 262 | public function setPassword($password, $recoveryPassword = null) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * get the users home folder to mount |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getHome() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the name of the backend class the user is connected with |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function getBackendClassName() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * check if the backend allows the user to change his avatar on Personal page |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public function canChangeAvatar() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * check if the backend supports changing passwords |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function canChangePassword() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * check if the backend supports changing display names |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | public function canChangeDisplayName() { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * check if the user is enabled |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | public function isEnabled() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * set the enabled status for the user |
||
| 381 | * |
||
| 382 | * @param bool $enabled |
||
| 383 | */ |
||
| 384 | public function setEnabled($enabled) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * get the users email address |
||
| 399 | * |
||
| 400 | * @return string|null |
||
| 401 | * @since 9.0.0 |
||
| 402 | */ |
||
| 403 | public function getEMailAddress() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * get the users' quota |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | * @since 9.0.0 |
||
| 412 | */ |
||
| 413 | public function getQuota() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * set the users' quota |
||
| 423 | * |
||
| 424 | * @param string $quota |
||
| 425 | * @return void |
||
| 426 | * @since 9.0.0 |
||
| 427 | */ |
||
| 428 | public function setQuota($quota) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * get the avatar image if it exists |
||
| 440 | * |
||
| 441 | * @param int $size |
||
| 442 | * @return IImage|null |
||
| 443 | * @since 9.0.0 |
||
| 444 | */ |
||
| 445 | public function getAvatarImage($size) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * get the federation cloud id |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | * @since 9.0.0 |
||
| 465 | */ |
||
| 466 | public function getCloudId() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param string $url |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | View Code Duplication | private function removeProtocolFromUrl($url) { |
|
| 485 | |||
| 486 | public function triggerChange($feature, $value = null) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @return string[] |
||
| 494 | * @since 10.0.1 |
||
| 495 | */ |
||
| 496 | public function getSearchTerms() { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @param string[] $terms |
||
| 506 | * @since 10.0.1 |
||
| 507 | */ |
||
| 508 | public function setSearchTerms(array $terms) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @return integer |
||
| 518 | * @since 11.0.0 |
||
| 519 | */ |
||
| 520 | public function getAccountId() { |
||
| 523 | } |
||
| 524 |
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: