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 string the attribute for password default is unicodePwd (AD passoword) |
||
121 | */ |
||
122 | public $unicodePassword = 'unicodePwd'; |
||
123 | |||
124 | /** |
||
125 | * @var bool stores the bool whether or not the current connection is bound. |
||
126 | */ |
||
127 | protected $_bound = false; |
||
128 | |||
129 | /** |
||
130 | * @var resource|false |
||
131 | */ |
||
132 | protected $resource; |
||
133 | |||
134 | /** |
||
135 | * |
||
136 | * @var string |
||
137 | */ |
||
138 | protected $userDn; |
||
139 | |||
140 | # Create AD password (Microsoft Active Directory password format) |
||
141 | protected static function makeAdPassword($password) { |
||
146 | |||
147 | /** |
||
148 | * Returns the current query cache information. |
||
149 | * This method is used internally by [[Command]]. |
||
150 | * @param integer $duration the preferred caching duration. If null, it will be ignored. |
||
151 | * @param \yii\caching\Dependency $dependency the preferred caching dependency. If null, it will be ignored. |
||
152 | * @return array the current query cache information, or null if query cache is not enabled. |
||
153 | * @internal |
||
154 | */ |
||
155 | public function getCacheInfo($duration = 3600, $dependency = null) |
||
174 | |||
175 | /** |
||
176 | * Invalidates the cached data that are associated with any of the specified [[tags]] in this connection. |
||
177 | * @param string|array $tags |
||
178 | */ |
||
179 | public function clearCache($tags) |
||
184 | |||
185 | /** |
||
186 | * Connects and Binds to the Domain Controller with a administrator credentials. |
||
187 | * @return void |
||
188 | */ |
||
189 | public function open($anonymous = false) |
||
208 | |||
209 | /** |
||
210 | * Connection. |
||
211 | * @param string|array $hostname |
||
212 | * @param type $port |
||
213 | * @return void |
||
214 | */ |
||
215 | protected function connect($hostname = [], $port = '389') |
||
233 | |||
234 | /** |
||
235 | * Authenticate user |
||
236 | * @param string $username |
||
237 | * @param string $password |
||
238 | * @return int indicate occurrence of error. |
||
239 | */ |
||
240 | public function auth($username, $password) |
||
260 | |||
261 | /** |
||
262 | * Change the password of the current user. This must be performed over TLS. |
||
263 | * @param string $username User for change password |
||
264 | * @param string $oldPassword The old password |
||
265 | * @param string $newPassword The new password |
||
266 | * @return bool return true if change password is success |
||
267 | * @throws \Exception |
||
268 | */ |
||
269 | public function changePassword($username, $oldPassword, $newPassword) |
||
288 | |||
289 | /** |
||
290 | * Closes the current connection. |
||
291 | * |
||
292 | * @return boolean |
||
293 | */ |
||
294 | public function close() |
||
301 | |||
302 | /** |
||
303 | * Execute ldap search like. |
||
304 | * |
||
305 | * http://php.net/manual/en/ref.ldap.php |
||
306 | * |
||
307 | * @param string $function php LDAP function |
||
308 | * @param array $params params for execute ldap function |
||
309 | * @return bool|DataReader |
||
310 | */ |
||
311 | public function executeQuery($function, $params) |
||
340 | |||
341 | /** |
||
342 | * Returns true/false if the current connection is bound. |
||
343 | * @return bool |
||
344 | */ |
||
345 | public function getBound() |
||
349 | |||
350 | /** |
||
351 | * Get the current resource of connection. |
||
352 | * @return resource |
||
353 | */ |
||
354 | public function getResource() |
||
358 | |||
359 | /** |
||
360 | * Sorts an AD search result by the specified attribute. |
||
361 | * @param resource $result |
||
362 | * @param string $attribute |
||
363 | * @return bool |
||
364 | */ |
||
365 | public function sort($result, $attribute) |
||
369 | |||
370 | /** |
||
371 | * Adds an entry to the current connection. |
||
372 | * @param string $dn |
||
373 | * @param array $entry |
||
374 | * @return bool |
||
375 | */ |
||
376 | public function add($dn, array $entry) |
||
380 | |||
381 | /** |
||
382 | * Deletes an entry on the current connection. |
||
383 | * @param string $dn |
||
384 | * @return bool |
||
385 | */ |
||
386 | public function delete($dn) |
||
390 | |||
391 | /** |
||
392 | * Modify the name of an entry on the current connection. |
||
393 | * |
||
394 | * @param string $dn |
||
395 | * @param string $newRdn |
||
396 | * @param string $newParent |
||
397 | * @param bool $deleteOldRdn |
||
398 | * @return bool |
||
399 | */ |
||
400 | public function rename($dn, $newRdn, $newParent, $deleteOldRdn = false) |
||
404 | |||
405 | /** |
||
406 | * Batch modifies an existing entry on the current connection. |
||
407 | * The types of modifications: |
||
408 | * LDAP_MODIFY_BATCH_ADD - Each value specified through values is added. |
||
409 | * LDAP_MODIFY_BATCH_REMOVE - Each value specified through values is removed. |
||
410 | * Any value of the attribute not contained in the values array will remain untouched. |
||
411 | * LDAP_MODIFY_BATCH_REMOVE_ALL - All values are removed from the attribute named by attrib. |
||
412 | * LDAP_MODIFY_BATCH_REPLACE - All current values are replaced by new one. |
||
413 | * @param string $dn |
||
414 | * @param array $values array associative with three keys: "attrib", "modtype" and "values". |
||
415 | * ```php |
||
416 | * [ |
||
417 | * "attrib" => "attribute", |
||
418 | * "modtype" => LDAP_MODIFY_BATCH_ADD, |
||
419 | * "values" => ["attribute value one"], |
||
420 | * ], |
||
421 | * ``` |
||
422 | * @return mixed |
||
423 | */ |
||
424 | public function modify($dn, array $values) |
||
429 | |||
430 | /** |
||
431 | * Retrieve the entries from a search result. |
||
432 | * @param resource $searchResult |
||
433 | * @return array|boolean |
||
434 | */ |
||
435 | public function getEntries($searchResult) |
||
439 | |||
440 | /** |
||
441 | * Retrieves the number of entries from a search result. |
||
442 | * @param resource $searchResult |
||
443 | * @return int |
||
444 | */ |
||
445 | public function countEntries($searchResult) |
||
449 | |||
450 | /** |
||
451 | * Retrieves the first entry from a search result. |
||
452 | * @param resource $searchResult |
||
453 | * @return resource link identifier |
||
454 | */ |
||
455 | public function getFirstEntry($searchResult) |
||
459 | |||
460 | /** |
||
461 | * Retrieves the next entry from a search result. |
||
462 | * @param resource $entry link identifier |
||
463 | * @return resource |
||
464 | */ |
||
465 | public function getNextEntry($entry) |
||
469 | |||
470 | /** |
||
471 | * Retrieves the ldap first entry attribute. |
||
472 | * @param resource $entry |
||
473 | * @return string |
||
474 | */ |
||
475 | public function getFirstAttribute($entry) |
||
479 | |||
480 | /** |
||
481 | * Retrieves the ldap next entry attribute. |
||
482 | * @param resource $entry |
||
483 | * @return string |
||
484 | */ |
||
485 | public function getNextAttribute($entry) |
||
489 | |||
490 | /** |
||
491 | * Retrieves the ldap entry's attributes. |
||
492 | * @param resource $entry |
||
493 | * @return array |
||
494 | */ |
||
495 | public function getAttributes($entry) |
||
499 | |||
500 | /** |
||
501 | * Retrieves all binary values from a result entry. |
||
502 | * @param resource $entry link identifier |
||
503 | * @param string $attribute name of attribute |
||
504 | * @return array |
||
505 | */ |
||
506 | public function getValuesLen($entry, $attribute) |
||
510 | |||
511 | /** |
||
512 | * Retrieves the DN of a result entry. |
||
513 | * @param resource $entry |
||
514 | * @return string |
||
515 | */ |
||
516 | public function getDn($entry) |
||
520 | |||
521 | /** |
||
522 | * Free result memory. |
||
523 | * @param resource $searchResult |
||
524 | * @return bool |
||
525 | */ |
||
526 | public function freeResult($searchResult) |
||
530 | |||
531 | /** |
||
532 | * Sets an option on the current connection. |
||
533 | * @param int $option |
||
534 | * @param mixed $value |
||
535 | * @return boolean |
||
536 | */ |
||
537 | public function setOption($option, $value) |
||
541 | |||
542 | /** |
||
543 | * Starts a connection using TLS. |
||
544 | * @return bool |
||
545 | */ |
||
546 | public function startTLS() |
||
550 | |||
551 | /** |
||
552 | * Send LDAP pagination control. |
||
553 | * @param int $pageSize |
||
554 | * @param bool $isCritical |
||
555 | * @param string $cookie |
||
556 | * @return bool |
||
557 | */ |
||
558 | public function setControlPagedResult($cookie) |
||
562 | |||
563 | /** |
||
564 | * Retrieve a paginated result response. |
||
565 | * @param resource $result |
||
566 | * @param string $cookie |
||
567 | * @return bool |
||
568 | */ |
||
569 | public function setControlPagedResultResponse($result, &$cookie) |
||
573 | |||
574 | /** |
||
575 | * Retrieve the last error on the current connection. |
||
576 | * @return string |
||
577 | */ |
||
578 | public function getLastError() |
||
582 | |||
583 | /** |
||
584 | * Returns the number of the last error on the current connection. |
||
585 | * @return int |
||
586 | */ |
||
587 | public function getErrNo() |
||
591 | |||
592 | /** |
||
593 | * Returns the error string of the specified error number. |
||
594 | * @param int $number |
||
595 | * @return string |
||
596 | */ |
||
597 | public function err2Str($number) |
||
601 | } |
||
602 |
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.