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 |
||
| 31 | class LDAP implements ILDAPWrapper { |
||
| 32 | protected $curFunc = ''; |
||
| 33 | protected $curArgs = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param resource $link |
||
| 37 | * @param string $dn |
||
| 38 | * @param string $password |
||
| 39 | * @return bool|mixed |
||
| 40 | */ |
||
| 41 | public function bind($link, $dn, $password) { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param string $host |
||
| 47 | * @param string $port |
||
| 48 | * @return mixed |
||
| 49 | */ |
||
| 50 | public function connect($host, $port) { |
||
| 51 | if(strpos($host, '://') === false) { |
||
| 52 | $host = 'ldap://' . $host; |
||
| 53 | } |
||
| 54 | if(strpos($host, ':', strpos($host, '://') + 1) === false) { |
||
| 55 | //ldap_connect ignores port parameter when URLs are passed |
||
| 56 | $host .= ':' . $port; |
||
| 57 | } |
||
| 58 | return $this->invokeLDAPMethod('connect', $host); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param LDAP $link |
||
| 63 | * @param LDAP $result |
||
| 64 | * @param string $cookie |
||
| 65 | * @return bool|LDAP |
||
| 66 | */ |
||
| 67 | public function controlPagedResultResponse($link, $result, &$cookie) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param LDAP $link |
||
| 78 | * @param int $pageSize |
||
| 79 | * @param bool $isCritical |
||
| 80 | * @param string $cookie |
||
| 81 | * @return mixed|true |
||
| 82 | */ |
||
| 83 | public function controlPagedResult($link, $pageSize, $isCritical, $cookie) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param LDAP $link |
||
| 90 | * @param LDAP $result |
||
| 91 | * @return mixed |
||
| 92 | */ |
||
| 93 | public function countEntries($link, $result) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param LDAP $link |
||
| 99 | * @return mixed|string |
||
| 100 | */ |
||
| 101 | public function errno($link) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param LDAP $link |
||
| 107 | * @return int|mixed |
||
| 108 | */ |
||
| 109 | public function error($link) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Splits DN into its component parts |
||
| 115 | * @param string $dn |
||
| 116 | * @param int @withAttrib |
||
| 117 | * @return array|false |
||
| 118 | * @link http://www.php.net/manual/en/function.ldap-explode-dn.php |
||
| 119 | */ |
||
| 120 | 1 | public function explodeDN($dn, $withAttrib) { |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @param LDAP $link |
||
| 126 | * @param LDAP $result |
||
| 127 | * @return mixed |
||
| 128 | */ |
||
| 129 | public function firstEntry($link, $result) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param LDAP $link |
||
| 135 | * @param LDAP $result |
||
| 136 | * @return array|mixed |
||
| 137 | */ |
||
| 138 | public function getAttributes($link, $result) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param LDAP $link |
||
| 144 | * @param LDAP $result |
||
| 145 | * @return mixed|string |
||
| 146 | */ |
||
| 147 | public function getDN($link, $result) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param LDAP $link |
||
| 153 | * @param LDAP $result |
||
| 154 | * @return array|mixed |
||
| 155 | */ |
||
| 156 | public function getEntries($link, $result) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param LDAP $link |
||
| 162 | * @param resource $result |
||
| 163 | * @return mixed|an |
||
| 164 | */ |
||
| 165 | public function nextEntry($link, $result) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param LDAP $link |
||
| 171 | * @param string $baseDN |
||
| 172 | * @param string $filter |
||
| 173 | * @param array $attr |
||
| 174 | * @return mixed |
||
| 175 | */ |
||
| 176 | public function read($link, $baseDN, $filter, $attr) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param LDAP $link |
||
| 182 | * @param string $baseDN |
||
| 183 | * @param string $filter |
||
| 184 | * @param array $attr |
||
| 185 | * @param int $attrsOnly |
||
| 186 | * @param int $limit |
||
| 187 | * @return mixed |
||
| 188 | */ |
||
| 189 | public function search($link, $baseDN, $filter, $attr, $attrsOnly = 0, $limit = 0) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param LDAP $link |
||
| 195 | * @param string $option |
||
| 196 | * @param int $value |
||
| 197 | * @return bool|mixed |
||
| 198 | */ |
||
| 199 | public function setOption($link, $option, $value) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param LDAP $link |
||
| 205 | * @param LDAP $result |
||
| 206 | * @param string $sortFilter |
||
| 207 | * @return mixed |
||
| 208 | */ |
||
| 209 | public function sort($link, $result, $sortFilter) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param LDAP $link |
||
| 215 | * @return mixed|true |
||
| 216 | */ |
||
| 217 | public function startTls($link) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param resource $link |
||
| 223 | * @return bool|mixed |
||
| 224 | */ |
||
| 225 | public function unbind($link) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Checks whether the server supports LDAP |
||
| 231 | * @return boolean if it the case, false otherwise |
||
| 232 | * */ |
||
| 233 | public function areLDAPFunctionsAvailable() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Checks whether PHP supports LDAP Paged Results |
||
| 239 | * @return boolean if it the case, false otherwise |
||
| 240 | * */ |
||
| 241 | public function hasPagedResultSupport() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Checks whether the submitted parameter is a resource |
||
| 249 | * @param Resource $resource the resource variable to check |
||
| 250 | * @return bool true if it is a resource, false otherwise |
||
| 251 | */ |
||
| 252 | 1 | public function isResource($resource) { |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @return mixed |
||
| 258 | */ |
||
| 259 | 1 | private function invokeLDAPMethod() { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $functionName |
||
| 274 | * @param array $args |
||
| 275 | */ |
||
| 276 | 1 | private function preFunctionCall($functionName, $args) { |
|
| 280 | |||
| 281 | 1 | private function postFunctionCall() { |
|
| 314 | } |
||
| 315 |