Complex classes like auth_plugin_authpdo 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 auth_plugin_authpdo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class auth_plugin_authpdo extends DokuWiki_Auth_Plugin { |
||
| 16 | |||
| 17 | /** @var PDO */ |
||
| 18 | protected $pdo; |
||
| 19 | |||
| 20 | /** @var null|array The list of all groups */ |
||
| 21 | protected $groupcache = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructor. |
||
| 25 | */ |
||
| 26 | public function __construct() { |
||
| 27 | parent::__construct(); // for compatibility |
||
| 28 | |||
| 29 | if(!class_exists('PDO')) { |
||
| 30 | $this->_debug('PDO extension for PHP not found.', -1, __LINE__); |
||
| 31 | $this->success = false; |
||
| 32 | return; |
||
| 33 | } |
||
| 34 | |||
| 35 | if(!$this->getConf('dsn')) { |
||
| 36 | $this->_debug('No DSN specified', -1, __LINE__); |
||
| 37 | $this->success = false; |
||
| 38 | return; |
||
| 39 | } |
||
| 40 | |||
| 41 | try { |
||
| 42 | $this->pdo = new PDO( |
||
| 43 | $this->getConf('dsn'), |
||
| 44 | $this->getConf('user'), |
||
| 45 | conf_decodeString($this->getConf('pass')), |
||
| 46 | array( |
||
| 47 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // always fetch as array |
||
| 48 | PDO::ATTR_EMULATE_PREPARES => true, // emulating prepares allows us to reuse param names |
||
| 49 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // we want exceptions, not error codes |
||
| 50 | ) |
||
| 51 | ); |
||
| 52 | } catch(PDOException $e) { |
||
| 53 | $this->_debug($e); |
||
| 54 | msg($this->getLang('connectfail'), -1); |
||
| 55 | $this->success = false; |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | // can Users be created? |
||
| 60 | $this->cando['addUser'] = $this->_chkcnf( |
||
| 61 | array( |
||
| 62 | 'select-user', |
||
| 63 | 'select-user-groups', |
||
| 64 | 'select-groups', |
||
| 65 | 'insert-user', |
||
| 66 | 'insert-group', |
||
| 67 | 'join-group' |
||
| 68 | ) |
||
| 69 | ); |
||
| 70 | |||
| 71 | // can Users be deleted? |
||
| 72 | $this->cando['delUser'] = $this->_chkcnf( |
||
| 73 | array( |
||
| 74 | 'select-user', |
||
| 75 | 'select-user-groups', |
||
| 76 | 'select-groups', |
||
| 77 | 'leave-group', |
||
| 78 | 'delete-user' |
||
| 79 | ) |
||
| 80 | ); |
||
| 81 | |||
| 82 | // can login names be changed? |
||
| 83 | $this->cando['modLogin'] = $this->_chkcnf( |
||
| 84 | array( |
||
| 85 | 'select-user', |
||
| 86 | 'select-user-groups', |
||
| 87 | 'update-user-login' |
||
| 88 | ) |
||
| 89 | ); |
||
| 90 | |||
| 91 | // can passwords be changed? |
||
| 92 | $this->cando['modPass'] = $this->_chkcnf( |
||
| 93 | array( |
||
| 94 | 'select-user', |
||
| 95 | 'select-user-groups', |
||
| 96 | 'update-user-pass' |
||
| 97 | ) |
||
| 98 | ); |
||
| 99 | |||
| 100 | // can real names be changed? |
||
| 101 | $this->cando['modName'] = $this->_chkcnf( |
||
| 102 | array( |
||
| 103 | 'select-user', |
||
| 104 | 'select-user-groups', |
||
| 105 | 'update-user-info:name' |
||
| 106 | ) |
||
| 107 | ); |
||
| 108 | |||
| 109 | // can real email be changed? |
||
| 110 | $this->cando['modMail'] = $this->_chkcnf( |
||
| 111 | array( |
||
| 112 | 'select-user', |
||
| 113 | 'select-user-groups', |
||
| 114 | 'update-user-info:mail' |
||
| 115 | ) |
||
| 116 | ); |
||
| 117 | |||
| 118 | // can groups be changed? |
||
| 119 | $this->cando['modGroups'] = $this->_chkcnf( |
||
| 120 | array( |
||
| 121 | 'select-user', |
||
| 122 | 'select-user-groups', |
||
| 123 | 'select-groups', |
||
| 124 | 'leave-group', |
||
| 125 | 'join-group', |
||
| 126 | 'insert-group' |
||
| 127 | ) |
||
| 128 | ); |
||
| 129 | |||
| 130 | // can a filtered list of users be retrieved? |
||
| 131 | $this->cando['getUsers'] = $this->_chkcnf( |
||
| 132 | array( |
||
| 133 | 'list-users' |
||
| 134 | ) |
||
| 135 | ); |
||
| 136 | |||
| 137 | // can the number of users be retrieved? |
||
| 138 | $this->cando['getUserCount'] = $this->_chkcnf( |
||
| 139 | array( |
||
| 140 | 'count-users' |
||
| 141 | ) |
||
| 142 | ); |
||
| 143 | |||
| 144 | // can a list of available groups be retrieved? |
||
| 145 | $this->cando['getGroups'] = $this->_chkcnf( |
||
| 146 | array( |
||
| 147 | 'select-groups' |
||
| 148 | ) |
||
| 149 | ); |
||
| 150 | |||
| 151 | $this->success = true; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Check user+password |
||
| 156 | * |
||
| 157 | * @param string $user the user name |
||
| 158 | * @param string $pass the clear text password |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | public function checkPass($user, $pass) { |
||
| 162 | |||
| 163 | $userdata = $this->_selectUser($user); |
||
| 164 | if($userdata == false) return false; |
||
| 165 | |||
| 166 | // password checking done in SQL? |
||
| 167 | if($this->_chkcnf(array('check-pass'))) { |
||
| 168 | $userdata['clear'] = $pass; |
||
| 169 | $userdata['hash'] = auth_cryptPassword($pass); |
||
| 170 | $result = $this->_query($this->getConf('check-pass'), $userdata); |
||
|
|
|||
| 171 | if($result === false) return false; |
||
| 172 | return (count($result) == 1); |
||
| 173 | } |
||
| 174 | |||
| 175 | // we do password checking on our own |
||
| 176 | if(isset($userdata['hash'])) { |
||
| 177 | // hashed password |
||
| 178 | $passhash = new PassHash(); |
||
| 179 | return $passhash->verify_hash($pass, $userdata['hash']); |
||
| 180 | } else { |
||
| 181 | // clear text password in the database O_o |
||
| 182 | return ($pass === $userdata['clear']); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Return user info |
||
| 188 | * |
||
| 189 | * Returns info about the given user needs to contain |
||
| 190 | * at least these fields: |
||
| 191 | * |
||
| 192 | * name string full name of the user |
||
| 193 | * mail string email addres of the user |
||
| 194 | * grps array list of groups the user is in |
||
| 195 | * |
||
| 196 | * @param string $user the user name |
||
| 197 | * @param bool $requireGroups whether or not the returned data must include groups |
||
| 198 | * @return array|bool containing user data or false |
||
| 199 | */ |
||
| 200 | public function getUserData($user, $requireGroups = true) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Create a new User [implement only where required/possible] |
||
| 217 | * |
||
| 218 | * Returns false if the user already exists, null when an error |
||
| 219 | * occurred and true if everything went well. |
||
| 220 | * |
||
| 221 | * The new user HAS TO be added to the default group by this |
||
| 222 | * function! |
||
| 223 | * |
||
| 224 | * Set addUser capability when implemented |
||
| 225 | * |
||
| 226 | * @param string $user |
||
| 227 | * @param string $clear |
||
| 228 | * @param string $name |
||
| 229 | * @param string $mail |
||
| 230 | * @param null|array $grps |
||
| 231 | * @return bool|null |
||
| 232 | */ |
||
| 233 | public function createUser($user, $clear, $name, $mail, $grps = null) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Modify user data |
||
| 286 | * |
||
| 287 | * @param string $user nick of the user to be changed |
||
| 288 | * @param array $changes array of field/value pairs to be changed (password will be clear text) |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public function modifyUser($user, $changes) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Delete one or more users |
||
| 376 | * |
||
| 377 | * Set delUser capability when implemented |
||
| 378 | * |
||
| 379 | * @param array $users |
||
| 380 | * @return int number of users deleted |
||
| 381 | */ |
||
| 382 | public function deleteUsers($users) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Bulk retrieval of user data [implement only where required/possible] |
||
| 392 | * |
||
| 393 | * Set getUsers capability when implemented |
||
| 394 | * |
||
| 395 | * @param int $start index of first user to be returned |
||
| 396 | * @param int $limit max number of users to be returned |
||
| 397 | * @param array $filter array of field/pattern pairs, null for no filter |
||
| 398 | * @return array list of userinfo (refer getUserData for internal userinfo details) |
||
| 399 | */ |
||
| 400 | public function retrieveUsers($start = 0, $limit = -1, $filter = null) { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Return a count of the number of user which meet $filter criteria |
||
| 431 | * |
||
| 432 | * @param array $filter array of field/pattern pairs, empty array for no filter |
||
| 433 | * @return int |
||
| 434 | */ |
||
| 435 | public function getUserCount($filter = array()) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Create a new group with the given name |
||
| 456 | * |
||
| 457 | * @param string $group |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public function addGroup($group) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Retrieve groups |
||
| 471 | * |
||
| 472 | * Set getGroups capability when implemented |
||
| 473 | * |
||
| 474 | * @param int $start |
||
| 475 | * @param int $limit |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | public function retrieveGroups($start = 0, $limit = 0) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Select data of a specified user |
||
| 491 | * |
||
| 492 | * @param string $user the user name |
||
| 493 | * @return bool|array user data, false on error |
||
| 494 | */ |
||
| 495 | protected function _selectUser($user) { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Delete a user after removing all their group memberships |
||
| 532 | * |
||
| 533 | * @param string $user |
||
| 534 | * @return bool true when the user was deleted |
||
| 535 | */ |
||
| 536 | protected function _deleteUser($user) { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Select all groups of a user |
||
| 563 | * |
||
| 564 | * @param array $userdata The userdata as returned by _selectUser() |
||
| 565 | * @return array|bool list of group names, false on error |
||
| 566 | */ |
||
| 567 | protected function _selectUserGroups($userdata) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Select all available groups |
||
| 589 | * |
||
| 590 | * @return array|bool list of all available groups and their properties |
||
| 591 | */ |
||
| 592 | protected function _selectGroups() { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Remove all entries from the group cache |
||
| 617 | */ |
||
| 618 | protected function _clearGroupCache() { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Adds the user to the group |
||
| 624 | * |
||
| 625 | * @param array $userdata all the user data |
||
| 626 | * @param array $groupdata all the group data |
||
| 627 | * @return bool |
||
| 628 | */ |
||
| 629 | protected function _joinGroup($userdata, $groupdata) { |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Removes the user from the group |
||
| 639 | * |
||
| 640 | * @param array $userdata all the user data |
||
| 641 | * @param array $groupdata all the group data |
||
| 642 | * @return bool |
||
| 643 | */ |
||
| 644 | protected function _leaveGroup($userdata, $groupdata) { |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Executes a query |
||
| 654 | * |
||
| 655 | * @param string $sql The SQL statement to execute |
||
| 656 | * @param array $arguments Named parameters to be used in the statement |
||
| 657 | * @return array|int|bool The result as associative array for SELECTs, affected rows for others, false on error |
||
| 658 | */ |
||
| 659 | protected function _query($sql, $arguments = array()) { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Wrapper around msg() but outputs only when debug is enabled |
||
| 708 | * |
||
| 709 | * @param string|Exception $message |
||
| 710 | * @param int $err |
||
| 711 | * @param int $line |
||
| 712 | */ |
||
| 713 | protected function _debug($message, $err = 0, $line = 0) { |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Check if the given config strings are set |
||
| 732 | * |
||
| 733 | * @author Matthias Grimm <[email protected]> |
||
| 734 | * |
||
| 735 | * @param string[] $keys |
||
| 736 | * @return bool |
||
| 737 | */ |
||
| 738 | protected function _chkcnf($keys) { |
||
| 754 | |||
| 755 | /** |
||
| 756 | * create an approximation of the SQL string with parameters replaced |
||
| 757 | * |
||
| 758 | * @param string $sql |
||
| 759 | * @param array $params |
||
| 760 | * @param bool $htmlescape Should the result be escaped for output in HTML? |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | protected function _debugSQL($sql, $params, $htmlescape = true) { |
||
| 779 | } |
||
| 780 | |||
| 782 |
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.