Complex classes like LDAPGroup 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 LDAPGroup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class LDAPGroup extends Group |
||
| 5 | { |
||
| 6 | use LDAPCachableObject; |
||
| 7 | |||
| 8 | private $ldapObj; |
||
| 9 | private $server; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Initialize a LDAPGroup object |
||
| 13 | * |
||
| 14 | * @SuppressWarnings("StaticAccess") |
||
| 15 | */ |
||
| 16 | public function __construct($data) |
||
| 17 | { |
||
| 18 | $this->server = \LDAP\LDAPServer::getInstance(); |
||
| 19 | $this->initialize($data); |
||
| 20 | } |
||
| 21 | |||
| 22 | public function getGroupName() |
||
| 26 | |||
| 27 | public function getDescription() |
||
| 28 | { |
||
| 29 | return $this->getFieldSingleValue('description'); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function setDescription($name) |
||
| 33 | { |
||
| 34 | return $this->setField('description', $name); |
||
| 35 | } |
||
| 36 | |||
| 37 | private function getMembersField(&$fieldName = false) |
||
| 38 | { |
||
| 39 | $rawMembers = $this->getField('member'); |
||
| 40 | $fieldName = 'member'; |
||
| 41 | if($rawMembers === false) |
||
| 42 | { |
||
| 43 | $rawMembers = $this->getField('uniqueMember'); |
||
| 44 | $fieldName = 'uniqueMember'; |
||
| 45 | } |
||
| 46 | if($rawMembers === false) |
||
| 47 | { |
||
| 48 | $rawMembers = $this->getField('memberUid'); |
||
| 49 | $fieldName = 'memberUid'; |
||
| 50 | } |
||
| 51 | if(!isset($rawMembers['count'])) |
||
| 52 | { |
||
| 53 | $rawMembers['count'] = count($rawMembers); |
||
| 54 | } |
||
| 55 | return $rawMembers; |
||
| 56 | } |
||
| 57 | |||
| 58 | private function getIDFromDN($distinguishedName) |
||
| 59 | { |
||
| 60 | $split = explode(',', $distinguishedName); |
||
| 61 | if(strncmp('cn=', $split[0], 3) === 0) |
||
| 62 | { |
||
| 63 | return substr($split[0], 3); |
||
| 64 | } |
||
| 65 | return substr($split[0], 4); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getMemberUids($recursive = true) |
||
| 69 | { |
||
| 70 | $members = array(); |
||
| 71 | $rawMembers = $this->getMembersField(); |
||
| 72 | for($i = 0; $i < $rawMembers['count']; $i++) |
||
| 73 | { |
||
| 74 | if($recursive && strncmp($rawMembers[$i], 'cn=', 3) === 0) |
||
| 75 | { |
||
| 76 | $child = new LDAPGroup($rawMembers[$i]); |
||
| 77 | if($child !== false) |
||
| 78 | { |
||
| 79 | $members = array_merge($members, $child->members()); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | else |
||
| 83 | { |
||
| 84 | array_push($members, $rawMembers[$i]); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | $count = count($members); |
||
| 88 | for($i = 0; $i < $count; $i++) |
||
| 89 | { |
||
| 90 | $members[$i] = $this->getIDFromDN($members[$i]); |
||
| 91 | } |
||
| 92 | return $members; |
||
| 93 | } |
||
| 94 | |||
| 95 | private function getObjectFromDN($distinguishedName) |
||
| 96 | { |
||
| 97 | $split = explode(',', $distinguishedName); |
||
| 98 | if(strncmp('cn=', $distinguishedName, 3) === 0) |
||
| 99 | { |
||
| 100 | if(count($split) === 1) |
||
| 101 | { |
||
| 102 | return LDAPGroup::from_name($distinguishedName, $this->server); |
||
| 103 | } |
||
| 104 | return LDAPGroup::from_name(substr($split[0], 3), $this->server); |
||
| 105 | } |
||
| 106 | if(count($split) === 1) |
||
| 107 | { |
||
| 108 | return LDAPUser::from_name($distinguishedName, $this->server); |
||
| 109 | } |
||
| 110 | return LDAPUser::from_name(substr($split[0], 4), $this->server); |
||
| 111 | } |
||
| 112 | |||
| 113 | private function getMemberDetail($members) |
||
| 123 | |||
| 124 | public function members($details = false, $recursive = true, $includeGroups = true) |
||
| 125 | { |
||
| 126 | $members = array(); |
||
| 127 | $rawMembers = $this->getMembersField(); |
||
| 128 | for($i = 0; $i < $rawMembers['count']; $i++) |
||
| 129 | { |
||
| 130 | if($recursive && strncmp($rawMembers[$i], 'cn=', 3) === 0) |
||
| 149 | |||
| 150 | public function getNonMembers($select = false) |
||
| 151 | { |
||
| 152 | $data = array(); |
||
| 153 | $groupFilter = '(&(cn=*)(!(cn='.$this->getGroupName().'))'; |
||
| 154 | $userFilter = '(&(cn=*)'; |
||
| 155 | $members = $this->members(); |
||
| 156 | $count = count($members); |
||
| 157 | for($i = 0; $i < $count; $i++) |
||
| 158 | { |
||
| 159 | $dnComps = explode(',', $members[$i]); |
||
| 160 | if(strncmp($members[$i], "uid=", 4) == 0) |
||
| 161 | { |
||
| 162 | $userFilter .= '(!('.$dnComps[0].'))'; |
||
| 163 | } |
||
| 164 | else |
||
| 165 | { |
||
| 166 | $groupFilter .= '(!('.$dnComps[0].'))'; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | $userFilter .= ')'; |
||
| 170 | $groupFilter .= ')'; |
||
| 171 | $groups = $this->server->read($this->server->group_base, $groupFilter); |
||
| 172 | $count = count($groups); |
||
| 173 | for($i = 0; $i < $count; $i++) |
||
| 174 | { |
||
| 175 | if($groups[$i] === false || $groups[$i] === null) |
||
| 176 | { |
||
| 177 | continue; |
||
| 178 | } |
||
| 179 | array_push($data, new LDAPGroup($groups[$i])); |
||
| 180 | } |
||
| 181 | $users = $this->server->read($this->server->user_base, $userFilter, false, $select); |
||
| 182 | $count = count($users); |
||
| 183 | for($i = 0; $i < $count; $i++) |
||
| 184 | { |
||
| 185 | array_push($data, new LDAPUser($users[$i])); |
||
| 186 | } |
||
| 187 | return $data; |
||
| 188 | } |
||
| 189 | |||
| 190 | public function clearMembers() |
||
| 205 | |||
| 206 | public function addMember($name, $isGroup = false, $flush = true) |
||
| 207 | { |
||
| 208 | $distinguishedName = false; |
||
|
|
|||
| 209 | if($isGroup) |
||
| 210 | { |
||
| 211 | $distinguishedName = 'cn='.$name.','.$this->server->group_base; |
||
| 212 | } |
||
| 213 | else |
||
| 214 | { |
||
| 215 | $distinguishedName = 'uid='.$name.','.$this->server->user_base; |
||
| 216 | } |
||
| 217 | $propName = false; |
||
| 218 | $rawMembers = $this->getMembersField($propName); |
||
| 219 | if(isset($rawMembers['count'])) |
||
| 220 | { |
||
| 221 | unset($rawMembers['count']); |
||
| 222 | } |
||
| 223 | if(in_array($distinguishedName, $rawMembers) || in_array($name, $rawMembers)) |
||
| 224 | { |
||
| 225 | return true; |
||
| 226 | } |
||
| 227 | if($propName === 'memberUid') |
||
| 228 | { |
||
| 229 | if($isGroup) |
||
| 230 | { |
||
| 231 | throw new \Exception('Unable to add a group as a child of this group type'); |
||
| 232 | } |
||
| 233 | array_push($rawMembers, $name); |
||
| 234 | } |
||
| 235 | else |
||
| 236 | { |
||
| 237 | array_push($rawMembers, $distinguishedName); |
||
| 238 | } |
||
| 239 | $tmp = strtolower($propName); |
||
| 240 | $this->ldapObj->$tmp = $rawMembers; |
||
| 241 | if($flush === true) |
||
| 242 | { |
||
| 243 | $obj = array('dn'=>$this->ldapObj->dn); |
||
| 244 | $obj[$propName] = $rawMembers; |
||
| 245 | return $this->server->update($obj); |
||
| 246 | } |
||
| 247 | else |
||
| 248 | { |
||
| 249 | return true; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $name The Group Name |
||
| 255 | * @param boolean|\LDAP\LDAPServer $data The server to read from |
||
| 256 | */ |
||
| 257 | public static function from_name($name, $data = false) |
||
| 271 | } |
||
| 272 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 273 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.