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 LDAPUser 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 LDAPUser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class LDAPUser extends User |
||
5 | { |
||
6 | use LDAPCachableObject; |
||
7 | use LDAPGettableObject; |
||
8 | use LDAPSettableObject; |
||
9 | |||
10 | private $ldapObj; |
||
11 | private $server; |
||
12 | |||
13 | /** |
||
14 | * Initialize a LDAPUser object |
||
15 | * |
||
16 | * @param boolean|array|object $data The data to initialize the LDAPUser with or false for an empty User |
||
17 | * |
||
18 | * @SuppressWarnings("StaticAccess") |
||
19 | */ |
||
20 | public function __construct($data = false) |
||
21 | { |
||
22 | $this->server = \LDAP\LDAPServer::getInstance(); |
||
23 | $this->initialize($data); |
||
24 | } |
||
25 | |||
26 | private function checkChildGroup($array) |
||
27 | { |
||
28 | $res = false; |
||
29 | for($i = 0; $i < $array['count']; $i++) |
||
30 | { |
||
31 | if(strpos($array[$i], $this->server->group_base) !== false) |
||
32 | { |
||
33 | $dn = explode(',', $array[$i]); |
||
34 | $res = $this->isInGroupNamed(substr($dn[0], 3)); |
||
35 | if($res) |
||
36 | { |
||
37 | return $res; |
||
38 | } |
||
39 | } |
||
40 | } |
||
41 | return $res; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param string $listName The name of the list to search |
||
46 | * @param Group $group The group to search inside |
||
47 | * @param string $dn The distringuished name to search for |
||
48 | */ |
||
49 | private function isInListOrChild($listName, $group, $dn) |
||
61 | |||
62 | private function uidInMemberUid($group, $uid) |
||
63 | { |
||
66 | |||
67 | public function isInGroupNamed($name) |
||
97 | |||
98 | protected $valueDefaults = array( |
||
99 | 'o' => 'Volunteer' |
||
100 | ); |
||
101 | |||
102 | protected $multiValueProps = array( |
||
103 | 'title', |
||
104 | 'ou', |
||
105 | 'host' |
||
106 | ); |
||
107 | |||
108 | protected $cachedOnlyProps = array( |
||
109 | 'uid' |
||
110 | ); |
||
111 | |||
112 | /** |
||
113 | * LDAP does not allow anonymous read |
||
114 | * |
||
115 | * @SuppressWarnings("StaticAccess") |
||
116 | */ |
||
117 | View Code Duplication | protected function enableRead() |
|
127 | |||
128 | /** |
||
129 | * Allow write for the user |
||
130 | * |
||
131 | * @SuppressWarnings("StaticAccess") |
||
132 | */ |
||
133 | View Code Duplication | protected function enableReadWrite() |
|
143 | |||
144 | public function getGroups() |
||
163 | |||
164 | public function addLoginProvider($provider) |
||
168 | |||
169 | private function generateLDAPPass($pass) |
||
176 | |||
177 | public function setPass($password) |
||
194 | |||
195 | public function validate_password($password) |
||
199 | |||
200 | public function validate_reset_hash($hash) |
||
208 | |||
209 | public static function from_name($name, $data) |
||
223 | |||
224 | public function flushUser() |
||
247 | |||
248 | private function getHashFromUser($ldapObj) |
||
256 | |||
257 | public function getPasswordResetHash() |
||
272 | |||
273 | public function delete() |
||
279 | } |
||
280 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
281 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: