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  | 
            ||
| 8 | class LDAPGateway extends Object  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 9 | { | 
            ||
| 10 | /**  | 
            ||
| 11 | * @var array  | 
            ||
| 12 | * @config  | 
            ||
| 13 | */  | 
            ||
| 14 | private static $options = array();  | 
            ||
| 15 | |||
| 16 | /**  | 
            ||
| 17 | * @var Zend\Ldap\Ldap  | 
            ||
| 18 | */  | 
            ||
| 19 | private $ldap;  | 
            ||
| 20 | |||
| 21 | public function __construct()  | 
            ||
| 30 | |||
| 31 | /**  | 
            ||
| 32 | * Query the LDAP directory with the given filter.  | 
            ||
| 33 | *  | 
            ||
| 34 | * @param string $filter The string to filter by, e.g. (objectClass=user)  | 
            ||
| 35 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given  | 
            ||
| 36 | * @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  | 
            ||
| 37 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes  | 
            ||
| 38 | * @param string $sort Sort results by this attribute if given  | 
            ||
| 39 | * @return array  | 
            ||
| 40 | */  | 
            ||
| 41 | protected function search($filter, $baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array(), $sort = '')  | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * Authenticate the given username and password with LDAP.  | 
            ||
| 74 | *  | 
            ||
| 75 | * @param string $username  | 
            ||
| 76 | * @param string $password  | 
            ||
| 77 | * @return \Zend\Authentication\Result  | 
            ||
| 78 | */  | 
            ||
| 79 | public function authenticate($username, $password)  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * Query for LDAP nodes (organizational units, containers, and domains).  | 
            ||
| 88 | *  | 
            ||
| 89 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given  | 
            ||
| 90 | * @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  | 
            ||
| 91 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes  | 
            ||
| 92 | * @param string $sort Sort results by this attribute if given  | 
            ||
| 93 | * @return array  | 
            ||
| 94 | */  | 
            ||
| 95 | public function getNodes($baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array(), $sort = '')  | 
            ||
| 99 | |||
| 100 | /**  | 
            ||
| 101 | * Query for LDAP groups.  | 
            ||
| 102 | *  | 
            ||
| 103 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given  | 
            ||
| 104 | * @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  | 
            ||
| 105 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes  | 
            ||
| 106 | * @param string $sort Sort results by this attribute if given  | 
            ||
| 107 | * @return array  | 
            ||
| 108 | */  | 
            ||
| 109 | public function getGroups($baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array(), $sort = '')  | 
            ||
| 113 | |||
| 114 | /**  | 
            ||
| 115 | * Return all nested AD groups underneath a specific DN  | 
            ||
| 116 | *  | 
            ||
| 117 | * @param string $dn  | 
            ||
| 118 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given  | 
            ||
| 119 | * @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  | 
            ||
| 120 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes  | 
            ||
| 121 | * @return array  | 
            ||
| 122 | */  | 
            ||
| 123 | public function getNestedGroups($dn, $baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array())  | 
            ||
| 132 | |||
| 133 | /**  | 
            ||
| 134 | * Return a particular LDAP group by objectGUID value.  | 
            ||
| 135 | *  | 
            ||
| 136 | * @param string $guid  | 
            ||
| 137 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given  | 
            ||
| 138 | * @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  | 
            ||
| 139 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes  | 
            ||
| 140 | * @return array  | 
            ||
| 141 | */  | 
            ||
| 142 | View Code Duplication | public function getGroupByGUID($guid, $baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array())  | 
            |
| 151 | |||
| 152 | /**  | 
            ||
| 153 | * Return a particular LDAP group by DN value.  | 
            ||
| 154 | *  | 
            ||
| 155 | * @param string $dn  | 
            ||
| 156 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given  | 
            ||
| 157 | * @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  | 
            ||
| 158 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes  | 
            ||
| 159 | * @return array  | 
            ||
| 160 | */  | 
            ||
| 161 | public function getGroupByDN($dn, $baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array())  | 
            ||
| 170 | |||
| 171 | /**  | 
            ||
| 172 | * Query for LDAP users, but don't include built-in user accounts.  | 
            ||
| 173 | *  | 
            ||
| 174 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given  | 
            ||
| 175 | * @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  | 
            ||
| 176 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes  | 
            ||
| 177 | * @param string $sort Sort results by this attribute if given  | 
            ||
| 178 | * @return array  | 
            ||
| 179 | */  | 
            ||
| 180 | public function getUsers($baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array(), $sort = '')  | 
            ||
| 190 | |||
| 191 | /**  | 
            ||
| 192 | * Return a particular LDAP user by objectGUID value.  | 
            ||
| 193 | *  | 
            ||
| 194 | * @param string $guid  | 
            ||
| 195 | * @return array  | 
            ||
| 196 | */  | 
            ||
| 197 | View Code Duplication | public function getUserByGUID($guid, $baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array())  | 
            |
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * Return a particular LDAP user by DN value.  | 
            ||
| 209 | *  | 
            ||
| 210 | * @param string $dn  | 
            ||
| 211 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given  | 
            ||
| 212 | * @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  | 
            ||
| 213 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes  | 
            ||
| 214 | * @return array  | 
            ||
| 215 | */  | 
            ||
| 216 | public function getUserByDN($dn, $baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array())  | 
            ||
| 225 | |||
| 226 | /**  | 
            ||
| 227 | * Return a particular LDAP user by mail value.  | 
            ||
| 228 | *  | 
            ||
| 229 | * @param string $email  | 
            ||
| 230 | * @return array  | 
            ||
| 231 | */  | 
            ||
| 232 | public function getUserByEmail($email, $baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array())  | 
            ||
| 241 | |||
| 242 | /**  | 
            ||
| 243 | * Get a specific user's data from LDAP  | 
            ||
| 244 | *  | 
            ||
| 245 | * @param string $username  | 
            ||
| 246 | * @param null|string $baseDn The DN to search from. Default is the baseDn option in the connection if not given  | 
            ||
| 247 | * @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  | 
            ||
| 248 | * @param array $attributes Restrict to specific AD attributes. An empty array will return all attributes  | 
            ||
| 249 | * @return array  | 
            ||
| 250 | * @throws Exception  | 
            ||
| 251 | */  | 
            ||
| 252 | public function getUserByUsername($username, $baseDn = null, $scope = Zend\Ldap\Ldap::SEARCH_SCOPE_SUB, $attributes = array())  | 
            ||
| 281 | |||
| 282 | /**  | 
            ||
| 283 | * Get a canonical username from the record based on the connection settings.  | 
            ||
| 284 | *  | 
            ||
| 285 | * @param array $data  | 
            ||
| 286 | * @return string  | 
            ||
| 287 | */  | 
            ||
| 288 | public function getCanonicalUsername($data)  | 
            ||
| 314 | |||
| 315 | /**  | 
            ||
| 316 | * Changes user password via LDAP.  | 
            ||
| 317 | *  | 
            ||
| 318 | * Change password is different to administrative password reset in that it will respect the password  | 
            ||
| 319 | * history policy. This is achieved by sending a remove followed by an add in one batch (which is different  | 
            ||
| 320 | * to an ordinary attribute modification operation).  | 
            ||
| 321 | *  | 
            ||
| 322 | * @param string $dn Location to update the entry at.  | 
            ||
| 323 | * @param string $password New password to set.  | 
            ||
| 324 | * @param string $oldPassword Old password is needed to trigger a password change.  | 
            ||
| 325 | * @throws \Zend\Ldap\Exception\LdapException  | 
            ||
| 326 | */  | 
            ||
| 327 | public function changePassword($dn, $password, $oldPassword)  | 
            ||
| 356 | |||
| 357 | /**  | 
            ||
| 358 | * Administrative password reset.  | 
            ||
| 359 | *  | 
            ||
| 360 | * This is different to password change - it does not require old password, but also  | 
            ||
| 361 | * it does not respect password history policy setting.  | 
            ||
| 362 | *  | 
            ||
| 363 | * @param string $dn Location to update the entry at.  | 
            ||
| 364 | * @param string $password New password to set.  | 
            ||
| 365 | * @throws \Zend\Ldap\Exception\LdapException  | 
            ||
| 366 | */  | 
            ||
| 367 | public function resetPassword($dn, $password)  | 
            ||
| 378 | |||
| 379 | /**  | 
            ||
| 380 | * Updates an LDAP object.  | 
            ||
| 381 | *  | 
            ||
| 382 | * For this work you might need that LDAP connection is bind:ed with a user with  | 
            ||
| 383 | * enough permissions to change attributes and that the LDAP connection is using  | 
            ||
| 384 | * SSL/TLS. It depends on the server setup.  | 
            ||
| 385 | *  | 
            ||
| 386 | * If there are some errors, the underlying LDAP library should throw an Exception  | 
            ||
| 387 | *  | 
            ||
| 388 | * @param string $dn Location to update the entry at.  | 
            ||
| 389 | * @param array $attributes An associative array of attributes.  | 
            ||
| 390 | * @throws \Zend\Ldap\Exception\LdapException  | 
            ||
| 391 | */  | 
            ||
| 392 | public function update($dn, array $attributes)  | 
            ||
| 396 | |||
| 397 | /**  | 
            ||
| 398 | * Deletes an LDAP object.  | 
            ||
| 399 | *  | 
            ||
| 400 | * @param string $dn Location of object to delete  | 
            ||
| 401 | * @param bool $recursively Recursively delete nested objects?  | 
            ||
| 402 | * @throws \Zend\Ldap\Exception\LdapException  | 
            ||
| 403 | */  | 
            ||
| 404 | public function delete($dn, $recursively = false)  | 
            ||
| 408 | |||
| 409 | /**  | 
            ||
| 410 | * Move an LDAP object from it's original DN location to another.  | 
            ||
| 411 | * This effectively copies the object then deletes the original one.  | 
            ||
| 412 | *  | 
            ||
| 413 | * @param string $fromDn  | 
            ||
| 414 | * @param string $toDn  | 
            ||
| 415 | * @param bool $recursively Recursively move nested objects?  | 
            ||
| 416 | */  | 
            ||
| 417 | public function move($fromDn, $toDn, $recursively = false)  | 
            ||
| 421 | |||
| 422 | /**  | 
            ||
| 423 | * Add an LDAP object.  | 
            ||
| 424 | *  | 
            ||
| 425 | * For this work you might need that LDAP connection is bind:ed with a user with  | 
            ||
| 426 | * enough permissions to change attributes and that the LDAP connection is using  | 
            ||
| 427 | * SSL/TLS. It depends on the server setup.  | 
            ||
| 428 | *  | 
            ||
| 429 | * @param string $dn Location to add the entry at.  | 
            ||
| 430 | * @param array $attributes An associative array of attributes.  | 
            ||
| 431 | * @throws \Zend\Ldap\Exception\LdapException  | 
            ||
| 432 | */  | 
            ||
| 433 | public function add($dn, array $attributes)  | 
            ||
| 437 | |||
| 438 | /**  | 
            ||
| 439 | * @param \Zend\Ldap\Exception\LdapException  | 
            ||
| 440 | * @return string  | 
            ||
| 441 | */  | 
            ||
| 442 |     private function getLastPasswordError() { | 
            ||
| 463 | }  | 
            ||
| 464 | 
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.