| Total Complexity | 75 |
| Total Lines | 375 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Group 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.
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 Group, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class Group implements IGroup { |
||
| 49 | /** @var null|string */ |
||
| 50 | protected $displayName; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | private $gid; |
||
| 54 | |||
| 55 | /** @var \OC\User\User[] */ |
||
| 56 | private $users = []; |
||
| 57 | |||
| 58 | /** @var bool */ |
||
| 59 | private $usersLoaded; |
||
| 60 | |||
| 61 | /** @var Backend[] */ |
||
| 62 | private $backends; |
||
| 63 | /** @var EventDispatcherInterface */ |
||
| 64 | private $dispatcher; |
||
| 65 | /** @var \OC\User\Manager|IUserManager */ |
||
| 66 | private $userManager; |
||
| 67 | /** @var PublicEmitter */ |
||
| 68 | private $emitter; |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $gid |
||
| 73 | * @param Backend[] $backends |
||
| 74 | * @param EventDispatcherInterface $dispatcher |
||
| 75 | * @param IUserManager $userManager |
||
| 76 | * @param PublicEmitter $emitter |
||
| 77 | * @param string $displayName |
||
| 78 | */ |
||
| 79 | public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) { |
||
| 80 | $this->gid = $gid; |
||
| 81 | $this->backends = $backends; |
||
| 82 | $this->dispatcher = $dispatcher; |
||
| 83 | $this->userManager = $userManager; |
||
| 84 | $this->emitter = $emitter; |
||
| 85 | $this->displayName = $displayName; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function getGID() { |
||
| 90 | } |
||
| 91 | |||
| 92 | public function getDisplayName() { |
||
| 93 | if (is_null($this->displayName)) { |
||
| 94 | foreach ($this->backends as $backend) { |
||
| 95 | if ($backend instanceof IGetDisplayNameBackend) { |
||
| 96 | $displayName = $backend->getDisplayName($this->gid); |
||
| 97 | if (trim($displayName) !== '') { |
||
| 98 | $this->displayName = $displayName; |
||
| 99 | return $this->displayName; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | return $this->gid; |
||
| 104 | } |
||
| 105 | return $this->displayName; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function setDisplayName(string $displayName): bool { |
||
| 109 | $displayName = trim($displayName); |
||
| 110 | if ($displayName !== '') { |
||
| 111 | foreach ($this->backends as $backend) { |
||
| 112 | if (($backend instanceof ISetDisplayNameBackend) |
||
| 113 | && $backend->setDisplayName($this->gid, $displayName)) { |
||
| 114 | $this->displayName = $displayName; |
||
| 115 | return true; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * get all users in the group |
||
| 124 | * |
||
| 125 | * @return \OC\User\User[] |
||
| 126 | */ |
||
| 127 | public function getUsers() { |
||
| 128 | if ($this->usersLoaded) { |
||
| 129 | return $this->users; |
||
| 130 | } |
||
| 131 | |||
| 132 | $userIds = []; |
||
| 133 | foreach ($this->backends as $backend) { |
||
| 134 | $diff = array_diff( |
||
| 135 | $backend->usersInGroup($this->gid), |
||
| 136 | $userIds |
||
| 137 | ); |
||
| 138 | if ($diff) { |
||
|
|
|||
| 139 | $userIds = array_merge($userIds, $diff); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $this->users = $this->getVerifiedUsers($userIds); |
||
| 144 | $this->usersLoaded = true; |
||
| 145 | return $this->users; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * check if a user is in the group |
||
| 150 | * |
||
| 151 | * @param IUser $user |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function inGroup(IUser $user) { |
||
| 155 | if (isset($this->users[$user->getUID()])) { |
||
| 156 | return true; |
||
| 157 | } |
||
| 158 | foreach ($this->backends as $backend) { |
||
| 159 | if ($backend->inGroup($user->getUID(), $this->gid)) { |
||
| 160 | $this->users[$user->getUID()] = $user; |
||
| 161 | return true; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * add a user to the group |
||
| 169 | * |
||
| 170 | * @param IUser $user |
||
| 171 | */ |
||
| 172 | public function addUser(IUser $user) { |
||
| 173 | if ($this->inGroup($user)) { |
||
| 174 | return; |
||
| 175 | } |
||
| 176 | |||
| 177 | $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [ |
||
| 178 | 'user' => $user, |
||
| 179 | ])); |
||
| 180 | |||
| 181 | if ($this->emitter) { |
||
| 182 | $this->emitter->emit('\OC\Group', 'preAddUser', [$this, $user]); |
||
| 183 | } |
||
| 184 | foreach ($this->backends as $backend) { |
||
| 185 | if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { |
||
| 186 | $backend->addToGroup($user->getUID(), $this->gid); |
||
| 187 | if ($this->users) { |
||
| 188 | $this->users[$user->getUID()] = $user; |
||
| 189 | } |
||
| 190 | |||
| 191 | $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [ |
||
| 192 | 'user' => $user, |
||
| 193 | ])); |
||
| 194 | |||
| 195 | if ($this->emitter) { |
||
| 196 | $this->emitter->emit('\OC\Group', 'postAddUser', [$this, $user]); |
||
| 197 | } |
||
| 198 | return; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * remove a user from the group |
||
| 205 | * |
||
| 206 | * @param \OC\User\User $user |
||
| 207 | */ |
||
| 208 | public function removeUser($user) { |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * search for users in the group by userid |
||
| 242 | * |
||
| 243 | * @param string $search |
||
| 244 | * @param int $limit |
||
| 245 | * @param int $offset |
||
| 246 | * @return \OC\User\User[] |
||
| 247 | */ |
||
| 248 | public function searchUsers($search, $limit = null, $offset = null) { |
||
| 249 | $users = []; |
||
| 250 | foreach ($this->backends as $backend) { |
||
| 251 | $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
||
| 252 | $users += $this->getVerifiedUsers($userIds); |
||
| 253 | if (!is_null($limit) and $limit <= 0) { |
||
| 254 | return $users; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | return $users; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * returns the number of users matching the search string |
||
| 262 | * |
||
| 263 | * @param string $search |
||
| 264 | * @return int|bool |
||
| 265 | */ |
||
| 266 | public function count($search = '') { |
||
| 267 | $users = false; |
||
| 268 | foreach ($this->backends as $backend) { |
||
| 269 | if ($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
||
| 270 | if ($users === false) { |
||
| 271 | //we could directly add to a bool variable, but this would |
||
| 272 | //be ugly |
||
| 273 | $users = 0; |
||
| 274 | } |
||
| 275 | $users += $backend->countUsersInGroup($this->gid, $search); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | return $users; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * returns the number of disabled users |
||
| 283 | * |
||
| 284 | * @return int|bool |
||
| 285 | */ |
||
| 286 | public function countDisabled() { |
||
| 287 | $users = false; |
||
| 288 | foreach ($this->backends as $backend) { |
||
| 289 | if ($backend instanceof ICountDisabledInGroup) { |
||
| 290 | if ($users === false) { |
||
| 291 | //we could directly add to a bool variable, but this would |
||
| 292 | //be ugly |
||
| 293 | $users = 0; |
||
| 294 | } |
||
| 295 | $users += $backend->countDisabledInGroup($this->gid); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | return $users; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * search for users in the group by displayname |
||
| 303 | * |
||
| 304 | * @param string $search |
||
| 305 | * @param int $limit |
||
| 306 | * @param int $offset |
||
| 307 | * @return \OC\User\User[] |
||
| 308 | */ |
||
| 309 | public function searchDisplayName($search, $limit = null, $offset = null) { |
||
| 310 | $users = []; |
||
| 311 | foreach ($this->backends as $backend) { |
||
| 312 | $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
||
| 313 | $users = $this->getVerifiedUsers($userIds); |
||
| 314 | if (!is_null($limit) and $limit <= 0) { |
||
| 315 | return array_values($users); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | return array_values($users); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get the names of the backend classes the group is connected to |
||
| 323 | * |
||
| 324 | * @return string[] |
||
| 325 | */ |
||
| 326 | public function getBackendNames() { |
||
| 327 | $backends = []; |
||
| 328 | foreach ($this->backends as $backend) { |
||
| 329 | if ($backend instanceof INamedBackend) { |
||
| 330 | $backends[] = $backend->getBackendName(); |
||
| 331 | } else { |
||
| 332 | $backends[] = get_class($backend); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | return $backends; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * delete the group |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function delete() { |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * returns all the Users from an array that really exists |
||
| 372 | * @param string[] $userIds an array containing user IDs |
||
| 373 | * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
||
| 374 | */ |
||
| 375 | private function getVerifiedUsers($userIds) { |
||
| 376 | if (!is_array($userIds)) { |
||
| 377 | return []; |
||
| 378 | } |
||
| 379 | $users = []; |
||
| 380 | foreach ($userIds as $userId) { |
||
| 381 | $user = $this->userManager->get($userId); |
||
| 382 | if (!is_null($user)) { |
||
| 383 | $users[$userId] = $user; |
||
| 384 | } |
||
| 385 | } |
||
| 386 | return $users; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @return bool |
||
| 391 | * @since 14.0.0 |
||
| 392 | */ |
||
| 393 | public function canRemoveUser() { |
||
| 394 | foreach ($this->backends as $backend) { |
||
| 395 | if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) { |
||
| 396 | return true; |
||
| 397 | } |
||
| 398 | } |
||
| 399 | return false; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return bool |
||
| 404 | * @since 14.0.0 |
||
| 405 | */ |
||
| 406 | public function canAddUser() { |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return bool |
||
| 417 | * @since 16.0.0 |
||
| 418 | */ |
||
| 419 | public function hideFromCollaboration(): bool { |
||
| 423 | } |
||
| 424 | } |
||
| 425 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.