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 |
||
| 44 | class User implements IUser { |
||
| 45 | /** @var string $uid */ |
||
| 46 | private $uid; |
||
| 47 | |||
| 48 | /** @var string $displayName */ |
||
| 49 | private $displayName; |
||
| 50 | |||
| 51 | /** @var UserInterface $backend */ |
||
| 52 | private $backend; |
||
| 53 | |||
| 54 | /** @var bool $enabled */ |
||
| 55 | private $enabled; |
||
| 56 | |||
| 57 | /** @var Emitter|Manager $emitter */ |
||
| 58 | private $emitter; |
||
| 59 | |||
| 60 | /** @var string $home */ |
||
| 61 | private $home; |
||
| 62 | |||
| 63 | /** @var int $lastLogin */ |
||
| 64 | private $lastLogin; |
||
| 65 | |||
| 66 | /** @var \OCP\IConfig $config */ |
||
| 67 | private $config; |
||
| 68 | |||
| 69 | /** @var IAvatarManager */ |
||
| 70 | private $avatarManager; |
||
| 71 | |||
| 72 | /** @var IURLGenerator */ |
||
| 73 | private $urlGenerator; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param string $uid |
||
| 77 | * @param UserInterface $backend |
||
| 78 | * @param \OC\Hooks\Emitter $emitter |
||
| 79 | * @param IConfig|null $config |
||
| 80 | * @param IURLGenerator $urlGenerator |
||
| 81 | */ |
||
| 82 | public function __construct($uid, $backend, $emitter = null, IConfig $config = null, $urlGenerator = null) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * get the user id |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function getUID() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * get the display name for the user, if no specific display name is set it will fallback to the user id |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function getDisplayName() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * set the displayname for the user |
||
| 135 | * |
||
| 136 | * @param string $displayName |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | public function setDisplayName($displayName) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * set the email address of the user |
||
| 155 | * |
||
| 156 | * @param string|null $mailAddress |
||
| 157 | * @return void |
||
| 158 | * @since 9.0.0 |
||
| 159 | */ |
||
| 160 | public function setEMailAddress($mailAddress) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * returns the timestamp of the user's last login or 0 if the user did never |
||
| 171 | * login |
||
| 172 | * |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | public function getLastLogin() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * updates the timestamp of the most recent login of this user |
||
| 181 | */ |
||
| 182 | public function updateLastLoginTimestamp() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Delete the user |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | public function delete() { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set the password of the user |
||
| 246 | * |
||
| 247 | * @param string $password |
||
| 248 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function setPassword($password, $recoveryPassword = null) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * get the users home folder to mount |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function getHome() { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get the name of the backend class the user is connected with |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getBackendClassName() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * check if the backend allows the user to change his avatar on Personal page |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | public function canChangeAvatar() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * check if the backend supports changing passwords |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public function canChangePassword() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * check if the backend supports changing display names |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public function canChangeDisplayName() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * check if the user is enabled |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function isEnabled() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * set the enabled status for the user |
||
| 340 | * |
||
| 341 | * @param bool $enabled |
||
| 342 | */ |
||
| 343 | public function setEnabled($enabled) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * get the users email address |
||
| 351 | * |
||
| 352 | * @return string|null |
||
| 353 | * @since 9.0.0 |
||
| 354 | */ |
||
| 355 | public function getEMailAddress() { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * get the users' quota |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | * @since 9.0.0 |
||
| 364 | */ |
||
| 365 | public function getQuota() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * set the users' quota |
||
| 375 | * |
||
| 376 | * @param string $quota |
||
| 377 | * @return void |
||
| 378 | * @since 9.0.0 |
||
| 379 | */ |
||
| 380 | public function setQuota($quota) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * get the avatar image if it exists |
||
| 391 | * |
||
| 392 | * @param int $size |
||
| 393 | * @return IImage|null |
||
| 394 | * @since 9.0.0 |
||
| 395 | */ |
||
| 396 | public function getAvatarImage($size) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * get the federation cloud id |
||
| 413 | * |
||
| 414 | * @return string |
||
| 415 | * @since 9.0.0 |
||
| 416 | */ |
||
| 417 | public function getCloudId() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $url |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | View Code Duplication | private function removeProtocolFromUrl($url) { |
|
| 437 | |||
| 438 | public function triggerChange($feature, $value = null) { |
||
| 443 | } |
||
| 444 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.