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 UsersController 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 UsersController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class UsersController extends Controller { |
||
| 61 | /** @var IL10N */ |
||
| 62 | private $l10n; |
||
| 63 | /** @var IUserSession */ |
||
| 64 | private $userSession; |
||
| 65 | /** @var bool */ |
||
| 66 | private $isAdmin; |
||
| 67 | /** @var IUserManager */ |
||
| 68 | private $userManager; |
||
| 69 | /** @var IGroupManager */ |
||
| 70 | private $groupManager; |
||
| 71 | /** @var IConfig */ |
||
| 72 | private $config; |
||
| 73 | /** @var ILogger */ |
||
| 74 | private $log; |
||
| 75 | /** @var IMailer */ |
||
| 76 | private $mailer; |
||
| 77 | /** @var bool contains the state of the encryption app */ |
||
| 78 | private $isEncryptionAppEnabled; |
||
| 79 | /** @var bool contains the state of the admin recovery setting */ |
||
| 80 | private $isRestoreEnabled = false; |
||
| 81 | /** @var IAvatarManager */ |
||
| 82 | private $avatarManager; |
||
| 83 | /** @var AccountManager */ |
||
| 84 | private $accountManager; |
||
| 85 | /** @var ISecureRandom */ |
||
| 86 | private $secureRandom; |
||
| 87 | /** @var NewUserMailHelper */ |
||
| 88 | private $newUserMailHelper; |
||
| 89 | /** @var ITimeFactory */ |
||
| 90 | private $timeFactory; |
||
| 91 | /** @var ICrypto */ |
||
| 92 | private $crypto; |
||
| 93 | /** @var Manager */ |
||
| 94 | private $keyManager; |
||
| 95 | /** @var IJobList */ |
||
| 96 | private $jobList; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $appName |
||
| 100 | * @param IRequest $request |
||
| 101 | * @param IUserManager $userManager |
||
| 102 | * @param IGroupManager $groupManager |
||
| 103 | * @param IUserSession $userSession |
||
| 104 | * @param IConfig $config |
||
| 105 | * @param bool $isAdmin |
||
| 106 | * @param IL10N $l10n |
||
| 107 | * @param ILogger $log |
||
| 108 | * @param IMailer $mailer |
||
| 109 | * @param IURLGenerator $urlGenerator |
||
| 110 | * @param IAppManager $appManager |
||
| 111 | * @param IAvatarManager $avatarManager |
||
| 112 | * @param AccountManager $accountManager |
||
| 113 | * @param ISecureRandom $secureRandom |
||
| 114 | * @param NewUserMailHelper $newUserMailHelper |
||
| 115 | * @param ITimeFactory $timeFactory |
||
| 116 | * @param ICrypto $crypto |
||
| 117 | * @param Manager $keyManager |
||
| 118 | * @param IJobList $jobList |
||
| 119 | */ |
||
| 120 | public function __construct($appName, |
||
| 121 | IRequest $request, |
||
| 122 | IUserManager $userManager, |
||
| 123 | IGroupManager $groupManager, |
||
| 124 | IUserSession $userSession, |
||
| 125 | IConfig $config, |
||
| 126 | $isAdmin, |
||
| 127 | IL10N $l10n, |
||
| 128 | ILogger $log, |
||
| 129 | IMailer $mailer, |
||
| 130 | IURLGenerator $urlGenerator, |
||
|
|
|||
| 131 | IAppManager $appManager, |
||
| 132 | IAvatarManager $avatarManager, |
||
| 133 | AccountManager $accountManager, |
||
| 134 | ISecureRandom $secureRandom, |
||
| 135 | NewUserMailHelper $newUserMailHelper, |
||
| 136 | ITimeFactory $timeFactory, |
||
| 137 | ICrypto $crypto, |
||
| 138 | Manager $keyManager, |
||
| 139 | IJobList $jobList) { |
||
| 140 | parent::__construct($appName, $request); |
||
| 141 | $this->userManager = $userManager; |
||
| 142 | $this->groupManager = $groupManager; |
||
| 143 | $this->userSession = $userSession; |
||
| 144 | $this->config = $config; |
||
| 145 | $this->isAdmin = $isAdmin; |
||
| 146 | $this->l10n = $l10n; |
||
| 147 | $this->log = $log; |
||
| 148 | $this->mailer = $mailer; |
||
| 149 | $this->avatarManager = $avatarManager; |
||
| 150 | $this->accountManager = $accountManager; |
||
| 151 | $this->secureRandom = $secureRandom; |
||
| 152 | $this->newUserMailHelper = $newUserMailHelper; |
||
| 153 | $this->timeFactory = $timeFactory; |
||
| 154 | $this->crypto = $crypto; |
||
| 155 | $this->keyManager = $keyManager; |
||
| 156 | $this->jobList = $jobList; |
||
| 157 | |||
| 158 | // check for encryption state - TODO see formatUserForIndex |
||
| 159 | $this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption'); |
||
| 160 | if($this->isEncryptionAppEnabled) { |
||
| 161 | // putting this directly in empty is possible in PHP 5.5+ |
||
| 162 | $result = $config->getAppValue('encryption', 'recoveryAdminEnabled', 0); |
||
| 163 | $this->isRestoreEnabled = !empty($result); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param IUser $user |
||
| 169 | * @param array $userGroups |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | private function formatUserForIndex(IUser $user, array $userGroups = null) { |
||
| 173 | |||
| 174 | // TODO: eliminate this encryption specific code below and somehow |
||
| 175 | // hook in additional user info from other apps |
||
| 176 | |||
| 177 | // recovery isn't possible if admin or user has it disabled and encryption |
||
| 178 | // is enabled - so we eliminate the else paths in the conditional tree |
||
| 179 | // below |
||
| 180 | $restorePossible = false; |
||
| 181 | |||
| 182 | if ($this->isEncryptionAppEnabled) { |
||
| 183 | if ($this->isRestoreEnabled) { |
||
| 184 | // check for the users recovery setting |
||
| 185 | $recoveryMode = $this->config->getUserValue($user->getUID(), 'encryption', 'recoveryEnabled', '0'); |
||
| 186 | // method call inside empty is possible with PHP 5.5+ |
||
| 187 | $recoveryModeEnabled = !empty($recoveryMode); |
||
| 188 | if ($recoveryModeEnabled) { |
||
| 189 | // user also has recovery mode enabled |
||
| 190 | $restorePossible = true; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } else { |
||
| 194 | // recovery is possible if encryption is disabled (plain files are |
||
| 195 | // available) |
||
| 196 | $restorePossible = true; |
||
| 197 | } |
||
| 198 | |||
| 199 | $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user); |
||
| 200 | foreach($subAdminGroups as $key => $subAdminGroup) { |
||
| 201 | $subAdminGroups[$key] = $subAdminGroup->getGID(); |
||
| 202 | } |
||
| 203 | |||
| 204 | $displayName = $user->getEMailAddress(); |
||
| 205 | if (is_null($displayName)) { |
||
| 206 | $displayName = ''; |
||
| 207 | } |
||
| 208 | |||
| 209 | $avatarAvailable = false; |
||
| 210 | try { |
||
| 211 | $avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists(); |
||
| 212 | } catch (\Exception $e) { |
||
| 213 | //No avatar yet |
||
| 214 | } |
||
| 215 | |||
| 216 | return [ |
||
| 217 | 'name' => $user->getUID(), |
||
| 218 | 'displayname' => $user->getDisplayName(), |
||
| 219 | 'groups' => (empty($userGroups)) ? $this->groupManager->getUserGroupIds($user) : $userGroups, |
||
| 220 | 'subadmin' => $subAdminGroups, |
||
| 221 | 'quota' => $user->getQuota(), |
||
| 222 | 'storageLocation' => $user->getHome(), |
||
| 223 | 'lastLogin' => $user->getLastLogin() * 1000, |
||
| 224 | 'backend' => $user->getBackendClassName(), |
||
| 225 | 'email' => $displayName, |
||
| 226 | 'isRestoreDisabled' => !$restorePossible, |
||
| 227 | 'isAvatarAvailable' => $avatarAvailable, |
||
| 228 | 'isEnabled' => $user->isEnabled(), |
||
| 229 | ]; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param array $userIDs Array with schema [$uid => $displayName] |
||
| 234 | * @return IUser[] |
||
| 235 | */ |
||
| 236 | private function getUsersForUID(array $userIDs) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @NoAdminRequired |
||
| 246 | * |
||
| 247 | * @param int $offset |
||
| 248 | * @param int $limit |
||
| 249 | * @param string $gid GID to filter for |
||
| 250 | * @param string $pattern Pattern to search for in the username |
||
| 251 | * @param string $backend Backend to filter for (class-name) |
||
| 252 | * @return DataResponse |
||
| 253 | * |
||
| 254 | * TODO: Tidy up and write unit tests - code is mainly static method calls |
||
| 255 | */ |
||
| 256 | public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @NoAdminRequired |
||
| 333 | * @PasswordConfirmationRequired |
||
| 334 | * |
||
| 335 | * @param string $username |
||
| 336 | * @param string $password |
||
| 337 | * @param array $groups |
||
| 338 | * @param string $email |
||
| 339 | * @return DataResponse |
||
| 340 | */ |
||
| 341 | public function create($username, $password, array $groups=[], $email='') { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @NoAdminRequired |
||
| 460 | * @PasswordConfirmationRequired |
||
| 461 | * |
||
| 462 | * @param string $id |
||
| 463 | * @return DataResponse |
||
| 464 | */ |
||
| 465 | public function destroy($id) { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @NoAdminRequired |
||
| 520 | * |
||
| 521 | * @param string $id |
||
| 522 | * @param int $enabled |
||
| 523 | * @return DataResponse |
||
| 524 | */ |
||
| 525 | public function setEnabled($id, $enabled) { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Set the mail address of a user |
||
| 586 | * |
||
| 587 | * @NoAdminRequired |
||
| 588 | * @NoSubadminRequired |
||
| 589 | * @PasswordConfirmationRequired |
||
| 590 | * |
||
| 591 | * @param string $account |
||
| 592 | * @param bool $onlyVerificationCode only return verification code without updating the data |
||
| 593 | * @return DataResponse |
||
| 594 | */ |
||
| 595 | public function getVerificationCode($account, $onlyVerificationCode) { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * get current timestamp |
||
| 651 | * |
||
| 652 | * @return int |
||
| 653 | */ |
||
| 654 | protected function getCurrentTime() { |
||
| 657 | |||
| 658 | /** |
||
| 659 | * sign message with users private key |
||
| 660 | * |
||
| 661 | * @param IUser $user |
||
| 662 | * @param string $message |
||
| 663 | * |
||
| 664 | * @return string base64 encoded signature |
||
| 665 | */ |
||
| 666 | protected function signMessage(IUser $user, $message) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @NoAdminRequired |
||
| 676 | * @NoSubadminRequired |
||
| 677 | * @PasswordConfirmationRequired |
||
| 678 | * |
||
| 679 | * @param string $avatarScope |
||
| 680 | * @param string $displayname |
||
| 681 | * @param string $displaynameScope |
||
| 682 | * @param string $phone |
||
| 683 | * @param string $phoneScope |
||
| 684 | * @param string $email |
||
| 685 | * @param string $emailScope |
||
| 686 | * @param string $website |
||
| 687 | * @param string $websiteScope |
||
| 688 | * @param string $address |
||
| 689 | * @param string $addressScope |
||
| 690 | * @param string $twitter |
||
| 691 | * @param string $twitterScope |
||
| 692 | * @return DataResponse |
||
| 693 | */ |
||
| 694 | public function setUserSettings($avatarScope, |
||
| 764 | |||
| 765 | |||
| 766 | /** |
||
| 767 | * update account manager with new user data |
||
| 768 | * |
||
| 769 | * @param IUser $user |
||
| 770 | * @param array $data |
||
| 771 | * @throws ForbiddenException |
||
| 772 | */ |
||
| 773 | protected function saveUserSettings(IUser $user, $data) { |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Count all unique users visible for the current admin/subadmin. |
||
| 806 | * |
||
| 807 | * @NoAdminRequired |
||
| 808 | * |
||
| 809 | * @return DataResponse |
||
| 810 | */ |
||
| 811 | public function stats() { |
||
| 840 | |||
| 841 | |||
| 842 | /** |
||
| 843 | * Set the displayName of a user |
||
| 844 | * |
||
| 845 | * @NoAdminRequired |
||
| 846 | * @NoSubadminRequired |
||
| 847 | * @PasswordConfirmationRequired |
||
| 848 | * @todo merge into saveUserSettings |
||
| 849 | * |
||
| 850 | * @param string $username |
||
| 851 | * @param string $displayName |
||
| 852 | * @return DataResponse |
||
| 853 | */ |
||
| 854 | public function setDisplayName($username, $displayName) { |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Set the mail address of a user |
||
| 902 | * |
||
| 903 | * @NoAdminRequired |
||
| 904 | * @NoSubadminRequired |
||
| 905 | * @PasswordConfirmationRequired |
||
| 906 | * |
||
| 907 | * @param string $id |
||
| 908 | * @param string $mailAddress |
||
| 909 | * @return DataResponse |
||
| 910 | */ |
||
| 911 | public function setEMailAddress($id, $mailAddress) { |
||
| 989 | |||
| 990 | } |
||
| 991 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.