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 SubAdmin extends PublicEmitter { |
||
| 38 | |||
| 39 | /** @var IUserManager */ |
||
| 40 | private $userManager; |
||
| 41 | |||
| 42 | /** @var IGroupManager */ |
||
| 43 | private $groupManager; |
||
| 44 | |||
| 45 | /** @var IDBConnection */ |
||
| 46 | private $dbConn; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param IUserManager $userManager |
||
| 50 | * @param IGroupManager $groupManager |
||
| 51 | * @param IDBConnection $dbConn |
||
| 52 | */ |
||
| 53 | public function __construct(IUserManager $userManager, |
||
| 67 | |||
| 68 | /** |
||
| 69 | * add a SubAdmin |
||
| 70 | * @param IUser $user user to be SubAdmin |
||
| 71 | * @param IGroup $group group $user becomes subadmin of |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | public function createSubAdmin(IUser $user, IGroup $group) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * delete a SubAdmin |
||
| 91 | * @param IUser $user the user that is the SubAdmin |
||
| 92 | * @param IGroup $group the group |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function deleteSubAdmin(IUser $user, IGroup $group) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * get groups of a SubAdmin |
||
| 110 | * @param IUser $user the SubAdmin |
||
| 111 | * @return IGroup[] |
||
| 112 | */ |
||
| 113 | View Code Duplication | public function getSubAdminsGroups(IUser $user) { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * get SubAdmins of a group |
||
| 135 | * @param IGroup $group the group |
||
| 136 | * @return IUser[] |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function getGroupsSubAdmins(IGroup $group) { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * get all SubAdmins |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | public function getAllSubAdmins() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * checks if a user is a SubAdmin of a group |
||
| 187 | * @param IUser $user |
||
| 188 | * @param IGroup $group |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | public function isSubAdminOfGroup(IUser $user, IGroup $group) { |
||
| 192 | $qb = $this->dbConn->getQueryBuilder(); |
||
| 193 | |||
| 194 | /* |
||
| 195 | * Primary key is ('gid', 'uid') so max 1 result possible here |
||
| 196 | */ |
||
| 197 | $result = $qb->select('*') |
||
| 198 | ->from('group_admin') |
||
| 199 | ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) |
||
| 200 | ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
||
| 201 | ->execute(); |
||
| 202 | |||
| 203 | $fetch = $result->fetch(); |
||
| 204 | $result->closeCursor(); |
||
| 205 | $result = !empty($fetch) ? true : false; |
||
| 206 | |||
| 207 | return $result; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * checks if a user is a SubAdmin |
||
| 212 | * @param IUser $user |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | public function isSubAdmin(IUser $user) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * checks if a user is a accessible by a subadmin |
||
| 239 | * @param IUser $subadmin |
||
| 240 | * @param IUser $user |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function isUserAccessible($subadmin, $user) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * delete all SubAdmins by $user |
||
| 261 | * @param IUser $user |
||
| 262 | * @return boolean |
||
| 263 | */ |
||
| 264 | View Code Duplication | private function post_deleteUser($user) { |
|
| 273 | |||
| 274 | /** |
||
| 275 | * delete all SubAdmins by $group |
||
| 276 | * @param IGroup $group |
||
| 277 | * @return boolean |
||
| 278 | */ |
||
| 279 | View Code Duplication | private function post_deleteGroup($group) { |
|
| 288 | } |
||
| 289 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.