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() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set the password of the user |
||
| 240 | * |
||
| 241 | * @param string $password |
||
| 242 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function setPassword($password, $recoveryPassword = null) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * get the users home folder to mount |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getHome() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get the name of the backend class the user is connected with |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function getBackendClassName() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * check if the backend allows the user to change his avatar on Personal page |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | public function canChangeAvatar() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * check if the backend supports changing passwords |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | public function canChangePassword() { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * check if the backend supports changing display names |
||
| 313 | * |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function canChangeDisplayName() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * check if the user is enabled |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function isEnabled() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * set the enabled status for the user |
||
| 334 | * |
||
| 335 | * @param bool $enabled |
||
| 336 | */ |
||
| 337 | public function setEnabled($enabled) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * get the users email address |
||
| 345 | * |
||
| 346 | * @return string|null |
||
| 347 | * @since 9.0.0 |
||
| 348 | */ |
||
| 349 | public function getEMailAddress() { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * get the users' quota |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | * @since 9.0.0 |
||
| 358 | */ |
||
| 359 | public function getQuota() { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * set the users' quota |
||
| 369 | * |
||
| 370 | * @param string $quota |
||
| 371 | * @return void |
||
| 372 | * @since 9.0.0 |
||
| 373 | */ |
||
| 374 | public function setQuota($quota) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * get the avatar image if it exists |
||
| 385 | * |
||
| 386 | * @param int $size |
||
| 387 | * @return IImage|null |
||
| 388 | * @since 9.0.0 |
||
| 389 | */ |
||
| 390 | public function getAvatarImage($size) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * get the federation cloud id |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | * @since 9.0.0 |
||
| 410 | */ |
||
| 411 | public function getCloudId() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param string $url |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | View Code Duplication | private function removeProtocolFromUrl($url) { |
|
| 431 | |||
| 432 | public function triggerChange($feature, $value = null) { |
||
| 437 | } |
||
| 438 |
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.