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 Principal 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 Principal, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class Principal implements BackendInterface { |
||
| 45 | |||
| 46 | /** @var IUserManager */ |
||
| 47 | private $userManager; |
||
| 48 | |||
| 49 | /** @var IGroupManager */ |
||
| 50 | private $groupManager; |
||
| 51 | |||
| 52 | /** @var IShareManager */ |
||
| 53 | private $shareManager; |
||
| 54 | |||
| 55 | /** @var IUserSession */ |
||
| 56 | private $userSession; |
||
| 57 | |||
| 58 | /** @var string */ |
||
| 59 | private $principalPrefix; |
||
| 60 | |||
| 61 | /** @var bool */ |
||
| 62 | private $hasGroups; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param IUserManager $userManager |
||
| 66 | * @param IGroupManager $groupManager |
||
| 67 | * @param IShareManager $shareManager |
||
| 68 | * @param IUserSession $userSession |
||
| 69 | * @param string $principalPrefix |
||
| 70 | */ |
||
| 71 | public function __construct(IUserManager $userManager, |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Returns a list of principals based on a prefix. |
||
| 86 | * |
||
| 87 | * This prefix will often contain something like 'principals'. You are only |
||
| 88 | * expected to return principals that are in this base path. |
||
| 89 | * |
||
| 90 | * You are expected to return at least a 'uri' for every user, you can |
||
| 91 | * return any additional properties if you wish so. Common properties are: |
||
| 92 | * {DAV:}displayname |
||
| 93 | * |
||
| 94 | * @param string $prefixPath |
||
| 95 | * @return string[] |
||
| 96 | */ |
||
| 97 | public function getPrincipalsByPrefix($prefixPath) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns a specific principal, specified by it's path. |
||
| 111 | * The returned structure should be the exact same as from |
||
| 112 | * getPrincipalsByPrefix. |
||
| 113 | * |
||
| 114 | * @param string $path |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function getPrincipalByPath($path) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the list of members for a group-principal |
||
| 132 | * |
||
| 133 | * @param string $principal |
||
| 134 | * @return string[] |
||
| 135 | * @throws Exception |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function getGroupMemberSet($principal) { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the list of groups a principal is a member of |
||
| 149 | * |
||
| 150 | * @param string $principal |
||
| 151 | * @param bool $needGroups |
||
| 152 | * @return array |
||
| 153 | * @throws Exception |
||
| 154 | */ |
||
| 155 | public function getGroupMembership($principal, $needGroups = false) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Updates the list of group members for a group principal. |
||
| 179 | * |
||
| 180 | * The principals should be passed as a list of uri's. |
||
| 181 | * |
||
| 182 | * @param string $principal |
||
| 183 | * @param string[] $members |
||
| 184 | * @throws Exception |
||
| 185 | */ |
||
| 186 | public function setGroupMemberSet($principal, array $members) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $path |
||
| 192 | * @param PropPatch $propPatch |
||
| 193 | * @return int |
||
| 194 | */ |
||
| 195 | function updatePrincipal($path, PropPatch $propPatch) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Search user principals |
||
| 201 | * |
||
| 202 | * @param array $searchProperties |
||
| 203 | * @param string $test |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
||
| 207 | $results = []; |
||
| 208 | |||
| 209 | // If sharing is disabled, return the empty array |
||
| 210 | if (!$this->shareManager->shareApiEnabled()) { |
||
| 211 | return []; |
||
| 212 | } |
||
| 213 | |||
| 214 | // If sharing is restricted to group members only, |
||
| 215 | // return only members that have groups in common |
||
| 216 | $restrictGroups = false; |
||
| 217 | View Code Duplication | if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
| 218 | $user = $this->userSession->getUser(); |
||
| 219 | if (!$user) { |
||
| 220 | return []; |
||
| 221 | } |
||
| 222 | |||
| 223 | $restrictGroups = $this->groupManager->getUserGroupIds($user); |
||
| 224 | } |
||
| 225 | |||
| 226 | foreach ($searchProperties as $prop => $value) { |
||
| 227 | switch ($prop) { |
||
| 228 | case '{http://sabredav.org/ns}email-address': |
||
| 229 | $users = $this->userManager->getByEmail($value); |
||
| 230 | |||
| 231 | $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
||
| 232 | // is sharing restricted to groups only? |
||
| 233 | View Code Duplication | if ($restrictGroups !== false) { |
|
| 234 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
| 235 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
| 236 | return $carry; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
||
| 241 | return $carry; |
||
| 242 | }, []); |
||
| 243 | break; |
||
| 244 | |||
| 245 | default: |
||
| 246 | $results[] = []; |
||
| 247 | break; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | // results is an array of arrays, so this is not the first search result |
||
| 252 | // but the results of the first searchProperty |
||
| 253 | if (count($results) === 1) { |
||
| 254 | return $results[0]; |
||
| 255 | } |
||
| 256 | |||
| 257 | switch ($test) { |
||
| 258 | case 'anyof': |
||
| 259 | return array_unique(array_merge(...$results)); |
||
| 260 | |||
| 261 | case 'allof': |
||
| 262 | default: |
||
| 263 | return array_intersect(...$results); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param string $prefixPath |
||
| 269 | * @param array $searchProperties |
||
| 270 | * @param string $test |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $uri |
||
| 289 | * @param string $principalPrefix |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | function findByUri($uri, $principalPrefix) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param IUser $user |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | protected function userToPrincipal($user) { |
||
| 351 | |||
| 352 | public function getPrincipalPrefix() { |
||
| 355 | |||
| 356 | } |
||
| 357 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.