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 |
||
| 35 | class Group implements IGroup { |
||
| 36 | /** @var null|string */ |
||
| 37 | protected $displayName; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string $id |
||
| 41 | */ |
||
| 42 | private $gid; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var \OC\User\User[] $users |
||
| 46 | */ |
||
| 47 | private $users = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var bool $usersLoaded |
||
| 51 | */ |
||
| 52 | private $usersLoaded; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend |
||
| 56 | */ |
||
| 57 | private $backends; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var \OC\Hooks\PublicEmitter $emitter |
||
| 61 | */ |
||
| 62 | private $emitter; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var \OC\User\Manager $userManager |
||
| 66 | */ |
||
| 67 | private $userManager; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $gid |
||
| 71 | * @param \OC\Group\Backend[] $backends |
||
| 72 | * @param \OC\User\Manager $userManager |
||
| 73 | * @param \OC\Hooks\PublicEmitter $emitter |
||
| 74 | * @param string $displayName |
||
| 75 | */ |
||
| 76 | public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) { |
||
| 83 | |||
| 84 | public function getGID() { |
||
| 87 | |||
| 88 | public function getDisplayName() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * get all users in the group |
||
| 97 | * |
||
| 98 | * @return \OC\User\User[] |
||
| 99 | */ |
||
| 100 | public function getUsers() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * check if a user is in the group |
||
| 123 | * |
||
| 124 | * @param IUser $user |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public function inGroup(IUser $user) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * add a user to the group |
||
| 142 | * |
||
| 143 | * @param IUser $user |
||
| 144 | */ |
||
| 145 | public function addUser(IUser $user) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * remove a user from the group |
||
| 169 | * |
||
| 170 | * @param \OC\User\User $user |
||
| 171 | */ |
||
| 172 | public function removeUser($user) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * search for users in the group by userid |
||
| 200 | * |
||
| 201 | * @param string $search |
||
| 202 | * @param int $limit |
||
| 203 | * @param int $offset |
||
| 204 | * @return \OC\User\User[] |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function searchUsers($search, $limit = null, $offset = null) { |
|
| 217 | |||
| 218 | /** |
||
| 219 | * returns the number of users matching the search string |
||
| 220 | * |
||
| 221 | * @param string $search |
||
| 222 | * @return int|bool |
||
| 223 | */ |
||
| 224 | View Code Duplication | public function count($search = '') { |
|
| 238 | |||
| 239 | /** |
||
| 240 | * returns the number of disabled users |
||
| 241 | * |
||
| 242 | * @return int|bool |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function countDisabled() { |
|
| 258 | |||
| 259 | /** |
||
| 260 | * search for users in the group by displayname |
||
| 261 | * |
||
| 262 | * @param string $search |
||
| 263 | * @param int $limit |
||
| 264 | * @param int $offset |
||
| 265 | * @return \OC\User\User[] |
||
| 266 | */ |
||
| 267 | View Code Duplication | public function searchDisplayName($search, $limit = null, $offset = null) { |
|
| 278 | |||
| 279 | /** |
||
| 280 | * delete the group |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function delete() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * returns all the Users from an array that really exists |
||
| 308 | * @param string[] $userIds an array containing user IDs |
||
| 309 | * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
||
| 310 | */ |
||
| 311 | private function getVerifiedUsers($userIds) { |
||
| 324 | } |
||
| 325 |
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.