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 Ldaph 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 Ldaph, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Comodojo\Ldaph; |
||
24 | class Ldaph { |
||
25 | |||
26 | /** |
||
27 | * Ldap handler |
||
28 | * |
||
29 | * @var resource |
||
30 | */ |
||
31 | private $ldaph = false; |
||
32 | |||
33 | /** |
||
34 | * Ldap version |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | private $version = 3; |
||
39 | |||
40 | /** |
||
41 | * ldaps support |
||
42 | * |
||
43 | * @var bool |
||
44 | */ |
||
45 | private $ssl = false; |
||
46 | |||
47 | /** |
||
48 | * Use tls |
||
49 | * |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $tls = false; |
||
53 | |||
54 | /** |
||
55 | * SSO support |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | private $sso = false; |
||
60 | |||
61 | /** |
||
62 | * DC(s) |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $dc = ''; |
||
67 | |||
68 | /** |
||
69 | * DN |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | private $dn = 'USER_NAME'; |
||
74 | |||
75 | /** |
||
76 | * The base to search in |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | private $searchbase = null; |
||
81 | |||
82 | /** |
||
83 | * Ldap username |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | private $user = null; |
||
88 | |||
89 | /** |
||
90 | * Ldap user password |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | private $pass = null; |
||
95 | |||
96 | /** |
||
97 | * Ldap server port |
||
98 | * |
||
99 | * @var int |
||
100 | */ |
||
101 | private $port = 389; |
||
102 | |||
103 | /** |
||
104 | * Ldap server |
||
105 | * |
||
106 | * @var string |
||
107 | */ |
||
108 | private $server = null; |
||
109 | |||
110 | /** |
||
111 | * Fields to return |
||
112 | * |
||
113 | * @var array |
||
114 | */ |
||
115 | private $fields = array(); |
||
116 | |||
117 | /** |
||
118 | * Constructor method |
||
119 | * |
||
120 | * Prepare environment for connection (bind) |
||
121 | * |
||
122 | * @param string $server ldap server (ip or FQDN) |
||
123 | * @param int $port port to connect to |
||
124 | * |
||
125 | * @throws LdaphException |
||
126 | */ |
||
127 | 15 | public function __construct($server, $port = 389) { |
|
128 | |||
129 | 15 | if ( empty($server) ) throw new LdaphException("Invalid LDAP parameters", 1401); |
|
130 | |||
131 | 12 | if ( !function_exists("ldap_connect") ) throw new LdaphException("PHP ldap extension not available", 1407); |
|
132 | |||
133 | 12 | $this->server = $server; |
|
134 | |||
135 | 12 | $this->port = filter_var($port, FILTER_VALIDATE_INT, array( |
|
136 | 4 | "options" => array( |
|
137 | 8 | "min_range" => 1, |
|
138 | 8 | "max_range" => 65535, |
|
139 | "default" => 389 |
||
140 | 8 | ) |
|
141 | 8 | ) |
|
142 | 8 | ); |
|
143 | |||
144 | 12 | } |
|
145 | |||
146 | /** |
||
147 | * Set ldap base |
||
148 | * |
||
149 | * @param string $dcs ldap base, comma separated, not spaced |
||
150 | * |
||
151 | * @return Ldaph |
||
152 | * |
||
153 | * @throws LdaphException |
||
154 | */ |
||
155 | 9 | View Code Duplication | final public function base($dcs) { |
166 | |||
167 | /** |
||
168 | * Set ldap distinguished name (used in ldap bind) |
||
169 | * |
||
170 | * Before bind, special word USERNAME will be substituted by real username |
||
171 | * |
||
172 | * @param string $dn ldap DN, comma separated, not spaced |
||
173 | * |
||
174 | * @return Ldaph |
||
175 | * |
||
176 | * @throws LdaphException |
||
177 | */ |
||
178 | 12 | final public function dn($dn) { |
|
187 | |||
188 | /** |
||
189 | * Set ldap version: 2 or 3 (default) |
||
190 | * |
||
191 | * @param int $mode ldap protocol version |
||
192 | * |
||
193 | * @return Ldaph |
||
194 | */ |
||
195 | 12 | final public function version($mode = 3) { |
|
208 | |||
209 | /** |
||
210 | * Enable/disable ssl for connection |
||
211 | * |
||
212 | * @param bool $mode |
||
213 | * |
||
214 | * @return Ldaph |
||
215 | */ |
||
216 | 12 | View Code Duplication | final public function ssl($mode = true) { |
229 | |||
230 | /** |
||
231 | * Enable/disable tls for connection |
||
232 | * |
||
233 | * @param bool $mode |
||
234 | * |
||
235 | * @return Ldaph |
||
236 | */ |
||
237 | 12 | View Code Duplication | final public function tls($mode = true) { |
250 | |||
251 | /** |
||
252 | * Enable/disable single sign on |
||
253 | * |
||
254 | * @param bool $mode |
||
255 | * |
||
256 | * @return Ldaph |
||
257 | * |
||
258 | * @throws LdaphException |
||
259 | */ |
||
260 | 3 | final public function sso($mode = true) { |
|
279 | |||
280 | /** |
||
281 | * Set user/pass for bind and search |
||
282 | * |
||
283 | * @param string $user |
||
284 | * @param string $pass |
||
285 | * |
||
286 | * @return Ldaph |
||
287 | * |
||
288 | * @throws LdaphException |
||
289 | */ |
||
290 | 9 | View Code Duplication | final public function account($user, $pass) { |
300 | |||
301 | /** |
||
302 | * Set ldap search base |
||
303 | * |
||
304 | * During search, special word PATTERN will be sbstituted by provided pattern |
||
305 | * |
||
306 | * @param string $s |
||
307 | * |
||
308 | * @return Ldaph |
||
309 | */ |
||
310 | 9 | final public function searchbase($s) { |
|
321 | |||
322 | /** |
||
323 | * Set fields to query ldap for |
||
324 | * |
||
325 | * @param mixed $f |
||
326 | * |
||
327 | * @return Ldaph |
||
328 | */ |
||
329 | 6 | final public function fields($f) { |
|
340 | |||
341 | /** |
||
342 | * Authenticate an user via LDAP |
||
343 | * |
||
344 | * @param string $userName The user to auth |
||
345 | * @param string $userPass The password for user |
||
346 | * |
||
347 | * @return bool |
||
348 | * |
||
349 | * @throws LdaphException |
||
350 | * @throws Exception |
||
351 | */ |
||
352 | 3 | public function auth($userName, $userPass) { |
|
379 | |||
380 | /** |
||
381 | * Search ldap directory for $what |
||
382 | * |
||
383 | * @param string $what The pattern to search for (will replace the searcbase PATTERN special word) |
||
384 | * @param bool $clean If true, raw ldap_get_entries result will be normalized as plain array |
||
385 | * |
||
386 | * @return array |
||
387 | * |
||
388 | * @throws LdaphException |
||
389 | * @throws Exception |
||
390 | */ |
||
391 | 6 | public function search($what = "*", $clean = false) { |
|
420 | |||
421 | /** |
||
422 | * Setup LDAP connection |
||
423 | * |
||
424 | * @param string $user |
||
425 | * @param string $pass |
||
426 | * |
||
427 | * @return bool |
||
428 | * |
||
429 | * @throws LdaphException |
||
430 | */ |
||
431 | 9 | private function setupConnection($user = null, $pass = null) { |
|
470 | |||
471 | /** |
||
472 | * Unset a previously opened ldap connection |
||
473 | */ |
||
474 | 9 | private function unsetConnection() { |
|
479 | |||
480 | /** |
||
481 | * Helper for $this->search() |
||
482 | * |
||
483 | * @param string $what |
||
484 | * @param bool $clean |
||
485 | * |
||
486 | * @return array |
||
487 | * |
||
488 | * @throws LdaphException |
||
489 | */ |
||
490 | private function searchHelper($what, $clean) { |
||
507 | |||
508 | /** |
||
509 | * Normalize ldap search result into plain array |
||
510 | * |
||
511 | * @return array |
||
512 | */ |
||
513 | private function searchCleaner($results) { |
||
572 | |||
573 | } |
||
574 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.