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) |
||
183 | |||
184 | /** |
||
185 | * Connects and Binds to the Domain Controller with a administrator credentials. |
||
186 | * @return void |
||
187 | */ |
||
188 | protected function open($anonymous = false) |
||
207 | |||
208 | /** |
||
209 | * Connection. |
||
210 | * @param string|array $hostname |
||
211 | * @param type $port |
||
212 | * @return void |
||
213 | */ |
||
214 | public function connect($hostname = [], $port = '389') |
||
232 | |||
233 | /** |
||
234 | * Authenticate user |
||
235 | * @param string $username |
||
236 | * @param string $password |
||
237 | * @return int indicate occurrence of error. |
||
238 | */ |
||
239 | public function auth($username, $password) |
||
259 | |||
260 | /** |
||
261 | * Change the password of the current user. This must be performed over TLS. |
||
262 | * @param string $username User for change password |
||
263 | * @param string $oldPassword The old password |
||
264 | * @param string $newPassword The new password |
||
265 | * @return bool return true if change password is success |
||
266 | * @throws \Exception |
||
267 | */ |
||
268 | public function changePassword($username, $oldPassword, $newPassword) |
||
287 | |||
288 | /** |
||
289 | * Closes the current connection. |
||
290 | * |
||
291 | * @return boolean |
||
292 | */ |
||
293 | public function close() |
||
300 | |||
301 | /** |
||
302 | * Execute ldap functions like. |
||
303 | * |
||
304 | * http://php.net/manual/en/ref.ldap.php |
||
305 | * |
||
306 | * @param string $function php LDAP function |
||
307 | * @param array $params params for execute ldap function |
||
308 | * @return bool|DataReader |
||
309 | */ |
||
310 | public function executeQuery($function, $params) |
||
339 | |||
340 | /** |
||
341 | * Returns true/false if the current connection is bound. |
||
342 | * @return bool |
||
343 | */ |
||
344 | public function getBound() |
||
348 | |||
349 | /** |
||
350 | * Get the current resource of connection. |
||
351 | * @return resource |
||
352 | */ |
||
353 | public function getResource() |
||
357 | |||
358 | /** |
||
359 | * Sorts an AD search result by the specified attribute. |
||
360 | * @param resource $result |
||
361 | * @param string $attribute |
||
362 | * @return bool |
||
363 | */ |
||
364 | public function sort($result, $attribute) |
||
368 | |||
369 | /** |
||
370 | * Adds an entry to the current connection. |
||
371 | * @param string $dn |
||
372 | * @param array $entry |
||
373 | * @return bool |
||
374 | */ |
||
375 | public function add($dn, array $entry) |
||
379 | |||
380 | /** |
||
381 | * Deletes an entry on the current connection. |
||
382 | * @param string $dn |
||
383 | * @return bool |
||
384 | */ |
||
385 | public function delete($dn) |
||
389 | |||
390 | /** |
||
391 | * Modify the name of an entry on the current connection. |
||
392 | * |
||
393 | * @param string $dn |
||
394 | * @param string $newRdn |
||
395 | * @param string $newParent |
||
396 | * @param bool $deleteOldRdn |
||
397 | * @return bool |
||
398 | */ |
||
399 | public function rename($dn, $newRdn, $newParent, $deleteOldRdn = false) |
||
403 | |||
404 | /** |
||
405 | * Batch modifies an existing entry on the current connection. |
||
406 | * The types of modifications: |
||
407 | * LDAP_MODIFY_BATCH_ADD - Each value specified through values is added. |
||
408 | * LDAP_MODIFY_BATCH_REMOVE - Each value specified through values is removed. |
||
409 | * Any value of the attribute not contained in the values array will remain untouched. |
||
410 | * LDAP_MODIFY_BATCH_REMOVE_ALL - All values are removed from the attribute named by attrib. |
||
411 | * LDAP_MODIFY_BATCH_REPLACE - All current values are replaced by new one. |
||
412 | * @param string $dn |
||
413 | * @param array $values array associative with three keys: "attrib", "modtype" and "values". |
||
414 | * ```php |
||
415 | * [ |
||
416 | * "attrib" => "attribute", |
||
417 | * "modtype" => LDAP_MODIFY_BATCH_ADD, |
||
418 | * "values" => ["attribute value one"], |
||
419 | * ], |
||
420 | * ``` |
||
421 | * @return mixed |
||
422 | */ |
||
423 | public function modify($dn, array $values) |
||
427 | |||
428 | /** |
||
429 | * Retrieve the entries from a search result. |
||
430 | * @param resource $searchResult |
||
431 | * @return array|boolean |
||
432 | */ |
||
433 | public function getEntries($searchResult) |
||
437 | |||
438 | /** |
||
439 | * Retrieves the number of entries from a search result. |
||
440 | * @param resource $searchResult |
||
441 | * @return int |
||
442 | */ |
||
443 | public function countEntries($searchResult) |
||
447 | |||
448 | public function countEntriesBySearch($db) |
||
452 | |||
453 | /** |
||
454 | * Retrieves the first entry from a search result. |
||
455 | * @param resource $searchResult |
||
456 | * @return resource link identifier |
||
457 | */ |
||
458 | public function getFirstEntry($searchResult) |
||
462 | |||
463 | /** |
||
464 | * Retrieves the next entry from a search result. |
||
465 | * @param resource $entry link identifier |
||
466 | * @return resource |
||
467 | */ |
||
468 | public function getNextEntry($entry) |
||
472 | |||
473 | /** |
||
474 | * Retrieves the ldap first entry attribute. |
||
475 | * @param resource $entry |
||
476 | * @return string |
||
477 | */ |
||
478 | public function getFirstAttribute($entry) |
||
482 | |||
483 | /** |
||
484 | * Retrieves the ldap next entry attribute. |
||
485 | * @param resource $entry |
||
486 | * @return string |
||
487 | */ |
||
488 | public function getNextAttribute($entry) |
||
492 | |||
493 | /** |
||
494 | * Retrieves the ldap entry's attributes. |
||
495 | * @param resource $entry |
||
496 | * @return array |
||
497 | */ |
||
498 | public function getAttributes($entry) |
||
502 | |||
503 | /** |
||
504 | * Retrieves all binary values from a result entry. |
||
505 | * @param resource $entry link identifier |
||
506 | * @param string $attribute name of attribute |
||
507 | * @return array |
||
508 | */ |
||
509 | public function getValuesLen($entry, $attribute) |
||
513 | |||
514 | /** |
||
515 | * Retrieves the DN of a result entry. |
||
516 | * @param resource $entry |
||
517 | * @return string |
||
518 | */ |
||
519 | public function getDn($entry) |
||
523 | |||
524 | /** |
||
525 | * Free result memory. |
||
526 | * @param resource $searchResult |
||
527 | * @return bool |
||
528 | */ |
||
529 | public function freeResult($searchResult) |
||
533 | |||
534 | /** |
||
535 | * Sets an option on the current connection. |
||
536 | * @param int $option |
||
537 | * @param mixed $value |
||
538 | * @return boolean |
||
539 | */ |
||
540 | public function setOption($option, $value) |
||
544 | |||
545 | /** |
||
546 | * Starts a connection using TLS. |
||
547 | * @return bool |
||
548 | */ |
||
549 | public function startTLS() |
||
553 | |||
554 | /** |
||
555 | * Send LDAP pagination control. |
||
556 | * @param int $pageSize |
||
557 | * @param bool $isCritical |
||
558 | * @param string $cookie |
||
559 | * @return bool |
||
560 | */ |
||
561 | public function setControlPagedResult($cookie) |
||
565 | |||
566 | /** |
||
567 | * Retrieve a paginated result response. |
||
568 | * @param resource $result |
||
569 | * @param string $cookie |
||
570 | * @return bool |
||
571 | */ |
||
572 | public function setControlPagedResultResponse($result, &$cookie) |
||
576 | |||
577 | /** |
||
578 | * Retrieve the last error on the current connection. |
||
579 | * @return string |
||
580 | */ |
||
581 | public function getLastError() |
||
585 | |||
586 | /** |
||
587 | * Returns the number of the last error on the current connection. |
||
588 | * @return int |
||
589 | */ |
||
590 | public function getErrNo() |
||
594 | |||
595 | /** |
||
596 | * Returns the error string of the specified error number. |
||
597 | * @param int $number |
||
598 | * @return string |
||
599 | */ |
||
600 | public function err2Str($number) |
||
604 | } |
||
605 |
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.