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 |
||
| 37 | class GroupPrincipalBackend implements BackendInterface { |
||
| 38 | |||
| 39 | const PRINCIPAL_PREFIX = 'principals/groups'; |
||
| 40 | |||
| 41 | /** @var IGroupManager */ |
||
| 42 | private $groupManager; |
||
| 43 | |||
| 44 | /** @var IUserSession */ |
||
| 45 | private $userSession; |
||
| 46 | |||
| 47 | /** @var IShareManager */ |
||
| 48 | private $shareManager; |
||
| 49 | |||
| 50 | /** @var IL10N */ |
||
| 51 | private $l10n; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param IGroupManager $IGroupManager |
||
| 55 | * @param IUserSession $userSession |
||
| 56 | * @param IShareManager $shareManager |
||
| 57 | * @param IL10N $l10n |
||
| 58 | */ |
||
| 59 | public function __construct(IGroupManager $IGroupManager, |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns a list of principals based on a prefix. |
||
| 71 | * |
||
| 72 | * This prefix will often contain something like 'principals'. You are only |
||
| 73 | * expected to return principals that are in this base path. |
||
| 74 | * |
||
| 75 | * You are expected to return at least a 'uri' for every user, you can |
||
| 76 | * return any additional properties if you wish so. Common properties are: |
||
| 77 | * {DAV:}displayname |
||
| 78 | * |
||
| 79 | * @param string $prefixPath |
||
| 80 | * @return string[] |
||
| 81 | */ |
||
| 82 | public function getPrincipalsByPrefix($prefixPath) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns a specific principal, specified by it's path. |
||
| 96 | * The returned structure should be the exact same as from |
||
| 97 | * getPrincipalsByPrefix. |
||
| 98 | * |
||
| 99 | * @param string $path |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | public function getPrincipalByPath($path) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns the list of members for a group-principal |
||
| 122 | * |
||
| 123 | * @param string $principal |
||
| 124 | * @return string[] |
||
| 125 | * @throws Exception |
||
| 126 | */ |
||
| 127 | 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 | * @throws Exception |
||
| 153 | */ |
||
| 154 | public function getGroupMembership($principal) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Updates the list of group members for a group principal. |
||
| 160 | * |
||
| 161 | * The principals should be passed as a list of uri's. |
||
| 162 | * |
||
| 163 | * @param string $principal |
||
| 164 | * @param string[] $members |
||
| 165 | * @throws Exception |
||
| 166 | */ |
||
| 167 | public function setGroupMemberSet($principal, array $members) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $path |
||
| 173 | * @param PropPatch $propPatch |
||
| 174 | * @return int |
||
| 175 | */ |
||
| 176 | function updatePrincipal($path, PropPatch $propPatch) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $prefixPath |
||
| 182 | * @param array $searchProperties |
||
| 183 | * @param string $test |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | function searchPrincipals($prefixPath, array $searchProperties, $test = 'allof') { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $uri |
||
| 256 | * @param string $principalPrefix |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | function findByUri($uri, $principalPrefix) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param IGroup $group |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | protected function groupToPrincipal($group) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param IUser $user |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | View Code Duplication | protected function userToPrincipal($user) { |
|
| 325 | } |
||
| 326 |
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.