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 |
||
| 50 | class User implements IUser { |
||
| 51 | use EventEmitterTrait; |
||
| 52 | |||
| 53 | /** @var Account */ |
||
| 54 | private $account; |
||
| 55 | |||
| 56 | /** @var Emitter|Manager $emitter */ |
||
| 57 | private $emitter; |
||
| 58 | |||
| 59 | /** @var \OCP\IConfig $config */ |
||
| 60 | private $config; |
||
| 61 | |||
| 62 | /** @var IAvatarManager */ |
||
| 63 | private $avatarManager; |
||
| 64 | |||
| 65 | /** @var IURLGenerator */ |
||
| 66 | private $urlGenerator; |
||
| 67 | |||
| 68 | /** @var EventDispatcher */ |
||
| 69 | private $eventDispatcher; |
||
| 70 | |||
| 71 | /** @var AccountMapper */ |
||
| 72 | private $mapper; |
||
| 73 | |||
| 74 | /** @var \OC\Group\Manager */ |
||
| 75 | private $groupManager; |
||
| 76 | |||
| 77 | /** @var Session */ |
||
| 78 | private $userSession; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * User constructor. |
||
| 82 | * |
||
| 83 | * @param Account $account |
||
| 84 | * @param AccountMapper $mapper |
||
| 85 | * @param null $emitter |
||
| 86 | * @param IConfig|null $config |
||
| 87 | * @param null $urlGenerator |
||
| 88 | * @param EventDispatcher|null $eventDispatcher |
||
| 89 | * @param \OC\Group\Manager|null $groupManager |
||
| 90 | * @param Session|null $userSession |
||
| 91 | */ |
||
| 92 | public function __construct(Account $account, AccountMapper $mapper, $emitter = null, IConfig $config = null, |
||
| 117 | |||
| 118 | /** |
||
| 119 | * get the user id |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function getUID() { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * get the user name |
||
| 129 | * TODO move username to account table |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getUserName() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * set the user name |
||
| 140 | * TODO move username to account table |
||
| 141 | * |
||
| 142 | * @param string $userName |
||
| 143 | */ |
||
| 144 | public function setUserName($userName) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * get the display name for the user, if no specific display name is set it will fallback to the user id |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getDisplayName() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * set the displayname for the user |
||
| 171 | * |
||
| 172 | * @param string $displayName |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public function setDisplayName($displayName) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * set the email address of the user |
||
| 198 | * |
||
| 199 | * @param string|null $mailAddress |
||
| 200 | * @return void |
||
| 201 | * @since 9.0.0 |
||
| 202 | */ |
||
| 203 | public function setEMailAddress($mailAddress) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * returns the timestamp of the user's last login or 0 if the user did never |
||
| 215 | * login |
||
| 216 | * |
||
| 217 | * @return int |
||
| 218 | */ |
||
| 219 | public function getLastLogin() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * updates the timestamp of the most recent login of this user |
||
| 225 | */ |
||
| 226 | public function updateLastLoginTimestamp() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Delete the user |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function delete() { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Set the user's password |
||
| 286 | * |
||
| 287 | * @param string $password |
||
| 288 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
| 289 | * @return bool |
||
| 290 | * @throws \InvalidArgumentException |
||
| 291 | */ |
||
| 292 | public function setPassword($password, $recoveryPassword = null) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * get the users home folder to mount |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function getHome() { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get the name of the backend class the user is connected with |
||
| 336 | * |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | public function getBackendClassName() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * check if the backend allows the user to change his avatar on Personal page |
||
| 349 | * |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function canChangeAvatar() { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * check if the backend supports changing passwords |
||
| 365 | * |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | public function canChangePassword() { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * check if the backend supports changing display names |
||
| 378 | * |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | public function canChangeDisplayName() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * check if the user is enabled |
||
| 402 | * |
||
| 403 | * @return bool |
||
| 404 | */ |
||
| 405 | public function isEnabled() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * set the enabled status for the user |
||
| 411 | * |
||
| 412 | * @param bool $enabled |
||
| 413 | */ |
||
| 414 | public function setEnabled($enabled) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * get the users email address |
||
| 429 | * |
||
| 430 | * @return string|null |
||
| 431 | * @since 9.0.0 |
||
| 432 | */ |
||
| 433 | public function getEMailAddress() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * get the users' quota |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | * @since 9.0.0 |
||
| 442 | */ |
||
| 443 | public function getQuota() { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * set the users' quota |
||
| 453 | * |
||
| 454 | * @param string $quota |
||
| 455 | * @return void |
||
| 456 | * @since 9.0.0 |
||
| 457 | */ |
||
| 458 | public function setQuota($quota) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * get the avatar image if it exists |
||
| 470 | * |
||
| 471 | * @param int $size |
||
| 472 | * @return IImage|null |
||
| 473 | * @since 9.0.0 |
||
| 474 | */ |
||
| 475 | public function getAvatarImage($size) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * get the federation cloud id |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | * @since 9.0.0 |
||
| 495 | */ |
||
| 496 | public function getCloudId() { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @param string $url |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | View Code Duplication | private function removeProtocolFromUrl($url) { |
|
| 515 | |||
| 516 | public function triggerChange($feature, $value = null) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @return string[] |
||
| 524 | * @since 10.0.1 |
||
| 525 | */ |
||
| 526 | public function getSearchTerms() { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param string[] $terms |
||
| 536 | * @since 10.0.1 |
||
| 537 | */ |
||
| 538 | public function setSearchTerms(array $terms) { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @return integer |
||
| 548 | * @since 11.0.0 |
||
| 549 | */ |
||
| 550 | public function getAccountId() { |
||
| 553 | } |
||
| 554 |
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: