Complex classes like LDAPAuthenticator 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 LDAPAuthenticator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 67 | class LDAPAuthenticator extends Authenticator |
||
| 68 | { |
||
| 69 | /** The URL for the LDAP host */ |
||
| 70 | private $host; |
||
| 71 | /** The base DN for all users in the LDAP server */ |
||
| 72 | public $user_base; |
||
| 73 | /** The base DN for all groups in the LDAP server */ |
||
| 74 | public $group_base; |
||
| 75 | /** The DN to use to bind if not binding as the user */ |
||
| 76 | private $bindDistinguishedName; |
||
| 77 | /** The password to use to bind if not binding as the user */ |
||
| 78 | private $bindPassword; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Create an LDAP Authenticator |
||
| 82 | * |
||
| 83 | * @param array $params Parementers needed to initialize the authenticator |
||
| 84 | */ |
||
| 85 | public function __construct($params) |
||
| 86 | { |
||
| 87 | parent::__construct($params); |
||
| 88 | $this->host = $this->getHostParam($params); |
||
| 89 | $this->user_base = $this->getParam($params, 'user_base'); |
||
| 90 | $this->group_base = $this->getParam($params, 'group_base'); |
||
| 91 | $this->bindDistinguishedName = $this->getParam($params, 'bind_dn', '$ldap_auth', 'read_write_pass'); |
||
| 92 | $this->bindPassword = $this->getParam($params, 'bind_pass', '$ldap_auth', 'read_write_user'); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Return the host string for this authenticator |
||
| 97 | * |
||
| 98 | * @param array $params The initial parameters of the authenticator |
||
| 99 | * |
||
| 100 | * @return string The host string |
||
| 101 | * |
||
| 102 | * @SuppressWarnings("StaticAccess") |
||
| 103 | */ |
||
| 104 | private function getHostParam($params) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Return the required paramter for this authenticator |
||
| 116 | * |
||
| 117 | * @param array $params The initial parameters of the authenticator |
||
| 118 | * @param string $paramName The name of the parameter in the $paramsArray |
||
| 119 | * @param string $settingsLocation The location in the Settings class |
||
| 120 | * @param string $settingsName The name in the Settings class |
||
| 121 | * |
||
| 122 | * @return mixed The paramter value |
||
| 123 | * |
||
| 124 | * @SuppressWarnings("StaticAccess") |
||
| 125 | */ |
||
| 126 | private function getParam($params, $paramName, $settingsLocation = '$ldap', $settingsName = false) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Return an instance to the \LDAP\LDAPServer object instance |
||
| 142 | * |
||
| 143 | * @param boolean $bind_write Should we be able to write to the server? |
||
|
|
|||
| 144 | * |
||
| 145 | * @return \LDAP\LDAPServer|false The server instance if the binding was successful, otherwise false |
||
| 146 | * |
||
| 147 | * @SuppressWarnings("StaticAccess") |
||
| 148 | */ |
||
| 149 | public function getAndBindServer($bindWrite = false) |
||
| 150 | { |
||
| 151 | $server = \LDAP\LDAPServer::getInstance(); |
||
| 152 | $server->user_base = $this->user_base; |
||
| 153 | $server->group_base = $this->group_base; |
||
| 154 | $server->connect($this->host); |
||
| 155 | if($bindWrite === false) |
||
| 156 | { |
||
| 157 | $ret = $server->bind(); |
||
| 158 | } |
||
| 159 | else |
||
| 160 | { |
||
| 161 | $ret = $server->bind($this->bindDistinguishedName, $this->bindPassword); |
||
| 162 | } |
||
| 163 | if($ret === false) |
||
| 164 | { |
||
| 165 | return false; |
||
| 166 | } |
||
| 167 | return $server; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Log the user in provided the credetials |
||
| 172 | * |
||
| 173 | * @param string $username The UID or email address for the user |
||
| 174 | * @param string $password The password for the user |
||
| 175 | * |
||
| 176 | * @return mixed False if the login failed and array otherwise |
||
| 177 | */ |
||
| 178 | public function login($username, $password) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Does this array represent a successful login? |
||
| 203 | * |
||
| 204 | * @param array $data The array data stored in the session after a login call |
||
| 205 | * |
||
| 206 | * @return boolean True if the user is logged in, false otherwise |
||
| 207 | */ |
||
| 208 | public function isLoggedIn($data) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Obtain the currently logged in user from the session data |
||
| 219 | * |
||
| 220 | * @param \stdClass $data The AuthData from the session |
||
| 221 | * |
||
| 222 | * @return \Auth\LDAPUser The LDAPUser represented by this data |
||
| 223 | */ |
||
| 224 | public function getUser($data) |
||
| 228 | |||
| 229 | public function getGroupByName($name) |
||
| 238 | |||
| 239 | public function getGroupsByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false) |
||
| 268 | |||
| 269 | public function getActiveUserCount() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param array $data The array data to filter and sort |
||
| 281 | * @param boolean|array $select The fields to return |
||
| 282 | * @param boolean|integer $top The number of records to return |
||
| 283 | * @param boolean|integer $skip The number of records to skip |
||
| 284 | * @param boolean|array $orderby The fields to sort by |
||
| 285 | */ |
||
| 286 | private function processFilteringParams(&$data, &$select, $top, $skip, $orderby) |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * @param boolean|\Data\Filter $filter The filter to user when reading users |
||
| 313 | * @param boolean|array $select The fields to return |
||
| 314 | * @param boolean|integer $top The number of records to return |
||
| 315 | * @param boolean|integer $skip The number of records to skip |
||
| 316 | * @param boolean|array $orderby The fields to sort by |
||
| 317 | */ |
||
| 318 | public function getUsersByFilter($filter, $select = false, $top = false, $skip = false, $orderby = false) |
||
| 348 | |||
| 349 | public function activatePendingUser($user) |
||
| 350 | { |
||
| 386 | |||
| 387 | public function getUserByResetHash($hash) |
||
| 396 | } |
||
| 397 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 398 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.