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:
Complex classes like LDAPGateway often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LDAPGateway, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class LDAPGateway extends Object |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | * @config |
||
| 25 | */ |
||
| 26 | private static $options = []; |
||
|
|
|||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Zend\Ldap\Ldap |
||
| 30 | */ |
||
| 31 | private $ldap; |
||
| 32 | |||
| 33 | public function __construct() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Query the LDAP directory with the given filter. |
||
| 45 | * |
||
| 46 | * @param string $filter The string to filter by, e.g. (objectClass=user) |
||
| 47 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given |
||
| 48 | * @param int $scope The scope to perform the search. Zend_Ldap::SEARCH_SCOPE_ONE, Zend_LDAP::SEARCH_SCOPE_BASE. |
||
| 49 | * Default is Zend_Ldap::SEARCH_SCOPE_SUB |
||
| 50 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes |
||
| 51 | * @param string $sort Sort results by this attribute if given |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | protected function search($filter, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Authenticate the given username and password with LDAP. |
||
| 87 | * |
||
| 88 | * @param string $username |
||
| 89 | * @param string $password |
||
| 90 | * @return \Zend\Authentication\Result |
||
| 91 | */ |
||
| 92 | public function authenticate($username, $password) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Query for LDAP nodes (organizational units, containers, and domains). |
||
| 101 | * |
||
| 102 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given |
||
| 103 | * @param int $scope The scope to perform the search. Zend_Ldap::SEARCH_SCOPE_ONE, Zend_LDAP::SEARCH_SCOPE_BASE. Default is Zend_Ldap::SEARCH_SCOPE_SUB |
||
| 104 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes |
||
| 105 | * @param string $sort Sort results by this attribute if given |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | public function getNodes($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Query for LDAP groups. |
||
| 115 | * |
||
| 116 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given |
||
| 117 | * @param int $scope The scope to perform the search. Zend_Ldap::SEARCH_SCOPE_ONE, Zend_LDAP::SEARCH_SCOPE_BASE. Default is Zend_Ldap::SEARCH_SCOPE_SUB |
||
| 118 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes |
||
| 119 | * @param string $sort Sort results by this attribute if given |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getGroups($baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Return all nested AD groups underneath a specific DN |
||
| 129 | * |
||
| 130 | * @param string $dn |
||
| 131 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given |
||
| 132 | * @param int $scope The scope to perform the search. Zend_Ldap::SEARCH_SCOPE_ONE, Zend_LDAP::SEARCH_SCOPE_BASE. Default is Zend_Ldap::SEARCH_SCOPE_SUB |
||
| 133 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | public function getNestedGroups($dn, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return a particular LDAP group by objectGUID value. |
||
| 148 | * |
||
| 149 | * @param string $guid |
||
| 150 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given |
||
| 151 | * @param int $scope The scope to perform the search. Zend_Ldap::SEARCH_SCOPE_ONE, Zend_LDAP::SEARCH_SCOPE_BASE. Default is Zend_Ldap::SEARCH_SCOPE_SUB |
||
| 152 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function getGroupByGUID($guid, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Return a particular LDAP group by DN value. |
||
| 167 | * |
||
| 168 | * @param string $dn |
||
| 169 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given |
||
| 170 | * @param int $scope The scope to perform the search. Zend_Ldap::SEARCH_SCOPE_ONE, Zend_LDAP::SEARCH_SCOPE_BASE. Default is Zend_Ldap::SEARCH_SCOPE_SUB |
||
| 171 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public function getGroupByDN($dn, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Query for LDAP users, but don't include built-in user accounts. |
||
| 186 | * |
||
| 187 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given |
||
| 188 | * @param int $scope The scope to perform the search. Zend_Ldap::SEARCH_SCOPE_ONE, Zend_LDAP::SEARCH_SCOPE_BASE. Default is Zend_Ldap::SEARCH_SCOPE_SUB |
||
| 189 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes |
||
| 190 | * @param string $sort Sort results by this attribute if given |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getUsers($baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = [], $sort = '') |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return a particular LDAP user by objectGUID value. |
||
| 206 | * |
||
| 207 | * @param string $guid |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | View Code Duplication | public function getUserByGUID($guid, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Return a particular LDAP user by DN value. |
||
| 222 | * |
||
| 223 | * @param string $dn |
||
| 224 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given |
||
| 225 | * @param int $scope The scope to perform the search. Zend_Ldap::SEARCH_SCOPE_ONE, Zend_LDAP::SEARCH_SCOPE_BASE. Default is Zend_Ldap::SEARCH_SCOPE_SUB |
||
| 226 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | public function getUserByDN($dn, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Return a particular LDAP user by mail value. |
||
| 241 | * |
||
| 242 | * @param string $email |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function getUserByEmail($email, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get a specific user's data from LDAP |
||
| 257 | * |
||
| 258 | * @param string $username |
||
| 259 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given |
||
| 260 | * @param int $scope The scope to perform the search. Zend_Ldap::SEARCH_SCOPE_ONE, Zend_LDAP::SEARCH_SCOPE_BASE. Default is Zend_Ldap::SEARCH_SCOPE_SUB |
||
| 261 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes |
||
| 262 | * @return array |
||
| 263 | * @throws Exception |
||
| 264 | */ |
||
| 265 | public function getUserByUsername($username, $baseDn = null, $scope = Ldap::SEARCH_SCOPE_SUB, $attributes = []) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get a canonical username from the record based on the connection settings. |
||
| 298 | * |
||
| 299 | * @param array $data |
||
| 300 | * @return string |
||
| 301 | * @throws Exception |
||
| 302 | */ |
||
| 303 | public function getCanonicalUsername($data) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Changes user password via LDAP. |
||
| 332 | * |
||
| 333 | * Change password is different to administrative password reset in that it will respect the password |
||
| 334 | * history policy. This is achieved by sending a remove followed by an add in one batch (which is different |
||
| 335 | * to an ordinary attribute modification operation). |
||
| 336 | * |
||
| 337 | * @param string $dn Location to update the entry at. |
||
| 338 | * @param string $password New password to set. |
||
| 339 | * @param string $oldPassword Old password is needed to trigger a password change. |
||
| 340 | * @throws \Zend\Ldap\Exception\LdapException |
||
| 341 | * @throws Exception |
||
| 342 | */ |
||
| 343 | public function changePassword($dn, $password, $oldPassword) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Administrative password reset. |
||
| 375 | * |
||
| 376 | * This is different to password change - it does not require old password, but also |
||
| 377 | * it does not respect password history policy setting. |
||
| 378 | * |
||
| 379 | * @param string $dn Location to update the entry at. |
||
| 380 | * @param string $password New password to set. |
||
| 381 | * @throws \Zend\Ldap\Exception\LdapException |
||
| 382 | * @throws Exception |
||
| 383 | */ |
||
| 384 | public function resetPassword($dn, $password) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Updates an LDAP object. |
||
| 398 | * |
||
| 399 | * For this work you might need that LDAP connection is bind:ed with a user with |
||
| 400 | * enough permissions to change attributes and that the LDAP connection is using |
||
| 401 | * SSL/TLS. It depends on the server setup. |
||
| 402 | * |
||
| 403 | * If there are some errors, the underlying LDAP library should throw an Exception |
||
| 404 | * |
||
| 405 | * @param string $dn Location to update the entry at. |
||
| 406 | * @param array $attributes An associative array of attributes. |
||
| 407 | * @throws \Zend\Ldap\Exception\LdapException |
||
| 408 | */ |
||
| 409 | public function update($dn, array $attributes) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Deletes an LDAP object. |
||
| 416 | * |
||
| 417 | * @param string $dn Location of object to delete |
||
| 418 | * @param bool $recursively Recursively delete nested objects? |
||
| 419 | * @throws \Zend\Ldap\Exception\LdapException |
||
| 420 | */ |
||
| 421 | public function delete($dn, $recursively = false) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Move an LDAP object from it's original DN location to another. |
||
| 428 | * This effectively copies the object then deletes the original one. |
||
| 429 | * |
||
| 430 | * @param string $fromDn |
||
| 431 | * @param string $toDn |
||
| 432 | * @param bool $recursively Recursively move nested objects? |
||
| 433 | */ |
||
| 434 | public function move($fromDn, $toDn, $recursively = false) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Add an LDAP object. |
||
| 441 | * |
||
| 442 | * For this work you might need that LDAP connection is bind:ed with a user with |
||
| 443 | * enough permissions to change attributes and that the LDAP connection is using |
||
| 444 | * SSL/TLS. It depends on the server setup. |
||
| 445 | * |
||
| 446 | * @param string $dn Location to add the entry at. |
||
| 447 | * @param array $attributes An associative array of attributes. |
||
| 448 | * @throws \Zend\Ldap\Exception\LdapException |
||
| 449 | */ |
||
| 450 | public function add($dn, array $attributes) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | private function getLastPasswordError() |
||
| 483 | } |
||
| 484 |
This check marks private properties in classes that are never used. Those properties can be removed.