Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | public function __construct($data) |
||
12 | { |
||
13 | $this->server = \LDAP\LDAPServer::getInstance(); |
||
14 | $this->initialize($data); |
||
15 | } |
||
16 | |||
17 | public function getGroupName() |
||
18 | { |
||
19 | return $this->getFieldSingleValue('cn'); |
||
|
|||
20 | } |
||
21 | |||
22 | public function getDescription() |
||
23 | { |
||
24 | return $this->getFieldSingleValue('description'); |
||
25 | } |
||
26 | |||
27 | public function setDescription($name) |
||
28 | { |
||
29 | return $this->setField('description', $name); |
||
30 | } |
||
31 | |||
32 | private function getMembersField(&$fieldName = false) |
||
33 | { |
||
34 | $rawMembers = $this->getField('member'); |
||
35 | $fieldName = 'member'; |
||
36 | if($rawMembers === false) |
||
37 | { |
||
38 | $rawMembers = $this->getField('uniqueMember'); |
||
39 | $fieldName = 'uniqueMember'; |
||
40 | } |
||
41 | if($rawMembers === false) |
||
42 | { |
||
43 | $rawMembers = $this->getField('memberUid'); |
||
44 | $fieldName = 'memberUid'; |
||
45 | } |
||
46 | if(!isset($rawMembers['count'])) |
||
47 | { |
||
48 | $rawMembers['count'] = count($rawMembers); |
||
49 | } |
||
50 | return $rawMembers; |
||
51 | } |
||
52 | |||
53 | private function getIDFromDN($dn) |
||
54 | { |
||
55 | $split = explode(',', $dn); |
||
56 | if(strncmp('cn=', $split[0], 3) === 0) |
||
57 | { |
||
58 | return substr($split[0], 3); |
||
59 | } |
||
60 | return substr($split[0], 4); |
||
61 | } |
||
62 | |||
63 | public function getMemberUids($recursive = true) |
||
64 | { |
||
65 | $members = array(); |
||
66 | $rawMembers = $this->getMembersField(); |
||
67 | for($i = 0; $i < $rawMembers['count']; $i++) |
||
68 | { |
||
69 | if($recursive && strncmp($rawMembers[$i], 'cn=', 3) === 0) |
||
70 | { |
||
71 | $child = new LDAPGroup($rawMembers[$i]); |
||
72 | if($child !== false) |
||
73 | { |
||
74 | $members = array_merge($members, $child->members()); |
||
75 | } |
||
76 | } |
||
77 | else |
||
78 | { |
||
79 | array_push($members, $rawMembers[$i]); |
||
80 | } |
||
81 | } |
||
82 | $count = count($members); |
||
83 | for($i = 0; $i < $count; $i++) |
||
84 | { |
||
85 | $members[$i] = $this->getIDFromDN($members[$i]); |
||
86 | } |
||
87 | return $members; |
||
88 | } |
||
89 | |||
90 | private function getObjectFromDN($dn) |
||
91 | { |
||
92 | $split = explode(',', $dn); |
||
93 | if(strncmp('cn=', $dn, 3) === 0) |
||
94 | { |
||
95 | if(count($split) === 1) |
||
96 | { |
||
97 | return LDAPGroup::from_name($dn, $this->server); |
||
98 | } |
||
99 | return LDAPGroup::from_name(substr($split[0], 3), $this->server); |
||
100 | } |
||
101 | if(count($split) === 1) |
||
102 | { |
||
103 | return LDAPUser::from_name($dn, $this->server); |
||
104 | } |
||
105 | return LDAPUser::from_name(substr($split[0], 4), $this->server); |
||
106 | } |
||
107 | |||
108 | private function getMemberDetail($members) |
||
109 | { |
||
110 | $details = array(); |
||
111 | $count = count($members); |
||
112 | for($i = 0; $i < $count; $i++) |
||
113 | { |
||
114 | $details[$i] = $this->getObjectFromDN($members[$i]); |
||
115 | } |
||
116 | return $details; |
||
117 | } |
||
118 | |||
119 | public function members($details = false, $recursive = true, $includeGroups = true) |
||
120 | { |
||
121 | $members = array(); |
||
122 | $rawMembers = $this->getMembersField(); |
||
123 | for($i = 0; $i < $rawMembers['count']; $i++) |
||
124 | { |
||
125 | if($recursive && strncmp($rawMembers[$i], 'cn=', 3) === 0) |
||
126 | { |
||
127 | $child = new LDAPGroup($rawMembers[$i]); |
||
128 | if($child !== false) |
||
129 | { |
||
130 | $members = array_merge($members, $child->members()); |
||
131 | } |
||
132 | } |
||
133 | else if($includeGroups !== false || strncmp($rawMembers[$i], 'cn=', 3) !== 0) |
||
134 | { |
||
135 | array_push($members, $rawMembers[$i]); |
||
136 | } |
||
137 | } |
||
138 | if($details === true) |
||
139 | { |
||
140 | $members = $this->getMemberDetail($members); |
||
141 | } |
||
142 | return $members; |
||
143 | } |
||
144 | |||
145 | public function getNonMemebers($select = false) |
||
184 | |||
185 | public function clearMembers() |
||
200 | |||
201 | public function addMember($name, $isGroup = false, $flush = true) |
||
247 | |||
248 | View Code Duplication | public static function from_name($name, $data = false) |
|
249 | { |
||
250 | if($data === false) |
||
251 | { |
||
252 | throw new \Exception('data must be set for LDAPGroup'); |
||
253 | } |
||
254 | $filter = new \Data\Filter("cn eq $name"); |
||
255 | $group = $data->read($data->group_base, $filter); |
||
256 | if($group === false || !isset($group[0])) |
||
257 | { |
||
262 | } |
||
263 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
264 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.