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 IConfig */ |
||
| 59 | private $config; |
||
| 60 | |||
| 61 | /** @var string */ |
||
| 62 | private $principalPrefix; |
||
| 63 | |||
| 64 | /** @var bool */ |
||
| 65 | private $hasGroups; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param IUserManager $userManager |
||
| 69 | * @param IGroupManager $groupManager |
||
| 70 | * @param IShareManager $shareManager |
||
| 71 | * @param IUserSession $userSession |
||
| 72 | * @param IConfig $config |
||
| 73 | * @param string $principalPrefix |
||
| 74 | */ |
||
| 75 | public function __construct(IUserManager $userManager, |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns a list of principals based on a prefix. |
||
| 92 | * |
||
| 93 | * This prefix will often contain something like 'principals'. You are only |
||
| 94 | * expected to return principals that are in this base path. |
||
| 95 | * |
||
| 96 | * You are expected to return at least a 'uri' for every user, you can |
||
| 97 | * return any additional properties if you wish so. Common properties are: |
||
| 98 | * {DAV:}displayname |
||
| 99 | * |
||
| 100 | * @param string $prefixPath |
||
| 101 | * @return string[] |
||
| 102 | */ |
||
| 103 | public function getPrincipalsByPrefix($prefixPath) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns a specific principal, specified by it's path. |
||
| 117 | * The returned structure should be the exact same as from |
||
| 118 | * getPrincipalsByPrefix. |
||
| 119 | * |
||
| 120 | * @param string $path |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | public function getPrincipalByPath($path) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Returns the list of members for a group-principal |
||
| 138 | * |
||
| 139 | * @param string $principal |
||
| 140 | * @return string[] |
||
| 141 | * @throws Exception |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function getGroupMemberSet($principal) { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Returns the list of groups a principal is a member of |
||
| 155 | * |
||
| 156 | * @param string $principal |
||
| 157 | * @param bool $needGroups |
||
| 158 | * @return array |
||
| 159 | * @throws Exception |
||
| 160 | */ |
||
| 161 | public function getGroupMembership($principal, $needGroups = false) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Updates the list of group members for a group principal. |
||
| 185 | * |
||
| 186 | * The principals should be passed as a list of uri's. |
||
| 187 | * |
||
| 188 | * @param string $principal |
||
| 189 | * @param string[] $members |
||
| 190 | * @throws Exception |
||
| 191 | */ |
||
| 192 | public function setGroupMemberSet($principal, array $members) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $path |
||
| 198 | * @param PropPatch $propPatch |
||
| 199 | * @return int |
||
| 200 | */ |
||
| 201 | function updatePrincipal($path, PropPatch $propPatch) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Search user principals |
||
| 207 | * |
||
| 208 | * @param array $searchProperties |
||
| 209 | * @param string $test |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | protected function searchUserPrincipals(array $searchProperties, $test = 'allof') { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $prefixPath |
||
| 276 | * @param array $searchProperties |
||
| 277 | * @param string $test |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $uri |
||
| 296 | * @param string $principalPrefix |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | function findByUri($uri, $principalPrefix) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param IUser $user |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | protected function userToPrincipal($user) { |
||
| 366 | |||
| 367 | public function getPrincipalPrefix() { |
||
| 370 | |||
| 371 | } |
||
| 372 |
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.