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 | protected function clearCaches() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $gid |
||
| 155 | * @return \OC\Group\Group |
||
| 156 | */ |
||
| 157 | public function get($gid) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $gid |
||
| 166 | * @param string $displayName |
||
| 167 | * @return \OCP\IGroup |
||
| 168 | */ |
||
| 169 | protected function getGroupObject($gid, $displayName = null) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $gid |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | public function groupExists($gid) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $gid |
||
| 202 | * @return \OC\Group\Group |
||
| 203 | */ |
||
| 204 | public function createGroup($gid) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $search |
||
| 225 | * @param int $limit |
||
| 226 | * @param int $offset |
||
| 227 | * @return \OC\Group\Group[] |
||
| 228 | */ |
||
| 229 | public function search($search, $limit = null, $offset = null) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param IUser|null $user |
||
| 250 | * @return \OC\Group\Group[] |
||
| 251 | */ |
||
| 252 | public function getUserGroups(IUser $user= null) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $uid the user id |
||
| 261 | * @return \OC\Group\Group[] |
||
| 262 | */ |
||
| 263 | public function getUserIdGroups($uid) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Checks if a userId is in the admin group |
||
| 287 | * @param string $userId |
||
| 288 | * @return bool if admin |
||
| 289 | */ |
||
| 290 | public function isAdmin($userId) { |
||
| 291 | foreach ($this->backends as $backend) { |
||
| 292 | if ($backend->implementsActions(\OC\Group\Backend::IS_ADMIN) && $backend->isAdmin($userId)) { |
||
| 293 | return true; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | return $this->isInGroup($userId, 'admin'); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Checks if a userId is in a group |
||
| 301 | * @param string $userId |
||
| 302 | * @param string $group |
||
| 303 | * @return bool if in group |
||
| 304 | */ |
||
| 305 | public function isInGroup($userId, $group) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * get a list of group ids for a user |
||
| 311 | * @param IUser $user |
||
| 312 | * @return array with group ids |
||
| 313 | */ |
||
| 314 | public function getUserGroupIds(IUser $user) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * get a list of all display names in a group |
||
| 322 | * @param string $gid |
||
| 323 | * @param string $search |
||
| 324 | * @param int $limit |
||
| 325 | * @param int $offset |
||
| 326 | * @return array an array of display names (value) and user ids (key) |
||
| 327 | */ |
||
| 328 | public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @return \OC\SubAdmin |
||
| 373 | */ |
||
| 374 | public function getSubAdmin() { |
||
| 385 | } |
||
| 386 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.