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:
| 1 | <?php |
||
| 40 | class Principal implements BackendInterface { |
||
| 41 | |||
| 42 | /** @var IUserManager */ |
||
| 43 | private $userManager; |
||
| 44 | |||
| 45 | /** @var IGroupManager */ |
||
| 46 | private $groupManager; |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | private $principalPrefix; |
||
| 50 | |||
| 51 | /** @var bool */ |
||
| 52 | private $hasGroups; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param IUserManager $userManager |
||
| 56 | * @param IGroupManager $groupManager |
||
| 57 | * @param string $principalPrefix |
||
| 58 | */ |
||
| 59 | public function __construct(IUserManager $userManager, |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returns a list of principals based on a prefix. |
||
| 70 | * |
||
| 71 | * This prefix will often contain something like 'principals'. You are only |
||
| 72 | * expected to return principals that are in this base path. |
||
| 73 | * |
||
| 74 | * You are expected to return at least a 'uri' for every user, you can |
||
| 75 | * return any additional properties if you wish so. Common properties are: |
||
| 76 | * {DAV:}displayname |
||
| 77 | * |
||
| 78 | * @param string $prefixPath |
||
| 79 | * @return string[] |
||
| 80 | */ |
||
| 81 | public function getPrincipalsByPrefix($prefixPath) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns a specific principal, specified by it's path. |
||
| 95 | * The returned structure should be the exact same as from |
||
| 96 | * getPrincipalsByPrefix. |
||
| 97 | * |
||
| 98 | * @param string $path |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | public function getPrincipalByPath($path) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns the list of members for a group-principal |
||
| 116 | * |
||
| 117 | * @param string $principal |
||
| 118 | * @return string[] |
||
| 119 | * @throws Exception |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function getGroupMemberSet($principal) { |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the list of groups a principal is a member of |
||
| 133 | * |
||
| 134 | * @param string $principal |
||
| 135 | * @param bool $needGroups |
||
| 136 | * @return array |
||
| 137 | * @throws Exception |
||
| 138 | */ |
||
| 139 | public function getGroupMembership($principal, $needGroups = false) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Updates the list of group members for a group principal. |
||
| 163 | * |
||
| 164 | * The principals should be passed as a list of uri's. |
||
| 165 | * |
||
| 166 | * @param string $principal |
||
| 167 | * @param string[] $members |
||
| 168 | * @throws Exception |
||
| 169 | */ |
||
| 170 | public function setGroupMemberSet($principal, array $members) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param string $path |
||
| 176 | * @param PropPatch $propPatch |
||
| 177 | * @return int |
||
| 178 | */ |
||
| 179 | function updatePrincipal($path, PropPatch $propPatch) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $prefixPath |
||
| 185 | * @param array $searchProperties |
||
| 186 | * @param string $test |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $uri |
||
| 195 | * @param string $principalPrefix |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | function findByUri($uri, $principalPrefix) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param IUser $user |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | protected function userToPrincipal($user) { |
||
| 236 | |||
| 237 | public function getPrincipalPrefix() { |
||
| 240 | |||
| 241 | } |
||
| 242 |