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 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. 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 Group, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Group implements IGroup { |
||
| 33 | /** |
||
| 34 | * @var string $id |
||
| 35 | */ |
||
| 36 | private $gid; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \OC\User\User[] $users |
||
| 40 | */ |
||
| 41 | private $users = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool $usersLoaded |
||
| 45 | */ |
||
| 46 | private $usersLoaded; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \OC_Group_Backend[]|\OC_Group_Database[] $backend |
||
| 50 | */ |
||
| 51 | private $backends; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \OC\Hooks\PublicEmitter $emitter |
||
| 55 | */ |
||
| 56 | private $emitter; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \OC\User\Manager $userManager |
||
| 60 | */ |
||
| 61 | private $userManager; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $gid |
||
| 65 | * @param \OC_Group_Backend[] $backends |
||
| 66 | * @param \OC\User\Manager $userManager |
||
| 67 | * @param \OC\Hooks\PublicEmitter $emitter |
||
| 68 | */ |
||
| 69 | 470 | public function __construct($gid, $backends, $userManager, $emitter = null) { |
|
| 70 | 470 | $this->gid = $gid; |
|
| 71 | 470 | $this->backends = $backends; |
|
| 72 | 470 | $this->userManager = $userManager; |
|
| 73 | 470 | $this->emitter = $emitter; |
|
| 74 | 470 | } |
|
| 75 | |||
| 76 | 477 | public function getGID() { |
|
| 79 | |||
| 80 | /** |
||
| 81 | * get all users in the group |
||
| 82 | * |
||
| 83 | * @return \OC\User\User[] |
||
| 84 | */ |
||
| 85 | 4 | public function getUsers() { |
|
| 86 | 4 | if ($this->usersLoaded) { |
|
| 87 | return $this->users; |
||
| 88 | } |
||
| 89 | |||
| 90 | 4 | $userIds = array(); |
|
| 91 | 4 | foreach ($this->backends as $backend) { |
|
| 92 | 4 | $diff = array_diff( |
|
| 93 | 4 | $backend->usersInGroup($this->gid), |
|
| 94 | $userIds |
||
| 95 | 4 | ); |
|
| 96 | 4 | if ($diff) { |
|
|
|
|||
| 97 | 4 | $userIds = array_merge($userIds, $diff); |
|
| 98 | 4 | } |
|
| 99 | 4 | } |
|
| 100 | |||
| 101 | 4 | $this->users = $this->getVerifiedUsers($userIds); |
|
| 102 | 4 | $this->usersLoaded = true; |
|
| 103 | 4 | return $this->users; |
|
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * check if a user is in the group |
||
| 108 | * |
||
| 109 | * @param \OC\User\User $user |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | 435 | public function inGroup($user) { |
|
| 124 | |||
| 125 | /** |
||
| 126 | * add a user to the group |
||
| 127 | * |
||
| 128 | * @param \OC\User\User $user |
||
| 129 | */ |
||
| 130 | 420 | public function addUser($user) { |
|
| 131 | 420 | if ($this->inGroup($user)) { |
|
| 132 | 3 | return; |
|
| 133 | } |
||
| 134 | |||
| 135 | 419 | if ($this->emitter) { |
|
| 136 | 418 | $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
|
| 137 | 418 | } |
|
| 138 | 419 | foreach ($this->backends as $backend) { |
|
| 139 | 419 | if ($backend->implementsActions(\OC_Group_Backend::ADD_TO_GROUP)) { |
|
| 140 | 419 | $backend->addToGroup($user->getUID(), $this->gid); |
|
| 141 | 419 | if ($this->users) { |
|
| 142 | $this->users[$user->getUID()] = $user; |
||
| 143 | } |
||
| 144 | 419 | if ($this->emitter) { |
|
| 145 | 418 | $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
|
| 146 | 418 | } |
|
| 147 | 419 | return; |
|
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * remove a user from the group |
||
| 154 | * |
||
| 155 | * @param \OC\User\User $user |
||
| 156 | */ |
||
| 157 | 410 | public function removeUser($user) { |
|
| 158 | 410 | $result = false; |
|
| 159 | 410 | if ($this->emitter) { |
|
| 160 | 407 | $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
|
| 161 | 407 | } |
|
| 162 | 410 | foreach ($this->backends as $backend) { |
|
| 163 | 410 | if ($backend->implementsActions(\OC_Group_Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
|
| 164 | 409 | $backend->removeFromGroup($user->getUID(), $this->gid); |
|
| 165 | 409 | $result = true; |
|
| 166 | 409 | } |
|
| 167 | 410 | } |
|
| 168 | 410 | if ($result) { |
|
| 169 | 409 | if ($this->emitter) { |
|
| 170 | 407 | $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
|
| 171 | 407 | } |
|
| 172 | 409 | if ($this->users) { |
|
| 173 | 7 | foreach ($this->users as $index => $groupUser) { |
|
| 174 | 7 | if ($groupUser->getUID() === $user->getUID()) { |
|
| 175 | 7 | unset($this->users[$index]); |
|
| 176 | 7 | return; |
|
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | 403 | } |
|
| 181 | 404 | } |
|
| 182 | |||
| 183 | /** |
||
| 184 | * search for users in the group by userid |
||
| 185 | * |
||
| 186 | * @param string $search |
||
| 187 | * @param int $limit |
||
| 188 | * @param int $offset |
||
| 189 | * @return \OC\User\User[] |
||
| 190 | */ |
||
| 191 | 36 | View Code Duplication | public function searchUsers($search, $limit = null, $offset = null) { |
| 202 | |||
| 203 | /** |
||
| 204 | * returns the number of users matching the search string |
||
| 205 | * |
||
| 206 | * @param string $search |
||
| 207 | * @return int|bool |
||
| 208 | */ |
||
| 209 | 3 | public function count($search = '') { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * search for users in the group by displayname |
||
| 226 | * |
||
| 227 | * @param string $search |
||
| 228 | * @param int $limit |
||
| 229 | * @param int $offset |
||
| 230 | * @return \OC\User\User[] |
||
| 231 | */ |
||
| 232 | View Code Duplication | public function searchDisplayName($search, $limit = null, $offset = null) { |
|
| 243 | |||
| 244 | /** |
||
| 245 | * delete the group |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | 458 | public function delete() { |
|
| 270 | |||
| 271 | /** |
||
| 272 | * returns all the Users from an array that really exists |
||
| 273 | * @param string[] $userIds an array containing user IDs |
||
| 274 | * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
||
| 275 | */ |
||
| 276 | 40 | private function getVerifiedUsers($userIds) { |
|
| 289 | } |
||
| 290 |
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.