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 |
||
| 43 | class User implements IUser { |
||
| 44 | /** @var string $uid */ |
||
| 45 | private $uid; |
||
| 46 | |||
| 47 | /** @var string $displayName */ |
||
| 48 | private $displayName; |
||
| 49 | |||
| 50 | /** @var UserInterface $backend */ |
||
| 51 | private $backend; |
||
| 52 | |||
| 53 | /** @var bool $enabled */ |
||
| 54 | private $enabled; |
||
| 55 | |||
| 56 | /** @var Emitter|Manager $emitter */ |
||
| 57 | private $emitter; |
||
| 58 | |||
| 59 | /** @var string $home */ |
||
| 60 | private $home; |
||
| 61 | |||
| 62 | /** @var int $lastLogin */ |
||
| 63 | private $lastLogin; |
||
| 64 | |||
| 65 | /** @var \OCP\IConfig $config */ |
||
| 66 | private $config; |
||
| 67 | |||
| 68 | /** @var IAvatarManager */ |
||
| 69 | private $avatarManager; |
||
| 70 | |||
| 71 | /** @var IURLGenerator */ |
||
| 72 | private $urlGenerator; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param string $uid |
||
| 76 | * @param UserInterface $backend |
||
| 77 | * @param \OC\Hooks\Emitter $emitter |
||
| 78 | * @param IConfig|null $config |
||
| 79 | * @param IURLGenerator $urlGenerator |
||
| 80 | */ |
||
| 81 | public function __construct($uid, $backend, $emitter = null, IConfig $config = null, $urlGenerator = null) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * get the user id |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getUID() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * get the display name for the user, if no specific display name is set it will fallback to the user id |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function getDisplayName() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * set the displayname for the user |
||
| 134 | * |
||
| 135 | * @param string $displayName |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | public function setDisplayName($displayName) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * set the email address of the user |
||
| 154 | * |
||
| 155 | * @param string|null $mailAddress |
||
| 156 | * @return void |
||
| 157 | * @since 9.0.0 |
||
| 158 | */ |
||
| 159 | public function setEMailAddress($mailAddress) { |
||
| 160 | if(is_null($mailAddress) || $mailAddress === '') { |
||
| 161 | $this->config->deleteUserValue($this->uid, 'settings', 'email'); |
||
| 162 | } else { |
||
| 163 | $this->config->setUserValue($this->uid, 'settings', 'email', $mailAddress); |
||
| 164 | } |
||
| 165 | $this->config->deleteUserValue($this->getUID(), 'owncloud', 'lostpassword'); |
||
| 166 | $this->triggerChange('eMailAddress', $mailAddress); |
||
| 167 | } |
||
| 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() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Delete the user |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function delete() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set the password of the user |
||
| 227 | * |
||
| 228 | * @param string $password |
||
| 229 | * @param string $recoveryPassword for the encryption app to reset encryption keys |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | public function setPassword($password, $recoveryPassword = null) { |
||
| 233 | if ($this->emitter) { |
||
| 234 | $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword)); |
||
| 235 | } |
||
| 236 | if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { |
||
| 237 | $result = $this->backend->setPassword($this->uid, $password); |
||
| 238 | if ($result) { |
||
| 239 | if ($this->emitter) { |
||
| 240 | $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword)); |
||
| 241 | } |
||
| 242 | $this->config->deleteUserValue($this->getUID(), 'owncloud', 'lostpassword'); |
||
| 243 | } |
||
| 244 | return !($result === false); |
||
| 245 | } else { |
||
| 246 | return false; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * get the users home folder to mount |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function getHome() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get the name of the backend class the user is connected with |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getBackendClassName() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * check if the backend allows the user to change his avatar on Personal page |
||
| 282 | * |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | public function canChangeAvatar() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * check if the backend supports changing passwords |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public function canChangePassword() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * check if the backend supports changing display names |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | public function canChangeDisplayName() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * check if the user is enabled |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | public function isEnabled() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * set the enabled status for the user |
||
| 324 | * |
||
| 325 | * @param bool $enabled |
||
| 326 | */ |
||
| 327 | public function setEnabled($enabled) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * get the users email address |
||
| 335 | * |
||
| 336 | * @return string|null |
||
| 337 | * @since 9.0.0 |
||
| 338 | */ |
||
| 339 | public function getEMailAddress() { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * get the users' quota |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | * @since 9.0.0 |
||
| 348 | */ |
||
| 349 | public function getQuota() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * set the users' quota |
||
| 359 | * |
||
| 360 | * @param string $quota |
||
| 361 | * @return void |
||
| 362 | * @since 9.0.0 |
||
| 363 | */ |
||
| 364 | public function setQuota($quota) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * get the avatar image if it exists |
||
| 375 | * |
||
| 376 | * @param int $size |
||
| 377 | * @return IImage|null |
||
| 378 | * @since 9.0.0 |
||
| 379 | */ |
||
| 380 | public function getAvatarImage($size) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * get the federation cloud id |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | * @since 9.0.0 |
||
| 400 | */ |
||
| 401 | public function getCloudId() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param string $url |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | View Code Duplication | private function removeProtocolFromUrl($url) { |
|
| 420 | |||
| 421 | public function triggerChange($feature, $value = null) { |
||
| 426 | } |
||
| 427 |
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.