Complex classes like LDAP 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 LDAP, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class LDAP implements ILDAPWrapper { |
||
| 36 | protected $curFunc = ''; |
||
| 37 | protected $curArgs = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param resource $link |
||
| 41 | * @param string $dn |
||
| 42 | * @param string $password |
||
| 43 | * @return bool|mixed |
||
| 44 | */ |
||
| 45 | public function bind($link, $dn, $password) { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $host |
||
| 51 | * @param string $port |
||
| 52 | * @return mixed |
||
| 53 | */ |
||
| 54 | public function connect($host, $port) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param LDAP $link |
||
| 67 | * @param LDAP $result |
||
| 68 | * @param string $cookie |
||
| 69 | * @return bool|LDAP |
||
| 70 | */ |
||
| 71 | public function controlPagedResultResponse($link, $result, &$cookie) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param LDAP $link |
||
| 82 | * @param int $pageSize |
||
| 83 | * @param bool $isCritical |
||
| 84 | * @param string $cookie |
||
| 85 | * @return mixed|true |
||
| 86 | */ |
||
| 87 | public function controlPagedResult($link, $pageSize, $isCritical, $cookie) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param LDAP $link |
||
| 94 | * @param LDAP $result |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | public function countEntries($link, $result) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param LDAP $link |
||
| 103 | * @return mixed|string |
||
| 104 | */ |
||
| 105 | public function errno($link) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param LDAP $link |
||
| 111 | * @return int|mixed |
||
| 112 | */ |
||
| 113 | public function error($link) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Splits DN into its component parts |
||
| 119 | * @param string $dn |
||
| 120 | * @param int @withAttrib |
||
| 121 | * @return array|false |
||
| 122 | * @link http://www.php.net/manual/en/function.ldap-explode-dn.php |
||
| 123 | */ |
||
| 124 | public function explodeDN($dn, $withAttrib) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param LDAP $link |
||
| 130 | * @param LDAP $result |
||
| 131 | * @return mixed |
||
| 132 | */ |
||
| 133 | public function firstEntry($link, $result) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param LDAP $link |
||
| 139 | * @param LDAP $result |
||
| 140 | * @return array|mixed |
||
| 141 | */ |
||
| 142 | public function getAttributes($link, $result) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param LDAP $link |
||
| 148 | * @param LDAP $result |
||
| 149 | * @return mixed|string |
||
| 150 | */ |
||
| 151 | public function getDN($link, $result) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param LDAP $link |
||
| 157 | * @param LDAP $result |
||
| 158 | * @return array|mixed |
||
| 159 | */ |
||
| 160 | public function getEntries($link, $result) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param LDAP $link |
||
| 166 | * @param resource $result |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | public function nextEntry($link, $result) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param LDAP $link |
||
| 175 | * @param string $baseDN |
||
| 176 | * @param string $filter |
||
| 177 | * @param array $attr |
||
| 178 | * @return mixed |
||
| 179 | */ |
||
| 180 | public function read($link, $baseDN, $filter, $attr) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param LDAP $link |
||
| 186 | * @param string $baseDN |
||
| 187 | * @param string $filter |
||
| 188 | * @param array $attr |
||
| 189 | * @param int $attrsOnly |
||
| 190 | * @param int $limit |
||
| 191 | * @return mixed |
||
| 192 | */ |
||
| 193 | public function search($link, $baseDN, $filter, $attr, $attrsOnly = 0, $limit = 0) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param LDAP $link |
||
| 199 | * @param string $userDN |
||
| 200 | * @param string $password |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function modReplace($link, $userDN, $password) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param LDAP $link |
||
| 209 | * @param string $option |
||
| 210 | * @param int $value |
||
| 211 | * @return bool|mixed |
||
| 212 | */ |
||
| 213 | public function setOption($link, $option, $value) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param LDAP $link |
||
| 219 | * @return mixed|true |
||
| 220 | */ |
||
| 221 | public function startTls($link) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param resource $link |
||
| 227 | * @return bool|mixed |
||
| 228 | */ |
||
| 229 | public function unbind($link) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Checks whether the server supports LDAP |
||
| 235 | * @return boolean if it the case, false otherwise |
||
| 236 | * */ |
||
| 237 | public function areLDAPFunctionsAvailable() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Checks whether PHP supports LDAP Paged Results |
||
| 243 | * @return boolean if it the case, false otherwise |
||
| 244 | * */ |
||
| 245 | public function hasPagedResultSupport() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Checks whether the submitted parameter is a resource |
||
| 253 | * @param Resource $resource the resource variable to check |
||
| 254 | * @return bool true if it is a resource, false otherwise |
||
| 255 | */ |
||
| 256 | public function isResource($resource) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Checks whether the return value from LDAP is wrong or not. |
||
| 262 | * |
||
| 263 | * When using ldap_search we provide an array, in case multiple bases are |
||
| 264 | * configured. Thus, we need to check the array elements. |
||
| 265 | * |
||
| 266 | * @param $result |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | protected function isResultFalse($result) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return mixed |
||
| 287 | */ |
||
| 288 | protected function invokeLDAPMethod() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $functionName |
||
| 304 | * @param array $args |
||
| 305 | */ |
||
| 306 | private function preFunctionCall($functionName, $args) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Analyzes the returned LDAP error and acts accordingly if not 0 |
||
| 313 | * |
||
| 314 | * @param resource $resource the LDAP Connection resource |
||
| 315 | * @throws ConstraintViolationException |
||
| 316 | * @throws ServerNotAvailableException |
||
| 317 | * @throws \Exception |
||
| 318 | */ |
||
| 319 | private function processLDAPError($resource) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Called after an ldap method is run to act on LDAP error if necessary |
||
| 352 | */ |
||
| 353 | private function postFunctionCall() { |
||
| 373 | } |
||
| 374 |