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 |
||
| 48 | class Database extends Backend { |
||
| 49 | |||
| 50 | /** @var string[] */ |
||
| 51 | private $groupCache = []; |
||
| 52 | |||
| 53 | /** @var IDBConnection */ |
||
| 54 | private $dbConn; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * \OC\Group\Database constructor. |
||
| 58 | * |
||
| 59 | * @param IDBConnection|null $dbConn |
||
| 60 | */ |
||
| 61 | public function __construct(IDBConnection $dbConn = null) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * FIXME: This function should not be required! |
||
| 67 | */ |
||
| 68 | private function fixDI() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Try to create a new group |
||
| 76 | * @param string $gid The name of the group to create |
||
| 77 | * @return bool |
||
| 78 | * |
||
| 79 | * Tries to create a new group. If the group name already exists, false will |
||
| 80 | * be returned. |
||
| 81 | */ |
||
| 82 | public function createGroup( $gid ) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * delete a group |
||
| 98 | * @param string $gid gid of the group to delete |
||
| 99 | * @return bool |
||
| 100 | * |
||
| 101 | * Deletes a group and removes it from the group_user-table |
||
| 102 | */ |
||
| 103 | public function deleteGroup( $gid ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * is user in group? |
||
| 132 | * @param string $uid uid of the user |
||
| 133 | * @param string $gid gid of the group |
||
| 134 | * @return bool |
||
| 135 | * |
||
| 136 | * Checks whether the user is member of a group or not. |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function inGroup( $uid, $gid ) { |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Add a user to a group |
||
| 157 | * @param string $uid Name of the user to add to group |
||
| 158 | * @param string $gid Name of the group in which add the user |
||
| 159 | * @return bool |
||
| 160 | * |
||
| 161 | * Adds a user to a group. |
||
| 162 | */ |
||
| 163 | public function addToGroup( $uid, $gid ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Removes a user from a group |
||
| 181 | * @param string $uid Name of the user to remove from group |
||
| 182 | * @param string $gid Name of the group from which remove the user |
||
| 183 | * @return bool |
||
| 184 | * |
||
| 185 | * removes the user from a group. |
||
| 186 | */ |
||
| 187 | public function removeFromGroup( $uid, $gid ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get all groups a user belongs to |
||
| 201 | * @param string $uid Name of the user |
||
| 202 | * @return array an array of group names |
||
| 203 | * |
||
| 204 | * This function fetches all groups a user belongs to. It does not check |
||
| 205 | * if the user exists at all. |
||
| 206 | */ |
||
| 207 | public function getUserGroups( $uid ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * get a list of all groups |
||
| 234 | * @param string $search |
||
| 235 | * @param int $limit |
||
| 236 | * @param int $offset |
||
| 237 | * @return array an array of group names |
||
| 238 | * |
||
| 239 | * Returns a list with all groups |
||
| 240 | */ |
||
| 241 | public function getGroups($search = '', $limit = null, $offset = null) { |
||
| 242 | $this->fixDI(); |
||
| 243 | |||
| 244 | $query = $this->dbConn->getQueryBuilder(); |
||
| 245 | $query->select('gid') |
||
| 246 | ->from('groups') |
||
| 247 | ->orderBy('gid', 'ASC'); |
||
| 248 | |||
| 249 | View Code Duplication | if ($search !== '') { |
|
| 250 | $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
||
| 251 | '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
||
| 252 | ))); |
||
| 253 | } |
||
| 254 | |||
| 255 | $query->setMaxResults($limit) |
||
| 256 | ->setFirstResult($offset); |
||
| 257 | $result = $query->execute(); |
||
| 258 | |||
| 259 | $groups = []; |
||
| 260 | while ($row = $result->fetch()) { |
||
| 261 | $groups[] = $row['gid']; |
||
| 262 | } |
||
| 263 | $result->closeCursor(); |
||
| 264 | |||
| 265 | return $groups; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * check if a group exists |
||
| 270 | * @param string $gid |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | public function groupExists($gid) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * get a list of all users in a group |
||
| 298 | * @param string $gid |
||
| 299 | * @param string $search |
||
| 300 | * @param int $limit |
||
| 301 | * @param int $offset |
||
| 302 | * @return array an array of user ids |
||
| 303 | */ |
||
| 304 | public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * get the number of all users matching the search string in a group |
||
| 334 | * @param string $gid |
||
| 335 | * @param string $search |
||
| 336 | * @return int|false |
||
| 337 | */ |
||
| 338 | public function countUsersInGroup($gid, $search = '') { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * get the number of disabled users in a group |
||
| 364 | * |
||
| 365 | * @param string $search |
||
| 366 | * @return int|bool |
||
| 367 | */ |
||
| 368 | public function countDisabledInGroup($gid) { |
||
| 388 | |||
| 389 | } |
||
| 390 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.