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 |
||
| 29 | class Group_Proxy extends Proxy implements \OCP\GroupInterface { |
||
| 30 | private $backends = array(); |
||
| 31 | private $refBackend = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Constructor |
||
| 35 | * @param string[] $serverConfigPrefixes array containing the config Prefixes |
||
| 36 | */ |
||
| 37 | View Code Duplication | public function __construct($serverConfigPrefixes, ILDAPWrapper $ldap) { |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Tries the backends one after the other until a positive result is returned from the specified method |
||
| 50 | * @param string $gid the gid connected to the request |
||
| 51 | * @param string $method the method of the group backend that shall be called |
||
| 52 | * @param array $parameters an array of parameters to be passed |
||
| 53 | * @return mixed, the result of the method or false |
||
| 54 | */ |
||
| 55 | protected function walkBackends($gid, $method, $parameters) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Asks the backend connected to the server that supposely takes care of the gid from the request. |
||
| 68 | * @param string $gid the gid connected to the request |
||
| 69 | * @param string $method the method of the group backend that shall be called |
||
| 70 | * @param array $parameters an array of parameters to be passed |
||
| 71 | * @param mixed $passOnWhen the result matches this variable |
||
| 72 | * @return mixed, the result of the method or false |
||
| 73 | */ |
||
| 74 | protected function callOnLastSeenOn($gid, $method, $parameters, $passOnWhen) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * is user in group? |
||
| 100 | * @param string $uid uid of the user |
||
| 101 | * @param string $gid gid of the group |
||
| 102 | * @return bool |
||
| 103 | * |
||
| 104 | * Checks whether the user is member of a group or not. |
||
| 105 | */ |
||
| 106 | public function inGroup($uid, $gid) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get all groups a user belongs to |
||
| 112 | * @param string $uid Name of the user |
||
| 113 | * @return string[] with group names |
||
| 114 | * |
||
| 115 | * This function fetches all groups a user belongs to. It does not check |
||
| 116 | * if the user exists at all. |
||
| 117 | */ |
||
| 118 | public function getUserGroups($uid) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * get a list of all users in a group |
||
| 133 | * @return string[] with user ids |
||
| 134 | */ |
||
| 135 | View Code Duplication | public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 147 | |||
| 148 | /** |
||
| 149 | * returns the number of users in a group, who match the search term |
||
| 150 | * @param string $gid the internal group name |
||
| 151 | * @param string $search optional, a search string |
||
| 152 | * @return int|bool |
||
| 153 | */ |
||
| 154 | public function countUsersInGroup($gid, $search = '') { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * get a list of all groups |
||
| 161 | * @return string[] with group names |
||
| 162 | * |
||
| 163 | * Returns a list with all groups |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 177 | |||
| 178 | /** |
||
| 179 | * check if a group exists |
||
| 180 | * @param string $gid |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function groupExists($gid) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Check if backend implements actions |
||
| 189 | * @param int $actions bitwise-or'ed actions |
||
| 190 | * @return boolean |
||
| 191 | * |
||
| 192 | * Returns the supported actions as int to be |
||
| 193 | * compared with OC_USER_BACKEND_CREATE_USER etc. |
||
| 194 | */ |
||
| 195 | public function implementsActions($actions) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Return access for LDAP interaction. |
||
| 202 | * @param string $gid |
||
| 203 | * @return Access instance of Access for LDAP interaction |
||
| 204 | */ |
||
| 205 | public function getLDAPAccess($gid) { |
||
| 208 | } |
||
| 209 |
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.