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 Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class Manager extends PublicEmitter implements IGroupManager { |
||
| 61 | /** |
||
| 62 | * @var GroupInterface[] $backends |
||
| 63 | */ |
||
| 64 | private $backends = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var \OC\User\Manager $userManager |
||
| 68 | */ |
||
| 69 | private $userManager; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \OC\Group\Group[] |
||
| 73 | */ |
||
| 74 | private $cachedGroups = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var \OC\Group\Group[] |
||
| 78 | */ |
||
| 79 | private $cachedUserGroups = array(); |
||
| 80 | |||
| 81 | /** @var \OC\SubAdmin */ |
||
| 82 | private $subAdmin = null; |
||
| 83 | |||
| 84 | /** @var ILogger */ |
||
| 85 | private $logger; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param \OC\User\Manager $userManager |
||
| 89 | * @param ILogger $logger |
||
| 90 | */ |
||
| 91 | public function __construct(\OC\User\Manager $userManager, ILogger $logger) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Checks whether a given backend is used |
||
| 119 | * |
||
| 120 | * @param string $backendClass Full classname including complete namespace |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function isBackendUsed($backendClass) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param \OCP\GroupInterface $backend |
||
| 137 | */ |
||
| 138 | public function addBackend($backend) { |
||
| 142 | |||
| 143 | public function clearBackends() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get the active backends |
||
| 150 | * @return \OCP\GroupInterface[] |
||
| 151 | */ |
||
| 152 | public function getBackends() { |
||
| 155 | |||
| 156 | |||
| 157 | protected function clearCaches() { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $gid |
||
| 164 | * @return \OC\Group\Group |
||
| 165 | */ |
||
| 166 | public function get($gid) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $gid |
||
| 175 | * @param string $displayName |
||
| 176 | * @return \OCP\IGroup |
||
| 177 | */ |
||
| 178 | protected function getGroupObject($gid, $displayName = null) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $gid |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | public function groupExists($gid) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $gid |
||
| 211 | * @return \OC\Group\Group |
||
| 212 | */ |
||
| 213 | public function createGroup($gid) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $search |
||
| 234 | * @param int $limit |
||
| 235 | * @param int $offset |
||
| 236 | * @return \OC\Group\Group[] |
||
| 237 | */ |
||
| 238 | public function search($search, $limit = null, $offset = null) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param IUser|null $user |
||
| 259 | * @return \OC\Group\Group[] |
||
| 260 | */ |
||
| 261 | public function getUserGroups(IUser $user= null) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $uid the user id |
||
| 270 | * @return \OC\Group\Group[] |
||
| 271 | */ |
||
| 272 | public function getUserIdGroups($uid) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Checks if a userId is in the admin group |
||
| 296 | * @param string $userId |
||
| 297 | * @return bool if admin |
||
| 298 | */ |
||
| 299 | public function isAdmin($userId) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Checks if a userId is in a group |
||
| 310 | * @param string $userId |
||
| 311 | * @param string $group |
||
| 312 | * @return bool if in group |
||
| 313 | */ |
||
| 314 | public function isInGroup($userId, $group) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * get a list of group ids for a user |
||
| 320 | * @param IUser $user |
||
| 321 | * @return array with group ids |
||
| 322 | */ |
||
| 323 | public function getUserGroupIds(IUser $user) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * get a list of all display names in a group |
||
| 331 | * @param string $gid |
||
| 332 | * @param string $search |
||
| 333 | * @param int $limit |
||
| 334 | * @param int $offset |
||
| 335 | * @return array an array of display names (value) and user ids (key) |
||
| 336 | */ |
||
| 337 | public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return \OC\SubAdmin |
||
| 382 | */ |
||
| 383 | public function getSubAdmin() { |
||
| 394 | } |
||
| 395 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.