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 |
||
| 45 | class Principal implements BackendInterface { |
||
| 46 | |||
| 47 | /** @var IUserManager */ |
||
| 48 | private $userManager; |
||
| 49 | |||
| 50 | /** @var IGroupManager */ |
||
| 51 | private $groupManager; |
||
| 52 | |||
| 53 | /** @var IShareManager */ |
||
| 54 | private $shareManager; |
||
| 55 | |||
| 56 | /** @var IUserSession */ |
||
| 57 | private $userSession; |
||
| 58 | |||
| 59 | /** @var IConfig */ |
||
| 60 | private $config; |
||
| 61 | |||
| 62 | /** @var string */ |
||
| 63 | private $principalPrefix; |
||
| 64 | |||
| 65 | /** @var bool */ |
||
| 66 | private $hasGroups; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param IUserManager $userManager |
||
| 70 | * @param IGroupManager $groupManager |
||
| 71 | * @param IShareManager $shareManager |
||
| 72 | * @param IUserSession $userSession |
||
| 73 | * @param IConfig $config |
||
| 74 | * @param string $principalPrefix |
||
| 75 | */ |
||
| 76 | public function __construct(IUserManager $userManager, |
||
| 77 | IGroupManager $groupManager, |
||
| 78 | IShareManager $shareManager, |
||
| 79 | IUserSession $userSession, |
||
| 80 | IConfig $config, |
||
| 81 | $principalPrefix = 'principals/users/') { |
||
| 82 | $this->userManager = $userManager; |
||
| 83 | $this->groupManager = $groupManager; |
||
| 84 | $this->shareManager = $shareManager; |
||
| 85 | $this->userSession = $userSession; |
||
| 86 | $this->config = $config; |
||
| 87 | $this->principalPrefix = trim($principalPrefix, '/'); |
||
| 88 | $this->hasGroups = ($principalPrefix === 'principals/users/'); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns a list of principals based on a prefix. |
||
| 93 | * |
||
| 94 | * This prefix will often contain something like 'principals'. You are only |
||
| 95 | * expected to return principals that are in this base path. |
||
| 96 | * |
||
| 97 | * You are expected to return at least a 'uri' for every user, you can |
||
| 98 | * return any additional properties if you wish so. Common properties are: |
||
| 99 | * {DAV:}displayname |
||
| 100 | * |
||
| 101 | * @param string $prefixPath |
||
| 102 | * @return string[] |
||
| 103 | */ |
||
| 104 | public function getPrincipalsByPrefix($prefixPath) { |
||
| 105 | $principals = []; |
||
| 106 | |||
| 107 | if ($prefixPath === $this->principalPrefix) { |
||
| 108 | foreach($this->userManager->search('') as $user) { |
||
| 109 | $principals[] = $this->userToPrincipal($user); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return $principals; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns a specific principal, specified by it's path. |
||
| 118 | * The returned structure should be the exact same as from |
||
| 119 | * getPrincipalsByPrefix. |
||
| 120 | * |
||
| 121 | * @param string $path |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | public function getPrincipalByPath($path) { |
||
| 125 | list($prefix, $name) = \Sabre\Uri\split($path); |
||
| 126 | |||
| 127 | if ($prefix === $this->principalPrefix) { |
||
| 128 | $user = $this->userManager->get($name); |
||
| 129 | |||
| 130 | if ($user !== null) { |
||
| 131 | return $this->userToPrincipal($user); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | return null; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns the list of members for a group-principal |
||
| 139 | * |
||
| 140 | * @param string $principal |
||
| 141 | * @return string[] |
||
| 142 | * @throws Exception |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function getGroupMemberSet($principal) { |
|
| 145 | // TODO: for now the group principal has only one member, the user itself |
||
| 146 | $principal = $this->getPrincipalByPath($principal); |
||
| 147 | if (!$principal) { |
||
| 148 | throw new Exception('Principal not found'); |
||
| 149 | } |
||
| 150 | |||
| 151 | return [$principal['uri']]; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the list of groups a principal is a member of |
||
| 156 | * |
||
| 157 | * @param string $principal |
||
| 158 | * @param bool $needGroups |
||
| 159 | * @return array |
||
| 160 | * @throws Exception |
||
| 161 | */ |
||
| 162 | public function getGroupMembership($principal, $needGroups = false) { |
||
| 163 | list($prefix, $name) = \Sabre\Uri\split($principal); |
||
| 164 | |||
| 165 | if ($prefix === $this->principalPrefix) { |
||
| 166 | $user = $this->userManager->get($name); |
||
| 167 | if (!$user) { |
||
| 168 | throw new Exception('Principal not found'); |
||
| 169 | } |
||
| 170 | |||
| 171 | if ($this->hasGroups || $needGroups) { |
||
| 172 | $groups = $this->groupManager->getUserGroups($user); |
||
| 173 | $groups = array_map(function($group) { |
||
| 174 | /** @var IGroup $group */ |
||
| 175 | return 'principals/groups/' . urlencode($group->getGID()); |
||
| 176 | }, $groups); |
||
| 177 | |||
| 178 | return $groups; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | return []; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Updates the list of group members for a group principal. |
||
| 186 | * |
||
| 187 | * The principals should be passed as a list of uri's. |
||
| 188 | * |
||
| 189 | * @param string $principal |
||
| 190 | * @param string[] $members |
||
| 191 | * @throws Exception |
||
| 192 | */ |
||
| 193 | public function setGroupMemberSet($principal, array $members) { |
||
| 194 | throw new Exception('Setting members of the group is not supported yet'); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $path |
||
| 199 | * @param PropPatch $propPatch |
||
| 200 | * @return int |
||
| 201 | */ |
||
| 202 | function updatePrincipal($path, PropPatch $propPatch) { |
||
|
|
|||
| 203 | return 0; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Search user principals |
||
| 208 | * |
||
| 209 | * @param array $searchProperties |
||
| 210 | * @param string $test |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
||
| 214 | $results = []; |
||
| 215 | |||
| 216 | // If sharing is disabled, return the empty array |
||
| 217 | $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
||
| 218 | if (!$shareAPIEnabled) { |
||
| 219 | return []; |
||
| 220 | } |
||
| 221 | |||
| 222 | // If sharing is restricted to group members only, |
||
| 223 | // return only members that have groups in common |
||
| 224 | $restrictGroups = false; |
||
| 225 | View Code Duplication | if ($this->shareManager->shareWithGroupMembersOnly()) { |
|
| 226 | $user = $this->userSession->getUser(); |
||
| 227 | if (!$user) { |
||
| 228 | return []; |
||
| 229 | } |
||
| 230 | |||
| 231 | $restrictGroups = $this->groupManager->getUserGroupIds($user); |
||
| 232 | } |
||
| 233 | |||
| 234 | foreach ($searchProperties as $prop => $value) { |
||
| 235 | switch ($prop) { |
||
| 236 | case '{http://sabredav.org/ns}email-address': |
||
| 237 | $users = $this->userManager->getByEmail($value); |
||
| 238 | |||
| 239 | $results[] = array_reduce($users, function(array $carry, IUser $user) use ($restrictGroups) { |
||
| 240 | // is sharing restricted to groups only? |
||
| 241 | View Code Duplication | if ($restrictGroups !== false) { |
|
| 242 | $userGroups = $this->groupManager->getUserGroupIds($user); |
||
| 243 | if (count(array_intersect($userGroups, $restrictGroups)) === 0) { |
||
| 244 | return $carry; |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | $carry[] = $this->principalPrefix . '/' . $user->getUID(); |
||
| 249 | return $carry; |
||
| 250 | }, []); |
||
| 251 | break; |
||
| 252 | |||
| 253 | default: |
||
| 254 | $results[] = []; |
||
| 255 | break; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | // results is an array of arrays, so this is not the first search result |
||
| 260 | // but the results of the first searchProperty |
||
| 261 | if (count($results) === 1) { |
||
| 262 | return $results[0]; |
||
| 263 | } |
||
| 264 | |||
| 265 | switch ($test) { |
||
| 266 | case 'anyof': |
||
| 267 | return array_unique(array_merge(...$results)); |
||
| 268 | |||
| 269 | case 'allof': |
||
| 270 | default: |
||
| 271 | return array_intersect(...$results); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $prefixPath |
||
| 277 | * @param array $searchProperties |
||
| 278 | * @param string $test |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $uri |
||
| 297 | * @param string $principalPrefix |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | function findByUri($uri, $principalPrefix) { |
||
| 301 | // If sharing is disabled, return the empty array |
||
| 302 | $shareAPIEnabled = $this->shareManager->shareApiEnabled(); |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param IUser $user |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | protected function userToPrincipal($user) { |
||
| 367 | |||
| 368 | public function getPrincipalPrefix() { |
||
| 371 | |||
| 372 | } |
||
| 373 |
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.