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 |
||
24 | class Connection extends Component |
||
25 | { |
||
26 | /** |
||
27 | * LDAP protocol string. |
||
28 | * @var string |
||
29 | */ |
||
30 | const PROTOCOL = 'ldap://'; |
||
31 | |||
32 | /** |
||
33 | * LDAP port number. |
||
34 | * @var string |
||
35 | */ |
||
36 | const PORT = '389'; |
||
37 | |||
38 | /** |
||
39 | * @event Event an event that is triggered after a DB connection is established |
||
40 | */ |
||
41 | const EVENT_AFTER_OPEN = 'afterOpen'; |
||
42 | |||
43 | /** |
||
44 | * @var string the LDAP base dn. |
||
45 | */ |
||
46 | public $baseDn; |
||
47 | |||
48 | /** |
||
49 | * https://msdn.microsoft.com/en-us/library/ms677913(v=vs.85).aspx |
||
50 | * @var bool the integer to instruct the LDAP connection whether or not to follow referrals. |
||
51 | */ |
||
52 | public $followReferrals = false; |
||
53 | |||
54 | /** |
||
55 | * @var string The LDAP port to use when connecting to the domain controllers. |
||
56 | */ |
||
57 | public $port = self::PORT; |
||
58 | |||
59 | /** |
||
60 | * @var bool Determines whether or not to use TLS with the current LDAP connection. |
||
61 | */ |
||
62 | public $useTLS = true; |
||
63 | |||
64 | /** |
||
65 | * @var array the domain controllers to connect to. |
||
66 | */ |
||
67 | public $dc = []; |
||
68 | |||
69 | /** |
||
70 | * @var string the username for establishing LDAP connection. Defaults to `null` meaning no username to use. |
||
71 | */ |
||
72 | public $username; |
||
73 | |||
74 | /** |
||
75 | * @var string the password for establishing DB connection. Defaults to `null` meaning no password to use. |
||
76 | */ |
||
77 | public $password; |
||
78 | |||
79 | /** |
||
80 | * @var int The page size for the paging operation. |
||
81 | */ |
||
82 | public $pageSize = -1; |
||
83 | |||
84 | /** |
||
85 | * @var integer zero-based offset from where the records are to be returned. If not set or |
||
86 | * less than 1, it means not filter values. |
||
87 | */ |
||
88 | public $offset = -1; |
||
89 | |||
90 | /** |
||
91 | * @var boolean whether to enable caching. |
||
92 | * Note that in order to enable query caching, a valid cache component as specified |
||
93 | * by [[cache]] must be enabled and [[enableCache]] must be set true. |
||
94 | * Also, only the results of the queries enclosed within [[cache()]] will be cached. |
||
95 | * @see cacheDuration |
||
96 | * @see cache |
||
97 | */ |
||
98 | public $enableCache = true; |
||
99 | |||
100 | /** |
||
101 | * @var integer number of seconds that table metadata can remain valid in cache. |
||
102 | * Use 0 to indicate that the cached data will never expire. |
||
103 | * @see enableCache |
||
104 | */ |
||
105 | public $cacheDuration = 3600; |
||
106 | |||
107 | /** |
||
108 | * @var Cache|string the cache object or the ID of the cache application component that |
||
109 | * is used to cache result query. |
||
110 | * @see enableCache |
||
111 | */ |
||
112 | public $cache = 'cache'; |
||
113 | |||
114 | /** |
||
115 | * @var string the attribute for authentication |
||
116 | */ |
||
117 | public $loginAttribute = "sAMAccountName"; |
||
118 | |||
119 | /** |
||
120 | * @var bool stores the bool whether or not the current connection is bound. |
||
121 | */ |
||
122 | protected $_bound = false; |
||
123 | |||
124 | /** |
||
125 | * @var resource|false |
||
126 | */ |
||
127 | protected $resource; |
||
128 | |||
129 | /** |
||
130 | * |
||
131 | * @var string |
||
132 | */ |
||
133 | protected $userDN; |
||
134 | |||
135 | # Create AD password (Microsoft Active Directory password format) |
||
136 | protected static function encodePassword($password) { |
||
141 | |||
142 | /** |
||
143 | * Returns the current query cache information. |
||
144 | * This method is used internally by [[Command]]. |
||
145 | * @param integer $duration the preferred caching duration. If null, it will be ignored. |
||
146 | * @param \yii\caching\Dependency $dependency the preferred caching dependency. If null, it will be ignored. |
||
147 | * @return array the current query cache information, or null if query cache is not enabled. |
||
148 | * @internal |
||
149 | */ |
||
150 | public function getCacheInfo($duration = 3600, $dependency = null) |
||
169 | |||
170 | /** |
||
171 | * Invalidates the cached data that are associated with any of the specified [[tags]] in this connection. |
||
172 | * @param string|array $tags |
||
173 | */ |
||
174 | public function clearCache($tags) |
||
179 | |||
180 | /** |
||
181 | * Connects and Binds to the Domain Controller with a administrator credentials. |
||
182 | * @return void |
||
183 | */ |
||
184 | public function open($anonymous = false) |
||
203 | |||
204 | /** |
||
205 | * Connection. |
||
206 | * @param string|array $hostname |
||
207 | * @param type $port |
||
208 | * @return void |
||
209 | */ |
||
210 | protected function connect($hostname = [], $port = '389') |
||
228 | |||
229 | /** |
||
230 | * Authenticate user |
||
231 | * @param string $username |
||
232 | * @param string $password |
||
233 | * @return int indicate occurrence of error. |
||
234 | */ |
||
235 | public function auth($username, $password) |
||
255 | |||
256 | /** |
||
257 | * Change the password of the current user. This must be performed over TLS. |
||
258 | * @param string $username User for change password |
||
259 | * @param string $oldPassword The old password |
||
260 | * @param string $newPassword The new password |
||
261 | * @return bool return true if change password is success |
||
262 | * @throws \Exception |
||
263 | */ |
||
264 | public function changePasswordAsUser($username, $oldPassword, $newPassword) |
||
278 | |||
279 | /** |
||
280 | * Change the password of the user as manager. This must be performed over TLS. |
||
281 | * @param string $userDN User Distinguished Names (DN) for change password. Ex.: cn=admin,dc=example,dc=com |
||
282 | * @param string $newPassword The new password |
||
283 | * @return bool return true if change password is success |
||
284 | * @throws \Exception |
||
285 | */ |
||
286 | public function changePasswordAsManager($userDN, $newPassword) |
||
301 | |||
302 | /** |
||
303 | * Closes the current connection. |
||
304 | * |
||
305 | * @return boolean |
||
306 | */ |
||
307 | public function close() |
||
314 | |||
315 | /** |
||
316 | * Execute ldap search like. |
||
317 | * |
||
318 | * http://php.net/manual/en/ref.ldap.php |
||
319 | * |
||
320 | * @param string $function php LDAP function |
||
321 | * @param array $params params for execute ldap function |
||
322 | * @return bool|DataReader |
||
323 | */ |
||
324 | public function executeQuery($function, $params) |
||
353 | |||
354 | /** |
||
355 | * Returns true/false if the current connection is bound. |
||
356 | * @return bool |
||
357 | */ |
||
358 | public function getBound() |
||
362 | |||
363 | /** |
||
364 | * Get the current resource of connection. |
||
365 | * @return resource |
||
366 | */ |
||
367 | public function getResource() |
||
371 | |||
372 | /** |
||
373 | * Sorts an AD search result by the specified attribute. |
||
374 | * @param resource $result |
||
375 | * @param string $attribute |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function sort($result, $attribute) |
||
382 | |||
383 | /** |
||
384 | * Adds an entry to the current connection. |
||
385 | * @param string $dn |
||
386 | * @param array $entry |
||
387 | * @return bool |
||
388 | */ |
||
389 | public function add($dn, array $entry) |
||
393 | |||
394 | /** |
||
395 | * Deletes an entry on the current connection. |
||
396 | * @param string $dn |
||
397 | * @return bool |
||
398 | */ |
||
399 | public function delete($dn) |
||
403 | |||
404 | /** |
||
405 | * Modify the name of an entry on the current connection. |
||
406 | * |
||
407 | * @param string $dn |
||
408 | * @param string $newRdn |
||
409 | * @param string $newParent |
||
410 | * @param bool $deleteOldRdn |
||
411 | * @return bool |
||
412 | */ |
||
413 | public function rename($dn, $newRdn, $newParent, $deleteOldRdn = false) |
||
417 | |||
418 | /** |
||
419 | * Batch modifies an existing entry on the current connection. |
||
420 | * The types of modifications: |
||
421 | * LDAP_MODIFY_BATCH_ADD - Each value specified through values is added. |
||
422 | * LDAP_MODIFY_BATCH_REMOVE - Each value specified through values is removed. |
||
423 | * Any value of the attribute not contained in the values array will remain untouched. |
||
424 | * LDAP_MODIFY_BATCH_REMOVE_ALL - All values are removed from the attribute named by attrib. |
||
425 | * LDAP_MODIFY_BATCH_REPLACE - All current values are replaced by new one. |
||
426 | * @param string $dn |
||
427 | * @param array $values array associative with three keys: "attrib", "modtype" and "values". |
||
428 | * ```php |
||
429 | * [ |
||
430 | * "attrib" => "attribute", |
||
431 | * "modtype" => LDAP_MODIFY_BATCH_ADD, |
||
432 | * "values" => ["attribute value one"], |
||
433 | * ], |
||
434 | * ``` |
||
435 | * @return mixed |
||
436 | */ |
||
437 | public function modify($dn, array $values) |
||
442 | |||
443 | /** |
||
444 | * Retrieve the entries from a search result. |
||
445 | * @param resource $searchResult |
||
446 | * @return array|boolean |
||
447 | */ |
||
448 | public function getEntries($searchResult) |
||
452 | |||
453 | /** |
||
454 | * Retrieves the number of entries from a search result. |
||
455 | * @param resource $searchResult |
||
456 | * @return int |
||
457 | */ |
||
458 | public function countEntries($searchResult) |
||
462 | |||
463 | /** |
||
464 | * Retrieves the first entry from a search result. |
||
465 | * @param resource $searchResult |
||
466 | * @return resource link identifier |
||
467 | */ |
||
468 | public function getFirstEntry($searchResult) |
||
472 | |||
473 | /** |
||
474 | * Retrieves the next entry from a search result. |
||
475 | * @param resource $entry link identifier |
||
476 | * @return resource |
||
477 | */ |
||
478 | public function getNextEntry($entry) |
||
482 | |||
483 | /** |
||
484 | * Retrieves the ldap first entry attribute. |
||
485 | * @param resource $entry |
||
486 | * @return string |
||
487 | */ |
||
488 | public function getFirstAttribute($entry) |
||
492 | |||
493 | /** |
||
494 | * Retrieves the ldap next entry attribute. |
||
495 | * @param resource $entry |
||
496 | * @return string |
||
497 | */ |
||
498 | public function getNextAttribute($entry) |
||
502 | |||
503 | /** |
||
504 | * Retrieves the ldap entry's attributes. |
||
505 | * @param resource $entry |
||
506 | * @return array |
||
507 | */ |
||
508 | public function getAttributes($entry) |
||
512 | |||
513 | /** |
||
514 | * Retrieves all binary values from a result entry. |
||
515 | * @param resource $entry link identifier |
||
516 | * @param string $attribute name of attribute |
||
517 | * @return array |
||
518 | */ |
||
519 | public function getValuesLen($entry, $attribute) |
||
523 | |||
524 | /** |
||
525 | * Retrieves the DN of a result entry. |
||
526 | * @param resource $entry |
||
527 | * @return string |
||
528 | */ |
||
529 | public function getDn($entry) |
||
533 | |||
534 | /** |
||
535 | * Free result memory. |
||
536 | * @param resource $searchResult |
||
537 | * @return bool |
||
538 | */ |
||
539 | public function freeResult($searchResult) |
||
543 | |||
544 | /** |
||
545 | * Sets an option on the current connection. |
||
546 | * @param int $option |
||
547 | * @param mixed $value |
||
548 | * @return boolean |
||
549 | */ |
||
550 | public function setOption($option, $value) |
||
554 | |||
555 | /** |
||
556 | * Starts a connection using TLS. |
||
557 | * @return bool |
||
558 | */ |
||
559 | public function startTLS() |
||
563 | |||
564 | /** |
||
565 | * Send LDAP pagination control. |
||
566 | * @param int $pageSize |
||
567 | * @param bool $isCritical |
||
568 | * @param string $cookie |
||
569 | * @return bool |
||
570 | */ |
||
571 | public function setControlPagedResult($cookie) |
||
575 | |||
576 | /** |
||
577 | * Retrieve a paginated result response. |
||
578 | * @param resource $result |
||
579 | * @param string $cookie |
||
580 | * @return bool |
||
581 | */ |
||
582 | public function setControlPagedResultResponse($result, &$cookie) |
||
586 | |||
587 | /** |
||
588 | * Retrieve the last error on the current connection. |
||
589 | * @return string |
||
590 | */ |
||
591 | public function getLastError() |
||
595 | |||
596 | /** |
||
597 | * Returns the number of the last error on the current connection. |
||
598 | * @return int |
||
599 | */ |
||
600 | public function getErrNo() |
||
604 | |||
605 | /** |
||
606 | * Returns the error string of the specified error number. |
||
607 | * @param int $number |
||
608 | * @return string |
||
609 | */ |
||
610 | public function err2Str($number) |
||
614 | } |
||
615 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.