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