Complex classes like LDAPServer 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 LDAPServer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 62 | class LDAPServer extends \Singleton |
||
| 63 | { |
||
| 64 | protected $ldapLink; |
||
| 65 | protected $connect; |
||
| 66 | protected $binder; |
||
| 67 | public $user_base; |
||
| 68 | public $group_base; |
||
| 69 | |||
| 70 | protected function __construct() |
||
| 75 | |||
| 76 | public function __destruct() |
||
| 79 | |||
| 80 | public function __wakeup() |
||
| 84 | |||
| 85 | private function getConnectString($name, $proto = false) |
||
| 97 | |||
| 98 | public function connect($name, $proto = false) |
||
| 115 | |||
| 116 | public function disconnect() |
||
| 117 | { |
||
| 118 | if($this->ldapLink !== null) |
||
| 119 | { |
||
| 120 | ldap_close($this->ldapLink); |
||
| 121 | $this->ldapLink = null; |
||
| 122 | } |
||
| 123 | $this->connect = false; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Bind (login0 to the LDAP Server |
||
| 128 | * |
||
| 129 | * @param string $commonName The common name to bind with, null to bind anonymously |
||
| 130 | * @param string $password The password to bind with, null to bind anonymously |
||
| 131 | */ |
||
| 132 | public function bind($commonName = null, $password = null) |
||
| 133 | { |
||
| 134 | $res = false; |
||
| 135 | if($this->ldapLink === null) |
||
| 136 | { |
||
| 137 | throw new \Exception('Not connected'); |
||
| 138 | } |
||
| 139 | $this->binder = $commonName; |
||
| 140 | if($commonName === null || $password === null) |
||
| 141 | { |
||
| 142 | return @ldap_bind($this->ldapLink); |
||
| 143 | } |
||
| 144 | try |
||
| 145 | { |
||
| 146 | $res = ldap_bind($this->ldapLink, $commonName, $password); |
||
| 147 | } |
||
| 148 | catch(\Exception $ex) |
||
| 149 | { |
||
| 150 | $this->ldapLink = ldap_connect($this->connect); |
||
| 151 | $res = @ldap_bind($this->ldapLink, $commonName, $password); |
||
| 152 | } |
||
| 153 | return $res; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function unbind() |
||
| 164 | |||
| 165 | private function fixChildArray(&$array, $key, &$entity) |
||
| 166 | { |
||
| 167 | $count = count($array); |
||
| 168 | for($i = 0; $i < $count; $i++) |
||
| 169 | { |
||
| 170 | if(isset($array[$i])) |
||
| 171 | { |
||
| 172 | $entity[$key][$i] = $array[$i]; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | private function fixObject($object, &$delete = false) |
||
| 178 | { |
||
| 179 | $entity = $object; |
||
| 180 | if(!is_array($object)) |
||
| 181 | { |
||
| 182 | $entity = $object->to_array(); |
||
| 183 | } |
||
| 184 | unset($entity['dn']); |
||
| 185 | $keys = array_keys($entity); |
||
| 186 | $count = count($keys); |
||
| 187 | for($i = 0; $i < $count; $i++) |
||
| 188 | { |
||
| 189 | if(is_array($entity[$keys[$i]])) |
||
| 190 | { |
||
| 191 | $this->fixChildArray($entity[$keys[$i]], $keys[$i], $entity); |
||
| 192 | //unset($entity[$keys[$i]]); |
||
| 193 | } |
||
| 194 | else if($delete !== false && $entity[$keys[$i]] === null) |
||
| 195 | { |
||
| 196 | $delete[$keys[$i]] = array(); |
||
| 197 | unset($entity[$keys[$i]]); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | return $entity; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function create($object) |
||
| 204 | { |
||
| 205 | $distinguishedName = ldap_escape($object['dn'], true); |
||
| 206 | $entity = $this->fixObject($object); |
||
| 207 | $ret = ldap_add($this->ldapLink, $distinguishedName, $entity); |
||
| 208 | if($ret === false) |
||
| 209 | { |
||
| 210 | throw new \Exception('Failed to create object with dn='.$distinguishedName); |
||
| 211 | } |
||
| 212 | return $ret; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get the LDAP filter represented by the passed object |
||
| 217 | * |
||
| 218 | * @param boolean|string|\Data\Filter $filter The fiter to use |
||
| 219 | * |
||
| 220 | * @return string The filter in LDAP format |
||
| 221 | */ |
||
| 222 | private function filterToString($filter) |
||
| 223 | { |
||
| 224 | if($filter === false) |
||
| 225 | { |
||
| 226 | return '(objectclass=*)'; |
||
| 227 | } |
||
| 228 | if(is_string($filter)) |
||
| 229 | { |
||
| 230 | return $filter; |
||
| 231 | } |
||
| 232 | return $filter->to_ldap_string(); |
||
| 233 | } |
||
| 234 | |||
| 235 | private function searchResultToArray($searchResult) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get data from the LDAP Server |
||
| 256 | * |
||
| 257 | * @param string $baseDN The distinguished name to start the search from |
||
| 258 | * @param boolean|string|\Data\Filter $filter The fiter to use |
||
| 259 | * @param boolean $single Read only the base DN |
||
| 260 | * @param boolean|array $attributes The list of attributes to read |
||
| 261 | * |
||
| 262 | * @return boolean|array The results from the LDAP Server |
||
| 263 | */ |
||
| 264 | public function read($baseDN, $filter = false, $single = false, $attributes = false) |
||
| 291 | |||
| 292 | public function count($baseDN, $filter = false) |
||
| 313 | |||
| 314 | public function update($object) |
||
| 315 | { |
||
| 316 | $distinguishedName = ldap_escape($object['dn'], true); |
||
| 338 | |||
| 339 | public function delete($distinguishedName) |
||
| 343 | } |
||
| 344 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 345 |
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.