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:
| 1 | <?php |
||
| 67 | class UsersController extends Controller { |
||
| 68 | /** @var IUserManager */ |
||
| 69 | private $userManager; |
||
| 70 | /** @var IGroupManager */ |
||
| 71 | private $groupManager; |
||
| 72 | /** @var IUserSession */ |
||
| 73 | private $userSession; |
||
| 74 | /** @var IConfig */ |
||
| 75 | private $config; |
||
| 76 | /** @var bool */ |
||
| 77 | private $isAdmin; |
||
| 78 | /** @var IL10N */ |
||
| 79 | private $l10n; |
||
| 80 | /** @var IMailer */ |
||
| 81 | private $mailer; |
||
| 82 | /** @var IFactory */ |
||
| 83 | private $l10nFactory; |
||
| 84 | /** @var IAppManager */ |
||
| 85 | private $appManager; |
||
| 86 | /** @var AccountManager */ |
||
| 87 | private $accountManager; |
||
| 88 | /** @var Manager */ |
||
| 89 | private $keyManager; |
||
| 90 | /** @var IJobList */ |
||
| 91 | private $jobList; |
||
| 92 | /** @var IManager */ |
||
| 93 | private $encryptionManager; |
||
| 94 | |||
| 95 | |||
| 96 | View Code Duplication | public function __construct(string $appName, |
|
| 97 | IRequest $request, |
||
| 98 | IUserManager $userManager, |
||
| 99 | IGroupManager $groupManager, |
||
| 100 | IUserSession $userSession, |
||
| 101 | IConfig $config, |
||
| 102 | bool $isAdmin, |
||
| 103 | IL10N $l10n, |
||
| 104 | IMailer $mailer, |
||
| 105 | IFactory $l10nFactory, |
||
| 106 | IAppManager $appManager, |
||
| 107 | AccountManager $accountManager, |
||
| 108 | Manager $keyManager, |
||
| 109 | IJobList $jobList, |
||
| 110 | IManager $encryptionManager) { |
||
| 111 | parent::__construct($appName, $request); |
||
| 112 | $this->userManager = $userManager; |
||
| 113 | $this->groupManager = $groupManager; |
||
| 114 | $this->userSession = $userSession; |
||
| 115 | $this->config = $config; |
||
| 116 | $this->isAdmin = $isAdmin; |
||
| 117 | $this->l10n = $l10n; |
||
| 118 | $this->mailer = $mailer; |
||
| 119 | $this->l10nFactory = $l10nFactory; |
||
| 120 | $this->appManager = $appManager; |
||
| 121 | $this->accountManager = $accountManager; |
||
| 122 | $this->keyManager = $keyManager; |
||
| 123 | $this->jobList = $jobList; |
||
| 124 | $this->encryptionManager = $encryptionManager; |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * @NoCSRFRequired |
||
| 130 | * @NoAdminRequired |
||
| 131 | * |
||
| 132 | * Display users list template |
||
| 133 | * |
||
| 134 | * @return TemplateResponse |
||
| 135 | */ |
||
| 136 | public function usersListByGroup() { |
||
| 137 | return $this->usersList(); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @NoCSRFRequired |
||
| 142 | * @NoAdminRequired |
||
| 143 | * |
||
| 144 | * Display users list template |
||
| 145 | * |
||
| 146 | * @return TemplateResponse |
||
| 147 | */ |
||
| 148 | public function usersList() { |
||
| 149 | $user = $this->userSession->getUser(); |
||
| 150 | $uid = $user->getUID(); |
||
| 151 | |||
| 152 | \OC::$server->getNavigationManager()->setActiveEntry('core_users'); |
||
| 153 | |||
| 154 | /* SORT OPTION: SORT_USERCOUNT or SORT_GROUPNAME */ |
||
| 155 | $sortGroupsBy = \OC\Group\MetaData::SORT_USERCOUNT; |
||
| 156 | if ($this->config->getSystemValue('sort_groups_by_name', false)) { |
||
| 157 | $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME; |
||
| 158 | } else { |
||
| 159 | $isLDAPUsed = false; |
||
| 160 | if ($this->appManager->isEnabledForUser('user_ldap')) { |
||
| 161 | $isLDAPUsed = |
||
| 162 | $this->groupManager->isBackendUsed('\OCA\User_LDAP\Group_LDAP') |
||
| 163 | || $this->groupManager->isBackendUsed('\OCA\User_LDAP\Group_Proxy'); |
||
| 164 | if ($isLDAPUsed) { |
||
| 165 | // LDAP user count can be slow, so we sort by group name here |
||
| 166 | $sortGroupsBy = \OC\Group\MetaData::SORT_GROUPNAME; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /* ENCRYPTION CONFIG */ |
||
| 172 | $isEncryptionEnabled = $this->encryptionManager->isEnabled(); |
||
| 173 | $useMasterKey = $this->config->getAppValue('encryption', 'useMasterKey', true); |
||
|
|
|||
| 174 | // If masterKey enabled, then you can change password. This is to avoid data loss! |
||
| 175 | $canChangePassword = ($isEncryptionEnabled && $useMasterKey) || $useMasterKey; |
||
| 176 | |||
| 177 | |||
| 178 | /* GROUPS */ |
||
| 179 | $groupsInfo = new \OC\Group\MetaData( |
||
| 180 | $uid, |
||
| 181 | $this->isAdmin, |
||
| 182 | $this->groupManager, |
||
| 183 | $this->userSession |
||
| 184 | ); |
||
| 185 | |||
| 186 | $groupsInfo->setSorting($sortGroupsBy); |
||
| 187 | list($adminGroup, $groups) = $groupsInfo->get(); |
||
| 188 | |||
| 189 | if ($this->isAdmin) { |
||
| 190 | $disabledUsers = $isLDAPUsed ? 0 : $this->userManager->countDisabledUsers(); |
||
| 191 | $userCount = array_reduce($this->userManager->countUsers(), function($v, $w) { |
||
| 192 | return $v + (int)$w; |
||
| 193 | }, 0); |
||
| 194 | } else { |
||
| 195 | // User is subadmin ! |
||
| 196 | // Map group list to names to retrieve the countDisabledUsersOfGroups |
||
| 197 | $userGroups = $this->groupManager->getUserGroups($user); |
||
| 198 | $groupsNames = []; |
||
| 199 | $userCount = 0; |
||
| 200 | |||
| 201 | foreach($groups as $key => $group) { |
||
| 202 | // $userCount += (int)$group['usercount']; |
||
| 203 | array_push($groupsNames, $group['name']); |
||
| 204 | // we prevent subadmins from looking up themselves |
||
| 205 | // so we lower the count of the groups he belongs to |
||
| 206 | if (array_key_exists($group['id'], $userGroups)) { |
||
| 207 | $groups[$key]['usercount']--; |
||
| 208 | $userCount = -1; // we also lower from one the total count |
||
| 209 | } |
||
| 210 | }; |
||
| 211 | $userCount += $this->userManager->countUsersOfGroups($groupsInfo->getGroups()); |
||
| 212 | $disabledUsers = $isLDAPUsed ? 0 : $this->userManager->countDisabledUsersOfGroups($groupsNames); |
||
| 213 | } |
||
| 214 | $disabledUsersGroup = [ |
||
| 215 | 'id' => 'disabled', |
||
| 216 | 'name' => 'Disabled users', |
||
| 217 | 'usercount' => $disabledUsers |
||
| 218 | ]; |
||
| 219 | |||
| 220 | /* QUOTAS PRESETS */ |
||
| 221 | $quotaPreset = $this->config->getAppValue('files', 'quota_preset', '1 GB, 5 GB, 10 GB'); |
||
| 222 | $quotaPreset = explode(',', $quotaPreset); |
||
| 223 | foreach ($quotaPreset as &$preset) { |
||
| 224 | $preset = trim($preset); |
||
| 225 | } |
||
| 226 | $quotaPreset = array_diff($quotaPreset, array('default', 'none')); |
||
| 227 | $defaultQuota = $this->config->getAppValue('files', 'default_quota', 'none'); |
||
| 228 | |||
| 229 | \OC::$server->getEventDispatcher()->dispatch('OC\Settings\Users::loadAdditionalScripts'); |
||
| 230 | |||
| 231 | /* LANGUAGES */ |
||
| 232 | $languages = $this->l10nFactory->getLanguages(); |
||
| 233 | |||
| 234 | /* FINAL DATA */ |
||
| 235 | $serverData = array(); |
||
| 236 | // groups |
||
| 237 | $serverData['groups'] = array_merge_recursive($adminGroup, [$disabledUsersGroup], $groups); |
||
| 238 | // Various data |
||
| 239 | $serverData['isAdmin'] = $this->isAdmin; |
||
| 240 | $serverData['sortGroups'] = $sortGroupsBy; |
||
| 241 | $serverData['quotaPreset'] = $quotaPreset; |
||
| 242 | $serverData['userCount'] = $userCount - $disabledUsers; |
||
| 243 | $serverData['languages'] = $languages; |
||
| 244 | $serverData['defaultLanguage'] = $this->config->getSystemValue('default_language', 'en'); |
||
| 245 | // Settings |
||
| 246 | $serverData['defaultQuota'] = $defaultQuota; |
||
| 247 | $serverData['canChangePassword'] = $canChangePassword; |
||
| 248 | |||
| 249 | return new TemplateResponse('settings', 'settings', ['serverData' => $serverData]); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @NoAdminRequired |
||
| 254 | * @NoSubadminRequired |
||
| 255 | * @PasswordConfirmationRequired |
||
| 256 | * |
||
| 257 | * @param string $avatarScope |
||
| 258 | * @param string $displayname |
||
| 259 | * @param string $displaynameScope |
||
| 260 | * @param string $phone |
||
| 261 | * @param string $phoneScope |
||
| 262 | * @param string $email |
||
| 263 | * @param string $emailScope |
||
| 264 | * @param string $website |
||
| 265 | * @param string $websiteScope |
||
| 266 | * @param string $address |
||
| 267 | * @param string $addressScope |
||
| 268 | * @param string $twitter |
||
| 269 | * @param string $twitterScope |
||
| 270 | * @return DataResponse |
||
| 271 | */ |
||
| 272 | public function setUserSettings($avatarScope, |
||
| 344 | /** |
||
| 345 | * update account manager with new user data |
||
| 346 | * |
||
| 347 | * @param IUser $user |
||
| 348 | * @param array $data |
||
| 349 | * @throws ForbiddenException |
||
| 350 | */ |
||
| 351 | protected function saveUserSettings(IUser $user, array $data) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Set the mail address of a user |
||
| 381 | * |
||
| 382 | * @NoAdminRequired |
||
| 383 | * @NoSubadminRequired |
||
| 384 | * @PasswordConfirmationRequired |
||
| 385 | * |
||
| 386 | * @param string $account |
||
| 387 | * @param bool $onlyVerificationCode only return verification code without updating the data |
||
| 388 | * @return DataResponse |
||
| 389 | */ |
||
| 390 | public function getVerificationCode(string $account, bool $onlyVerificationCode): DataResponse { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * get current timestamp |
||
| 446 | * |
||
| 447 | * @return int |
||
| 448 | */ |
||
| 449 | protected function getCurrentTime(): int { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * sign message with users private key |
||
| 455 | * |
||
| 456 | * @param IUser $user |
||
| 457 | * @param string $message |
||
| 458 | * |
||
| 459 | * @return string base64 encoded signature |
||
| 460 | */ |
||
| 461 | protected function signMessage(IUser $user, string $message): string { |
||
| 466 | } |
||
| 467 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: