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 | /** |
||
| 39 | * @var string $id |
||
| 40 | */ |
||
| 41 | private $gid; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \OC\User\User[] $users |
||
| 45 | */ |
||
| 46 | private $users = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var bool $usersLoaded |
||
| 50 | */ |
||
| 51 | private $usersLoaded; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend |
||
| 55 | */ |
||
| 56 | private $backends; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \OC\Hooks\PublicEmitter $emitter |
||
| 60 | */ |
||
| 61 | private $emitter; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \OC\User\Manager $userManager |
||
| 65 | */ |
||
| 66 | private $userManager; |
||
| 67 | |||
| 68 | /** @var EventDispatcherInterface */ |
||
| 69 | private $eventDispatcher; |
||
| 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, EventDispatcherInterface $eventDispatcher, $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($user) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * add a user to the group |
||
| 145 | * |
||
| 146 | * @param \OC\User\User $user |
||
| 147 | */ |
||
| 148 | public function addUser($user) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * remove a user from the group |
||
| 174 | * |
||
| 175 | * @param \OC\User\User $user |
||
| 176 | */ |
||
| 177 | public function removeUser($user) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * search for users in the group by userid |
||
| 207 | * |
||
| 208 | * @param string $search |
||
| 209 | * @param int $limit |
||
| 210 | * @param int $offset |
||
| 211 | * @return \OC\User\User[] |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function searchUsers($search, $limit = null, $offset = null) { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * returns the number of users matching the search string |
||
| 227 | * |
||
| 228 | * @param string $search |
||
| 229 | * @return int|bool |
||
| 230 | */ |
||
| 231 | public function count($search = '') { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * search for users in the group by displayname |
||
| 248 | * |
||
| 249 | * @param string $search |
||
| 250 | * @param int $limit |
||
| 251 | * @param int $offset |
||
| 252 | * @return \OC\User\User[] |
||
| 253 | */ |
||
| 254 | View Code Duplication | public function searchDisplayName($search, $limit = null, $offset = null) { |
|
| 265 | |||
| 266 | /** |
||
| 267 | * delete the group |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | public function delete() { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * returns all the Users from an array that really exists |
||
| 297 | * @param string[] $userIds an array containing user IDs |
||
| 298 | * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
||
| 299 | */ |
||
| 300 | private function getVerifiedUsers($userIds) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Returns the backend for this group |
||
| 316 | * |
||
| 317 | * @return \OC\Group\Backend |
||
| 318 | * @since 10.0.0 |
||
| 319 | */ |
||
| 320 | public function getBackend() { |
||
| 325 | } |
||
| 326 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: