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 |
||
| 43 | class Principal implements BackendInterface { |
||
| 44 | |||
| 45 | /** @var IUserManager */ |
||
| 46 | private $userManager; |
||
| 47 | |||
| 48 | /** @var IGroupManager */ |
||
| 49 | private $groupManager; |
||
| 50 | |||
| 51 | /** @var IShareManager */ |
||
| 52 | private $shareManager; |
||
| 53 | |||
| 54 | /** @var IUserSession */ |
||
| 55 | private $userSession; |
||
| 56 | |||
| 57 | /** @var string */ |
||
| 58 | private $principalPrefix; |
||
| 59 | |||
| 60 | /** @var bool */ |
||
| 61 | private $hasGroups; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param IUserManager $userManager |
||
| 65 | * @param IGroupManager $groupManager |
||
| 66 | * @param IShareManager $shareManager |
||
| 67 | * @param IUserSession $userSession |
||
| 68 | * @param string $principalPrefix |
||
| 69 | */ |
||
| 70 | public function __construct(IUserManager $userManager, |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns a list of principals based on a prefix. |
||
| 85 | * |
||
| 86 | * This prefix will often contain something like 'principals'. You are only |
||
| 87 | * expected to return principals that are in this base path. |
||
| 88 | * |
||
| 89 | * You are expected to return at least a 'uri' for every user, you can |
||
| 90 | * return any additional properties if you wish so. Common properties are: |
||
| 91 | * {DAV:}displayname |
||
| 92 | * |
||
| 93 | * @param string $prefixPath |
||
| 94 | * @return string[] |
||
| 95 | */ |
||
| 96 | public function getPrincipalsByPrefix($prefixPath) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns a specific principal, specified by it's path. |
||
| 110 | * The returned structure should be the exact same as from |
||
| 111 | * getPrincipalsByPrefix. |
||
| 112 | * |
||
| 113 | * @param string $path |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function getPrincipalByPath($path) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns the list of members for a group-principal |
||
| 131 | * |
||
| 132 | * @param string $principal |
||
| 133 | * @return string[] |
||
| 134 | * @throws Exception |
||
| 135 | */ |
||
| 136 | View Code Duplication | public function getGroupMemberSet($principal) { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the list of groups a principal is a member of |
||
| 148 | * |
||
| 149 | * @param string $principal |
||
| 150 | * @param bool $needGroups |
||
| 151 | * @return array |
||
| 152 | * @throws Exception |
||
| 153 | */ |
||
| 154 | public function getGroupMembership($principal, $needGroups = false) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Updates the list of group members for a group principal. |
||
| 178 | * |
||
| 179 | * The principals should be passed as a list of uri's. |
||
| 180 | * |
||
| 181 | * @param string $principal |
||
| 182 | * @param string[] $members |
||
| 183 | * @throws Exception |
||
| 184 | */ |
||
| 185 | public function setGroupMemberSet($principal, array $members) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $path |
||
| 191 | * @param PropPatch $propPatch |
||
| 192 | * @return int |
||
| 193 | */ |
||
| 194 | function updatePrincipal($path, PropPatch $propPatch) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Search user principals |
||
| 200 | * |
||
| 201 | * @param array $searchProperties |
||
| 202 | * @param string $test |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $prefixPath |
||
| 268 | * @param array $searchProperties |
||
| 269 | * @param string $test |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $uri |
||
| 288 | * @param string $principalPrefix |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | function findByUri($uri, $principalPrefix) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param IUser $user |
||
| 340 | * @return array |
||
| 341 | */ |
||
| 342 | protected function userToPrincipal($user) { |
||
| 357 | |||
| 358 | public function getPrincipalPrefix() { |
||
| 361 | |||
| 362 | } |
||
| 363 |
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.