Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Connection extends Component |
||
23 | { |
||
24 | /** |
||
25 | * LDAP protocol string. |
||
26 | * @var string |
||
27 | */ |
||
28 | const PROTOCOL = 'ldap://'; |
||
29 | |||
30 | /** |
||
31 | * LDAP port number. |
||
32 | * @var string |
||
33 | */ |
||
34 | const PORT = '389'; |
||
35 | |||
36 | /** |
||
37 | * @event Event an event that is triggered after a DB connection is established |
||
38 | */ |
||
39 | const EVENT_AFTER_OPEN = 'afterOpen'; |
||
40 | |||
41 | /** |
||
42 | * @var string the LDAP base dn. |
||
43 | */ |
||
44 | public $baseDn; |
||
45 | |||
46 | /** |
||
47 | * https://msdn.microsoft.com/en-us/library/ms677913(v=vs.85).aspx |
||
48 | * @var bool the integer to instruct the LDAP connection whether or not to follow referrals. |
||
49 | */ |
||
50 | public $followReferrals = false; |
||
51 | |||
52 | /** |
||
53 | * @var string The LDAP port to use when connecting to the domain controllers. |
||
54 | */ |
||
55 | public $port = self::PORT; |
||
56 | |||
57 | /** |
||
58 | * @var bool Determines whether or not to use TLS with the current LDAP connection. |
||
59 | */ |
||
60 | public $useTLS = true; |
||
61 | |||
62 | /** |
||
63 | * @var array the domain controllers to connect to. |
||
64 | */ |
||
65 | public $dc = []; |
||
66 | |||
67 | /** |
||
68 | * @var string the username for establishing LDAP connection. Defaults to `null` meaning no username to use. |
||
69 | */ |
||
70 | public $username; |
||
71 | |||
72 | /** |
||
73 | * @var string the password for establishing DB connection. Defaults to `null` meaning no password to use. |
||
74 | */ |
||
75 | public $password; |
||
76 | |||
77 | /** |
||
78 | * @var int The page size for the paging operation. |
||
79 | */ |
||
80 | public $pageSize = -1; |
||
81 | |||
82 | /** |
||
83 | * @var integer zero-based offset from where the records are to be returned. If not set or |
||
84 | * less than 1, it means not filter values. |
||
85 | */ |
||
86 | public $offset = -1; |
||
87 | |||
88 | /** |
||
89 | * @var boolean whether to enable caching. |
||
90 | * Note that in order to enable truly caching, a valid cache component as specified |
||
91 | * by [[cache]] must be enabled and [[enableCache]] must be set true. |
||
92 | * @see cacheDuration |
||
93 | * @see cache |
||
94 | */ |
||
95 | public $enableCache = false; |
||
96 | |||
97 | /** |
||
98 | * @var integer number of seconds that table metadata can remain valid in cache. |
||
99 | * Use 0 to indicate that the cached data will never expire. |
||
100 | * @see enableCache |
||
101 | */ |
||
102 | public $cacheDuration = 3600; |
||
103 | |||
104 | /** |
||
105 | * @var Cache|string the cache object or the ID of the cache application component that |
||
106 | * is used to cache result query. |
||
107 | * @see enableCache |
||
108 | */ |
||
109 | public $cache = 'cache'; |
||
110 | |||
111 | /** |
||
112 | * @var string the attribute for authentication |
||
113 | */ |
||
114 | public $loginAttribute = "sAMAccountName"; |
||
115 | |||
116 | /** |
||
117 | * @var string the attribute for password default is unicodePwd (AD passoword) |
||
118 | */ |
||
119 | public $unicodePassword = 'unicodePwd'; |
||
120 | |||
121 | /** |
||
122 | * @var bool stores the bool whether or not the current connection is bound. |
||
123 | */ |
||
124 | protected $_bound = false; |
||
125 | |||
126 | /** |
||
127 | * @var resource|false |
||
128 | */ |
||
129 | protected $resource; |
||
130 | |||
131 | /** |
||
132 | * |
||
133 | * @var string |
||
134 | */ |
||
135 | protected $userDn; |
||
136 | |||
137 | # Create AD password (Microsoft Active Directory password format) |
||
138 | protected static function makeAdPassword($password) { |
||
143 | |||
144 | /** |
||
145 | * Connects and Binds to the Domain Controller with a administrator credentials. |
||
146 | * @return void |
||
147 | */ |
||
148 | protected function open($anonymous = false) |
||
163 | |||
164 | /** |
||
165 | * Connection. |
||
166 | * @param string|array $hostname |
||
167 | * @param type $port |
||
168 | * @return void |
||
169 | */ |
||
170 | public function connect($hostname = [], $port = '389') |
||
188 | |||
189 | /** |
||
190 | * Authenticate user |
||
191 | * @param string $username |
||
192 | * @param string $password |
||
193 | * @return int indicate occurrence of error. |
||
194 | */ |
||
195 | public function auth($username, $password) |
||
215 | |||
216 | /** |
||
217 | * Change the password of the current user. This must be performed over TLS. |
||
218 | * @param string $username User for change password |
||
219 | * @param string $oldPassword The old password |
||
220 | * @param string $newPassword The new password |
||
221 | * @return bool return true if change password is success |
||
222 | * @throws \Exception |
||
223 | */ |
||
224 | public function changePassword($username, $oldPassword, $newPassword) |
||
243 | |||
244 | /** |
||
245 | * Closes the current connection. |
||
246 | * |
||
247 | * @return boolean |
||
248 | */ |
||
249 | public function close() |
||
256 | |||
257 | /** |
||
258 | * Execute ldap functions like. |
||
259 | * |
||
260 | * http://php.net/manual/en/ref.ldap.php |
||
261 | * |
||
262 | * @param string $function php LDAP function |
||
263 | * @param array $params params for execute ldap function |
||
264 | * @return bool|DataReader |
||
265 | */ |
||
266 | public function executeQuery($function, $params) |
||
287 | |||
288 | /** |
||
289 | * Returns true/false if the current connection is bound. |
||
290 | * @return bool |
||
291 | */ |
||
292 | public function getBound() |
||
296 | |||
297 | /** |
||
298 | * Get the current resource of connection. |
||
299 | * @return resource |
||
300 | */ |
||
301 | public function getResource() |
||
305 | |||
306 | /** |
||
307 | * Sorts an AD search result by the specified attribute. |
||
308 | * @param resource $result |
||
309 | * @param string $attribute |
||
310 | * @return bool |
||
311 | */ |
||
312 | public function sort($result, $attribute) |
||
316 | |||
317 | /** |
||
318 | * Adds an entry to the current connection. |
||
319 | * @param string $dn |
||
320 | * @param array $entry |
||
321 | * @return bool |
||
322 | */ |
||
323 | public function add($dn, array $entry) |
||
327 | |||
328 | /** |
||
329 | * Deletes an entry on the current connection. |
||
330 | * @param string $dn |
||
331 | * @return bool |
||
332 | */ |
||
333 | public function delete($dn) |
||
337 | |||
338 | /** |
||
339 | * Modify the name of an entry on the current connection. |
||
340 | * |
||
341 | * @param string $dn |
||
342 | * @param string $newRdn |
||
343 | * @param string $newParent |
||
344 | * @param bool $deleteOldRdn |
||
345 | * @return bool |
||
346 | */ |
||
347 | public function rename($dn, $newRdn, $newParent, $deleteOldRdn = false) |
||
351 | |||
352 | /** |
||
353 | * Batch modifies an existing entry on the current connection. |
||
354 | * The types of modifications: |
||
355 | * LDAP_MODIFY_BATCH_ADD - Each value specified through values is added. |
||
356 | * LDAP_MODIFY_BATCH_REMOVE - Each value specified through values is removed. |
||
357 | * Any value of the attribute not contained in the values array will remain untouched. |
||
358 | * LDAP_MODIFY_BATCH_REMOVE_ALL - All values are removed from the attribute named by attrib. |
||
359 | * LDAP_MODIFY_BATCH_REPLACE - All current values are replaced by new one. |
||
360 | * @param string $dn |
||
361 | * @param array $values array associative with three keys: "attrib", "modtype" and "values". |
||
362 | * ```php |
||
363 | * [ |
||
364 | * "attrib" => "attribute", |
||
365 | * "modtype" => LDAP_MODIFY_BATCH_ADD, |
||
366 | * "values" => ["attribute value one"], |
||
367 | * ], |
||
368 | * ``` |
||
369 | * @return mixed |
||
370 | */ |
||
371 | public function modify($dn, array $values) |
||
375 | |||
376 | /** |
||
377 | * Retrieve the entries from a search result. |
||
378 | * @param resource $searchResult |
||
379 | * @return array|boolean |
||
380 | */ |
||
381 | public function getEntries($searchResult) |
||
385 | |||
386 | /** |
||
387 | * Retrieves the number of entries from a search result. |
||
388 | * @param resource $searchResult |
||
389 | * @return int |
||
390 | */ |
||
391 | public function countEntries($searchResult) |
||
395 | |||
396 | /** |
||
397 | * Retrieves the first entry from a search result. |
||
398 | * @param resource $searchResult |
||
399 | * @return resource link identifier |
||
400 | */ |
||
401 | public function getFirstEntry($searchResult) |
||
405 | |||
406 | /** |
||
407 | * Retrieves the next entry from a search result. |
||
408 | * @param resource $entry link identifier |
||
409 | * @return resource |
||
410 | */ |
||
411 | public function getNextEntry($entry) |
||
415 | |||
416 | /** |
||
417 | * Retrieves the ldap first entry attribute. |
||
418 | * @param resource $entry |
||
419 | * @return string |
||
420 | */ |
||
421 | public function getFirstAttribute($entry) |
||
425 | |||
426 | /** |
||
427 | * Retrieves the ldap next entry attribute. |
||
428 | * @param resource $entry |
||
429 | * @return string |
||
430 | */ |
||
431 | public function getNextAttribute($entry) |
||
435 | |||
436 | /** |
||
437 | * Retrieves the ldap entry's attributes. |
||
438 | * @param resource $entry |
||
439 | * @return array |
||
440 | */ |
||
441 | public function getAttributes($entry) |
||
445 | |||
446 | /** |
||
447 | * Retrieves all binary values from a result entry. |
||
448 | * @param resource $entry link identifier |
||
449 | * @param string $attribute name of attribute |
||
450 | * @return array |
||
451 | */ |
||
452 | public function getValuesLen($entry, $attribute) |
||
456 | |||
457 | /** |
||
458 | * Retrieves the DN of a result entry. |
||
459 | * @param resource $entry |
||
460 | * @return string |
||
461 | */ |
||
462 | public function getDn($entry) |
||
466 | |||
467 | /** |
||
468 | * Free result memory. |
||
469 | * @param resource $searchResult |
||
470 | * @return bool |
||
471 | */ |
||
472 | public function freeResult($searchResult) |
||
476 | |||
477 | /** |
||
478 | * Sets an option on the current connection. |
||
479 | * @param int $option |
||
480 | * @param mixed $value |
||
481 | * @return boolean |
||
482 | */ |
||
483 | public function setOption($option, $value) |
||
487 | |||
488 | /** |
||
489 | * Starts a connection using TLS. |
||
490 | * @return bool |
||
491 | */ |
||
492 | public function startTLS() |
||
496 | |||
497 | /** |
||
498 | * Send LDAP pagination control. |
||
499 | * @param int $pageSize |
||
500 | * @param bool $isCritical |
||
501 | * @param string $cookie |
||
502 | * @return bool |
||
503 | */ |
||
504 | public function setControlPagedResult($cookie) |
||
508 | |||
509 | /** |
||
510 | * Retrieve a paginated result response. |
||
511 | * @param resource $result |
||
512 | * @param string $cookie |
||
513 | * @return bool |
||
514 | */ |
||
515 | public function setControlPagedResultResponse($result, &$cookie) |
||
519 | |||
520 | /** |
||
521 | * Retrieve the last error on the current connection. |
||
522 | * @return string |
||
523 | */ |
||
524 | public function getLastError() |
||
528 | |||
529 | /** |
||
530 | * Returns the number of the last error on the current connection. |
||
531 | * @return int |
||
532 | */ |
||
533 | public function getErrNo() |
||
537 | |||
538 | /** |
||
539 | * Returns the error string of the specified error number. |
||
540 | * @param int $number |
||
541 | * @return string |
||
542 | */ |
||
543 | public function err2Str($number) |
||
547 | } |
||
548 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.