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