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 |
||
| 38 | class LDAPProvider implements ILDAPProvider, IDeletionFlagSupport { |
||
| 39 | |||
| 40 | private $userBackend; |
||
| 41 | private $groupBackend; |
||
| 42 | private $logger; |
||
| 43 | private $helper; |
||
| 44 | private $deletedUsersIndex; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Create new LDAPProvider |
||
| 48 | * @param \OCP\IServerContainer $serverContainer |
||
| 49 | * @param Helper $helper |
||
| 50 | * @param DeletedUsersIndex $deletedUsersIndex |
||
| 51 | * @throws \Exception if user_ldap app was not enabled |
||
| 52 | */ |
||
| 53 | public function __construct(IServerContainer $serverContainer, Helper $helper, DeletedUsersIndex $deletedUsersIndex) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Translate an user id to LDAP DN |
||
| 83 | * @param string $uid user id |
||
| 84 | * @return string with the LDAP DN |
||
| 85 | * @throws \Exception if translation was unsuccessful |
||
| 86 | */ |
||
| 87 | public function getUserDN($uid) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Translate a group id to LDAP DN. |
||
| 100 | * @param string $gid group id |
||
| 101 | * @return string |
||
| 102 | * @throws \Exception |
||
| 103 | */ |
||
| 104 | public function getGroupDN($gid) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Translate a LDAP DN to an internal user name. If there is no mapping between |
||
| 117 | * the DN and the user name, a new one will be created. |
||
| 118 | * @param string $dn LDAP DN |
||
| 119 | * @return string with the internal user name |
||
| 120 | * @throws \Exception if translation was unsuccessful |
||
| 121 | */ |
||
| 122 | public function getUserName($dn) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Convert a stored DN so it can be used as base parameter for LDAP queries. |
||
| 132 | * @param string $dn the DN in question |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function DNasBaseParameter($dn) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Sanitize a DN received from the LDAP server. |
||
| 141 | * @param array $dn the DN in question |
||
| 142 | * @return array the sanitized DN |
||
| 143 | */ |
||
| 144 | public function sanitizeDN($dn) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Return a new LDAP connection resource for the specified user. |
||
| 150 | * The connection must be closed manually. |
||
| 151 | * @param string $uid user id |
||
| 152 | * @return resource of the LDAP connection |
||
| 153 | * @throws \Exception if user id was not found in LDAP |
||
| 154 | */ |
||
| 155 | public function getLDAPConnection($uid) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Return a new LDAP connection resource for the specified user. |
||
| 164 | * The connection must be closed manually. |
||
| 165 | * @param string $gid group id |
||
| 166 | * @return resource of the LDAP connection |
||
| 167 | * @throws \Exception if group id was not found in LDAP |
||
| 168 | */ |
||
| 169 | public function getGroupLDAPConnection($gid) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the LDAP base for users. |
||
| 178 | * @param string $uid user id |
||
| 179 | * @return string the base for users |
||
| 180 | * @throws \Exception if user id was not found in LDAP |
||
| 181 | */ |
||
| 182 | public function getLDAPBaseUsers($uid) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the LDAP base for groups. |
||
| 191 | * @param string $uid user id |
||
| 192 | * @return string the base for groups |
||
| 193 | * @throws \Exception if user id was not found in LDAP |
||
| 194 | */ |
||
| 195 | public function getLDAPBaseGroups($uid) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Clear the cache if a cache is used, otherwise do nothing. |
||
| 204 | * @param string $uid user id |
||
| 205 | * @throws \Exception if user id was not found in LDAP |
||
| 206 | */ |
||
| 207 | public function clearCache($uid) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Clear the cache if a cache is used, otherwise do nothing. |
||
| 216 | * Acts on the LDAP connection of a group |
||
| 217 | * @param string $gid group id |
||
| 218 | * @throws \Exception if user id was not found in LDAP |
||
| 219 | */ |
||
| 220 | public function clearGroupCache($gid) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Check whether a LDAP DN exists |
||
| 229 | * @param string $dn LDAP DN |
||
| 230 | * @return bool whether the DN exists |
||
| 231 | */ |
||
| 232 | public function dnExists($dn) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Flag record for deletion. |
||
| 239 | * @param string $uid user id |
||
| 240 | */ |
||
| 241 | public function flagRecord($uid) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Unflag record for deletion. |
||
| 247 | * @param string $uid user id |
||
| 248 | */ |
||
| 249 | public function unflagRecord($uid) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the LDAP attribute name for the user's display name |
||
| 255 | * @param string $uid user id |
||
| 256 | * @return string the display name field |
||
| 257 | * @throws \Exception if user id was not found in LDAP |
||
| 258 | */ |
||
| 259 | public function getLDAPDisplayNameField($uid) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get the LDAP attribute name for the email |
||
| 268 | * @param string $uid user id |
||
| 269 | * @return string the email field |
||
| 270 | * @throws \Exception if user id was not found in LDAP |
||
| 271 | */ |
||
| 272 | public function getLDAPEmailField($uid) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get the LDAP type of association between users and groups |
||
| 281 | * @param string $gid group id |
||
| 282 | * @return string the configuration, one of: 'memberUid', 'uniqueMember', 'member', 'gidNumber' |
||
| 283 | * @throws \Exception if group id was not found in LDAP |
||
| 284 | */ |
||
| 285 | public function getLDAPGroupMemberAssoc($gid) { |
||
| 291 | |||
| 292 | } |
||
| 293 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.