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 |
||
| 63 | class Manager extends PublicEmitter implements IGroupManager { |
||
| 64 | /** |
||
| 65 | * @var GroupInterface[] $backends |
||
| 66 | */ |
||
| 67 | private $backends = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var \OC\User\Manager $userManager |
||
| 71 | */ |
||
| 72 | private $userManager; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var \OC\Group\Group[] |
||
| 76 | */ |
||
| 77 | private $cachedGroups = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \OC\Group\Group[] |
||
| 81 | */ |
||
| 82 | private $cachedUserGroups = array(); |
||
| 83 | |||
| 84 | /** @var \OC\SubAdmin */ |
||
| 85 | private $subAdmin = null; |
||
| 86 | |||
| 87 | /** @var ILogger */ |
||
| 88 | private $logger; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param \OC\User\Manager $userManager |
||
| 92 | * @param ILogger $logger |
||
| 93 | */ |
||
| 94 | public function __construct(\OC\User\Manager $userManager, ILogger $logger) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Checks whether a given backend is used |
||
| 122 | * |
||
| 123 | * @param string $backendClass Full classname including complete namespace |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | public function isBackendUsed($backendClass) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param \OCP\GroupInterface $backend |
||
| 140 | */ |
||
| 141 | public function addBackend($backend) { |
||
| 145 | |||
| 146 | public function clearBackends() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the active backends |
||
| 153 | * @return \OCP\GroupInterface[] |
||
| 154 | */ |
||
| 155 | public function getBackends() { |
||
| 158 | |||
| 159 | |||
| 160 | protected function clearCaches() { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $gid |
||
| 167 | * @return \OC\Group\Group |
||
| 168 | */ |
||
| 169 | public function get($gid) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $gid |
||
| 178 | * @param string $displayName |
||
| 179 | * @return \OCP\IGroup |
||
| 180 | */ |
||
| 181 | protected function getGroupObject($gid, $displayName = null) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $gid |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | public function groupExists($gid) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $gid |
||
| 214 | * @return \OC\Group\Group |
||
| 215 | */ |
||
| 216 | public function createGroup($gid) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $search |
||
| 237 | * @param int $limit |
||
| 238 | * @param int $offset |
||
| 239 | * @return \OC\Group\Group[] |
||
| 240 | */ |
||
| 241 | public function search($search, $limit = null, $offset = null) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param IUser|null $user |
||
| 262 | * @return \OC\Group\Group[] |
||
| 263 | */ |
||
| 264 | public function getUserGroups(IUser $user= null) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $uid the user id |
||
| 273 | * @return \OC\Group\Group[] |
||
| 274 | */ |
||
| 275 | public function getUserIdGroups($uid) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Checks if a userId is in the admin group |
||
| 299 | * @param string $userId |
||
| 300 | * @return bool if admin |
||
| 301 | */ |
||
| 302 | public function isAdmin($userId) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Checks if a userId is in a group |
||
| 313 | * @param string $userId |
||
| 314 | * @param string $group |
||
| 315 | * @return bool if in group |
||
| 316 | */ |
||
| 317 | public function isInGroup($userId, $group) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * get a list of group ids for a user |
||
| 323 | * @param IUser $user |
||
| 324 | * @return array with group ids |
||
| 325 | */ |
||
| 326 | public function getUserGroupIds(IUser $user) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * get an array of groupid and displayName for a user |
||
| 334 | * @param IUser $user |
||
| 335 | * @return array ['displayName' => displayname] |
||
| 336 | */ |
||
| 337 | public function getUserGroupNames(IUser $user) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * get a list of all display names in a group |
||
| 345 | * @param string $gid |
||
| 346 | * @param string $search |
||
| 347 | * @param int $limit |
||
| 348 | * @param int $offset |
||
| 349 | * @return array an array of display names (value) and user ids (key) |
||
| 350 | */ |
||
| 351 | public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return \OC\SubAdmin |
||
| 396 | */ |
||
| 397 | public function getSubAdmin() { |
||
| 408 | } |
||
| 409 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.