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 |
||
| 35 | class User { |
||
| 36 | /** |
||
| 37 | * @var IUserTools |
||
| 38 | */ |
||
| 39 | protected $access; |
||
| 40 | /** |
||
| 41 | * @var Connection |
||
| 42 | */ |
||
| 43 | protected $connection; |
||
| 44 | /** |
||
| 45 | * @var \OCP\IConfig |
||
| 46 | */ |
||
| 47 | protected $config; |
||
| 48 | /** |
||
| 49 | * @var FilesystemHelper |
||
| 50 | */ |
||
| 51 | protected $fs; |
||
| 52 | /** |
||
| 53 | * @var \OCP\Image |
||
| 54 | */ |
||
| 55 | protected $image; |
||
| 56 | /** |
||
| 57 | * @var LogWrapper |
||
| 58 | */ |
||
| 59 | protected $log; |
||
| 60 | /** |
||
| 61 | * @var \OCP\IAvatarManager |
||
| 62 | */ |
||
| 63 | protected $avatarManager; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $dn; |
||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | protected $uid; |
||
| 73 | /** |
||
| 74 | * @var string[] |
||
| 75 | */ |
||
| 76 | protected $refreshedFeatures = array(); |
||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $avatarImage; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * DB config keys for user preferences |
||
| 84 | */ |
||
| 85 | const USER_PREFKEY_FIRSTLOGIN = 'firstLoginAccomplished'; |
||
| 86 | const USER_PREFKEY_LASTREFRESH = 'lastFeatureRefresh'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @brief constructor, make sure the subclasses call this one! |
||
| 90 | * @param string the internal username |
||
| 91 | * @param string the LDAP DN |
||
| 92 | * @param IUserTools $access an instance that implements IUserTools for |
||
| 93 | * LDAP interaction |
||
| 94 | * @param \OCP\IConfig |
||
| 95 | * @param FilesystemHelper |
||
| 96 | * @param \OCP\Image any empty instance |
||
| 97 | * @param LogWrapper |
||
| 98 | * @param \OCP\IAvatarManager |
||
| 99 | */ |
||
| 100 | 43 | public function __construct($username, $dn, IUserTools $access, |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @brief updates properties like email, quota or avatar provided by LDAP |
||
| 117 | * @return null |
||
| 118 | */ |
||
| 119 | 9 | public function update() { |
|
| 140 | |||
| 141 | /** |
||
| 142 | * processes results from LDAP for attributes as returned by getAttributesToRead() |
||
| 143 | * @param array $ldapEntry the user entry as retrieved from LDAP |
||
| 144 | */ |
||
| 145 | 3 | public function processAttributes($ldapEntry) { |
|
| 219 | |||
| 220 | 10 | /** |
|
| 221 | 10 | * @brief returns the LDAP DN of the user |
|
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function getDN() { |
||
| 227 | |||
| 228 | 12 | /** |
|
| 229 | 12 | * @brief returns the ownCloud internal username of the user |
|
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function getUsername() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * returns the home directory of the user if specified by LDAP settings |
||
| 238 | 6 | * @param string $valueFromLDAP |
|
| 239 | 6 | * @return bool|string |
|
| 240 | 6 | * @throws \Exception |
|
| 241 | */ |
||
| 242 | 6 | public function getHomePath($valueFromLDAP = null) { |
|
| 287 | |||
| 288 | public function getMemberOfGroups() { |
||
| 297 | |||
| 298 | 11 | /** |
|
| 299 | 11 | * @brief reads the image from LDAP that shall be used as Avatar |
|
| 300 | 1 | * @return string data (provided by LDAP) | false |
|
| 301 | */ |
||
| 302 | public function getAvatarImage() { |
||
| 319 | |||
| 320 | 3 | /** |
|
| 321 | 3 | * @brief marks the user as having logged in at least once |
|
| 322 | 3 | * @return null |
|
| 323 | 3 | */ |
|
| 324 | public function markLogin() { |
||
| 328 | |||
| 329 | 9 | /** |
|
| 330 | 9 | * @brief marks the time when user features like email have been updated |
|
| 331 | 9 | * @return null |
|
| 332 | 9 | */ |
|
| 333 | public function markRefreshTime() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @brief checks whether user features needs to be updated again by |
||
| 340 | 9 | * comparing the difference of time of the last refresh to now with the |
|
| 341 | 9 | * desired interval |
|
| 342 | 9 | * @return bool |
|
| 343 | */ |
||
| 344 | private function needsRefresh() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Stores a key-value pair in relation to this user |
||
| 357 | 5 | * |
|
| 358 | 5 | * @param string $key |
|
| 359 | 5 | * @param string $value |
|
| 360 | */ |
||
| 361 | private function store($key, $value) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Composes the display name and stores it in the database. The final |
||
| 367 | * display name is returned. |
||
| 368 | * |
||
| 369 | 5 | * @param string $displayName |
|
| 370 | 5 | * @param string $displayName2 |
|
| 371 | 1 | * @returns string the effective display name |
|
| 372 | 1 | */ |
|
| 373 | 5 | public function composeAndStoreDisplayName($displayName, $displayName2 = '') { |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Stores the LDAP Username in the Database |
||
| 383 | * @param string $userName |
||
| 384 | */ |
||
| 385 | public function storeLDAPUserName($userName) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @brief checks whether an update method specified by feature was run |
||
| 391 | * already. If not, it will marked like this, because it is expected that |
||
| 392 | 20 | * the method will be run, when false is returned. |
|
| 393 | 20 | * @param string email | quota | avatar (can be extended) |
|
| 394 | 1 | * @return bool |
|
| 395 | */ |
||
| 396 | 20 | private function wasRefreshed($feature) { |
|
| 403 | |||
| 404 | /** |
||
| 405 | 11 | * fetches the email from LDAP and stores it as ownCloud user value |
|
| 406 | 11 | * @param string $valueFromLDAP if known, to save an LDAP read request |
|
| 407 | 1 | * @return null |
|
| 408 | */ |
||
| 409 | 11 | public function updateEmail($valueFromLDAP = null) { |
|
| 428 | |||
| 429 | /** |
||
| 430 | * fetches the quota from LDAP and stores it as ownCloud user value |
||
| 431 | 14 | * @param string $valueFromLDAP the quota attribute's value can be passed, |
|
| 432 | 14 | * to save the readAttribute request |
|
| 433 | 1 | * @return null |
|
| 434 | */ |
||
| 435 | public function updateQuota($valueFromLDAP = null) { |
||
| 457 | |||
| 458 | 10 | /** |
|
| 459 | 10 | * called by a post_login hook to save the avatar picture |
|
| 460 | 1 | * |
|
| 461 | * @param array $params |
||
| 462 | 10 | */ |
|
| 463 | 10 | public function updateAvatarPostLogin($params) { |
|
| 468 | 2 | ||
| 469 | 2 | /** |
|
| 470 | * @brief attempts to get an image from LDAP and sets it as ownCloud avatar |
||
| 471 | * @return null |
||
| 472 | */ |
||
| 473 | public function updateAvatar() { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @brief sets an image as ownCloud avatar |
||
| 488 | * @return null |
||
| 489 | */ |
||
| 490 | 2 | private function setOwnCloudAvatar() { |
|
| 518 | |||
| 519 | } |
||
| 520 |