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 | /** @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 user name |
||
| 128 | * TODO move username to account table |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function getUserName() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * set the user name |
||
| 139 | * TODO move username to account table |
||
| 140 | * |
||
| 141 | * @param string $userName |
||
| 142 | */ |
||
| 143 | public function setUserName($userName) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * get the display name for the user, if no specific display name is set it will fallback to the user id |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function getDisplayName() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * set the displayname for the user |
||
| 170 | * |
||
| 171 | * @param string $displayName |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function setDisplayName($displayName) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * set the email address of the user |
||
| 197 | * |
||
| 198 | * @param string|null $mailAddress |
||
| 199 | * @return void |
||
| 200 | * @since 9.0.0 |
||
| 201 | */ |
||
| 202 | public function setEMailAddress($mailAddress) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * returns the timestamp of the user's last login or 0 if the user did never |
||
| 214 | * login |
||
| 215 | * |
||
| 216 | * @return int |
||
| 217 | */ |
||
| 218 | public function getLastLogin() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * updates the timestamp of the most recent login of this user |
||
| 224 | */ |
||
| 225 | public function updateLastLoginTimestamp() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Delete the user |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function delete() { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Set the password of the user |
||
| 285 | * |
||
| 286 | * @param string $password |
||
| 287 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function setPassword($password, $recoveryPassword = null) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * get the users home folder to mount |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getHome() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get the name of the backend class the user is connected with |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function getBackendClassName() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * check if the backend allows the user to change his avatar on Personal page |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | public function canChangeAvatar() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * check if the backend supports changing passwords |
||
| 359 | * |
||
| 360 | * @return bool |
||
| 361 | */ |
||
| 362 | public function canChangePassword() { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * check if the backend supports changing display names |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | public function canChangeDisplayName() { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * check if the user is enabled |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | public function isEnabled() { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * set the enabled status for the user |
||
| 405 | * |
||
| 406 | * @param bool $enabled |
||
| 407 | */ |
||
| 408 | public function setEnabled($enabled) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * get the users email address |
||
| 423 | * |
||
| 424 | * @return string|null |
||
| 425 | * @since 9.0.0 |
||
| 426 | */ |
||
| 427 | public function getEMailAddress() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * get the users' quota |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | * @since 9.0.0 |
||
| 436 | */ |
||
| 437 | public function getQuota() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * set the users' quota |
||
| 447 | * |
||
| 448 | * @param string $quota |
||
| 449 | * @return void |
||
| 450 | * @since 9.0.0 |
||
| 451 | */ |
||
| 452 | public function setQuota($quota) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * get the avatar image if it exists |
||
| 464 | * |
||
| 465 | * @param int $size |
||
| 466 | * @return IImage|null |
||
| 467 | * @since 9.0.0 |
||
| 468 | */ |
||
| 469 | public function getAvatarImage($size) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * get the federation cloud id |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | * @since 9.0.0 |
||
| 489 | */ |
||
| 490 | public function getCloudId() { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param string $url |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | View Code Duplication | private function removeProtocolFromUrl($url) { |
|
| 509 | |||
| 510 | public function triggerChange($feature, $value = null) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @return string[] |
||
| 518 | * @since 10.0.1 |
||
| 519 | */ |
||
| 520 | public function getSearchTerms() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param string[] $terms |
||
| 530 | * @since 10.0.1 |
||
| 531 | */ |
||
| 532 | public function setSearchTerms(array $terms) { |
||
| 539 | } |
||
| 540 |
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: