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 AbstractPrincipalBackend 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 AbstractPrincipalBackend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | abstract class AbstractPrincipalBackend implements BackendInterface { |
||
| 34 | |||
| 35 | /** @var IDBConnection */ |
||
| 36 | private $db; |
||
| 37 | |||
| 38 | /** @var IUserSession */ |
||
| 39 | private $userSession; |
||
| 40 | |||
| 41 | /** @var IGroupManager */ |
||
| 42 | private $groupManager; |
||
| 43 | |||
| 44 | /** @var ILogger */ |
||
| 45 | private $logger; |
||
| 46 | |||
| 47 | /** @var string */ |
||
| 48 | private $principalPrefix; |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $dbTableName; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param IDBConnection $dbConnection |
||
| 55 | * @param IUserSession $userSession |
||
| 56 | * @param IGroupManager $groupManager |
||
| 57 | * @param ILogger $logger |
||
| 58 | * @param string $principalPrefix |
||
| 59 | * @param string $dbPrefix |
||
| 60 | */ |
||
| 61 | public function __construct(IDBConnection $dbConnection, |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Returns a list of principals based on a prefix. |
||
| 76 | * |
||
| 77 | * This prefix will often contain something like 'principals'. You are only |
||
| 78 | * expected to return principals that are in this base path. |
||
| 79 | * |
||
| 80 | * You are expected to return at least a 'uri' for every user, you can |
||
| 81 | * return any additional properties if you wish so. Common properties are: |
||
| 82 | * {DAV:}displayname |
||
| 83 | * |
||
| 84 | * @param string $prefixPath |
||
| 85 | * @return string[] |
||
| 86 | */ |
||
| 87 | public function getPrincipalsByPrefix($prefixPath) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns a specific principal, specified by it's path. |
||
| 108 | * The returned structure should be the exact same as from |
||
| 109 | * getPrincipalsByPrefix. |
||
| 110 | * |
||
| 111 | * @param string $path |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function getPrincipalByPath($path) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns the list of members for a group-principal |
||
| 139 | * |
||
| 140 | * @param string $principal |
||
| 141 | * @return string[] |
||
| 142 | */ |
||
| 143 | public function getGroupMemberSet($principal) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the list of groups a principal is a member of |
||
| 149 | * |
||
| 150 | * @param string $principal |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function getGroupMembership($principal) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Updates the list of group members for a group principal. |
||
| 159 | * |
||
| 160 | * The principals should be passed as a list of uri's. |
||
| 161 | * |
||
| 162 | * @param string $principal |
||
| 163 | * @param string[] $members |
||
| 164 | * @throws Exception |
||
| 165 | */ |
||
| 166 | public function setGroupMemberSet($principal, array $members) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $path |
||
| 172 | * @param PropPatch $propPatch |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | function updatePrincipal($path, PropPatch $propPatch) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $prefixPath |
||
| 181 | * @param array $searchProperties |
||
| 182 | * @param string $test |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $uri |
||
| 264 | * @param string $principalPrefix |
||
| 265 | * @return null|string |
||
| 266 | */ |
||
| 267 | function findByUri($uri, $principalPrefix) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * convert database row to principal |
||
| 326 | */ |
||
| 327 | private function rowToPrincipal($row) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param $row |
||
| 337 | * @param $userGroups |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | private function isAllowedToAccessResource($row, $userGroups) { |
||
| 361 | } |
||
| 362 |
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.