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 |
||
| 38 | class Group implements IGroup { |
||
| 39 | /** @var null|string */ |
||
| 40 | protected $displayName; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string $id |
||
| 44 | */ |
||
| 45 | private $gid; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \OC\User\User[] $users |
||
| 49 | */ |
||
| 50 | private $users = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var bool $usersLoaded |
||
| 54 | */ |
||
| 55 | private $usersLoaded; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend |
||
| 59 | */ |
||
| 60 | private $backends; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \OC\Hooks\PublicEmitter $emitter |
||
| 64 | */ |
||
| 65 | private $emitter; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \OC\User\Manager $userManager |
||
| 69 | */ |
||
| 70 | private $userManager; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $gid |
||
| 74 | * @param \OC\Group\Backend[] $backends |
||
| 75 | * @param \OC\User\Manager $userManager |
||
| 76 | * @param \OC\Hooks\PublicEmitter $emitter |
||
| 77 | * @param string $displayName |
||
| 78 | */ |
||
| 79 | public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) { |
||
| 86 | |||
| 87 | public function getGID() { |
||
| 90 | |||
| 91 | public function getDisplayName() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * get all users in the group |
||
| 100 | * |
||
| 101 | * @return \OC\User\User[] |
||
| 102 | */ |
||
| 103 | public function getUsers() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * check if a user is in the group |
||
| 126 | * |
||
| 127 | * @param IUser $user |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function inGroup(IUser $user) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * add a user to the group |
||
| 145 | * |
||
| 146 | * @param IUser $user |
||
| 147 | */ |
||
| 148 | public function addUser(IUser $user) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * remove a user from the group |
||
| 172 | * |
||
| 173 | * @param \OC\User\User $user |
||
| 174 | */ |
||
| 175 | public function removeUser($user) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * search for users in the group by userid |
||
| 203 | * |
||
| 204 | * @param string $search |
||
| 205 | * @param int $limit |
||
| 206 | * @param int $offset |
||
| 207 | * @return \OC\User\User[] |
||
| 208 | */ |
||
| 209 | public function searchUsers($search, $limit = null, $offset = null) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * returns the number of users matching the search string |
||
| 223 | * |
||
| 224 | * @param string $search |
||
| 225 | * @return int|bool |
||
| 226 | */ |
||
| 227 | public function count($search = '') { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * returns the number of disabled users |
||
| 244 | * |
||
| 245 | * @return int|bool |
||
| 246 | */ |
||
| 247 | View Code Duplication | public function countDisabled() { |
|
| 261 | |||
| 262 | /** |
||
| 263 | * search for users in the group by displayname |
||
| 264 | * |
||
| 265 | * @param string $search |
||
| 266 | * @param int $limit |
||
| 267 | * @param int $offset |
||
| 268 | * @return \OC\User\User[] |
||
| 269 | */ |
||
| 270 | public function searchDisplayName($search, $limit = null, $offset = null) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * delete the group |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public function delete() { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * returns all the Users from an array that really exists |
||
| 311 | * @param string[] $userIds an array containing user IDs |
||
| 312 | * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
||
| 313 | */ |
||
| 314 | private function getVerifiedUsers($userIds) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return bool |
||
| 330 | * @since 14.0.0 |
||
| 331 | */ |
||
| 332 | public function canRemoveUser() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return bool |
||
| 343 | * @since 14.0.0 |
||
| 344 | */ |
||
| 345 | public function canAddUser() { |
||
| 353 | } |
||
| 354 |
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.