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 |
||
| 41 | class User { |
||
| 42 | /** |
||
| 43 | * @var IUserTools |
||
| 44 | */ |
||
| 45 | protected $access; |
||
| 46 | /** |
||
| 47 | * @var Connection |
||
| 48 | */ |
||
| 49 | protected $connection; |
||
| 50 | /** |
||
| 51 | * @var IConfig |
||
| 52 | */ |
||
| 53 | protected $config; |
||
| 54 | /** |
||
| 55 | * @var FilesystemHelper |
||
| 56 | */ |
||
| 57 | protected $fs; |
||
| 58 | /** |
||
| 59 | * @var \OCP\Image |
||
| 60 | */ |
||
| 61 | protected $image; |
||
| 62 | /** |
||
| 63 | * @var LogWrapper |
||
| 64 | */ |
||
| 65 | protected $log; |
||
| 66 | /** |
||
| 67 | * @var IAvatarManager |
||
| 68 | */ |
||
| 69 | protected $avatarManager; |
||
| 70 | /** |
||
| 71 | * @var IUserManager |
||
| 72 | */ |
||
| 73 | protected $userManager; |
||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $dn; |
||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | protected $uid; |
||
| 82 | /** |
||
| 83 | * @var string[] |
||
| 84 | */ |
||
| 85 | protected $refreshedFeatures = array(); |
||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $avatarImage; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * DB config keys for user preferences |
||
| 93 | */ |
||
| 94 | const USER_PREFKEY_FIRSTLOGIN = 'firstLoginAccomplished'; |
||
| 95 | const USER_PREFKEY_LASTREFRESH = 'lastFeatureRefresh'; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @brief constructor, make sure the subclasses call this one! |
||
| 99 | * @param string $username the internal username |
||
| 100 | * @param string $dn the LDAP DN |
||
| 101 | * @param IUserTools $access an instance that implements IUserTools for |
||
| 102 | * LDAP interaction |
||
| 103 | * @param IConfig $config |
||
| 104 | * @param FilesystemHelper $fs |
||
| 105 | * @param \OCP\Image $image any empty instance |
||
| 106 | * @param LogWrapper $log |
||
| 107 | * @param IAvatarManager $avatarManager |
||
| 108 | * @param IUserManager $userManager |
||
| 109 | */ |
||
| 110 | public function __construct($username, $dn, IUserTools $access, |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @brief updates properties like email, quota or avatar provided by LDAP |
||
| 128 | * @return null |
||
| 129 | */ |
||
| 130 | public function update() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * processes results from LDAP for attributes as returned by getAttributesToRead() |
||
| 154 | * @param array $ldapEntry the user entry as retrieved from LDAP |
||
| 155 | */ |
||
| 156 | public function processAttributes($ldapEntry) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @brief returns the LDAP DN of the user |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getDN() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @brief returns the ownCloud internal username of the user |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getUsername() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * returns the home directory of the user if specified by LDAP settings |
||
| 249 | * @param string $valueFromLDAP |
||
| 250 | * @return bool|string |
||
| 251 | * @throws \Exception |
||
| 252 | */ |
||
| 253 | public function getHomePath($valueFromLDAP = null) { |
||
| 298 | |||
| 299 | public function getMemberOfGroups() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @brief reads the image from LDAP that shall be used as Avatar |
||
| 312 | * @return string data (provided by LDAP) | false |
||
| 313 | */ |
||
| 314 | public function getAvatarImage() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @brief marks the user as having logged in at least once |
||
| 334 | * @return null |
||
| 335 | */ |
||
| 336 | public function markLogin() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @brief marks the time when user features like email have been updated |
||
| 343 | * @return null |
||
| 344 | */ |
||
| 345 | public function markRefreshTime() { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @brief checks whether user features needs to be updated again by |
||
| 352 | * comparing the difference of time of the last refresh to now with the |
||
| 353 | * desired interval |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | private function needsRefresh() { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Stores a key-value pair in relation to this user |
||
| 369 | * |
||
| 370 | * @param string $key |
||
| 371 | * @param string $value |
||
| 372 | */ |
||
| 373 | private function store($key, $value) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Composes the display name and stores it in the database. The final |
||
| 379 | * display name is returned. |
||
| 380 | * |
||
| 381 | * @param string $displayName |
||
| 382 | * @param string $displayName2 |
||
| 383 | * @returns string the effective display name |
||
| 384 | */ |
||
| 385 | public function composeAndStoreDisplayName($displayName, $displayName2 = '') { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Stores the LDAP Username in the Database |
||
| 395 | * @param string $userName |
||
| 396 | */ |
||
| 397 | public function storeLDAPUserName($userName) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @brief checks whether an update method specified by feature was run |
||
| 403 | * already. If not, it will marked like this, because it is expected that |
||
| 404 | * the method will be run, when false is returned. |
||
| 405 | * @param string $feature email | quota | avatar (can be extended) |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | private function wasRefreshed($feature) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * fetches the email from LDAP and stores it as ownCloud user value |
||
| 418 | * @param string $valueFromLDAP if known, to save an LDAP read request |
||
| 419 | * @return null |
||
| 420 | */ |
||
| 421 | public function updateEmail($valueFromLDAP = null) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * fetches the quota from LDAP and stores it as ownCloud user value |
||
| 446 | * @param string $valueFromLDAP the quota attribute's value can be passed, |
||
| 447 | * to save the readAttribute request |
||
| 448 | * @return null |
||
| 449 | */ |
||
| 450 | public function updateQuota($valueFromLDAP = null) { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * called by a post_login hook to save the avatar picture |
||
| 475 | * |
||
| 476 | * @param array $params |
||
| 477 | */ |
||
| 478 | public function updateAvatarPostLogin($params) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @brief attempts to get an image from LDAP and sets it as ownCloud avatar |
||
| 486 | * @return null |
||
| 487 | */ |
||
| 488 | public function updateAvatar() { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @brief sets an image as ownCloud avatar |
||
| 503 | * @return null |
||
| 504 | */ |
||
| 505 | private function setOwnCloudAvatar() { |
||
| 533 | |||
| 534 | } |
||
| 535 |