Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class User { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Wiki class |
||
| 32 | * |
||
| 33 | * @var Wiki |
||
| 34 | * @access protected |
||
| 35 | */ |
||
| 36 | protected $wiki; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Username |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | * @access protected |
||
| 43 | */ |
||
| 44 | protected $username; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Whether or not user exists |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | * @access protected |
||
| 51 | */ |
||
| 52 | protected $exists = true; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Whether or not user is blocked |
||
| 56 | * |
||
| 57 | * @var bool |
||
| 58 | * @access protected |
||
| 59 | */ |
||
| 60 | protected $blocked; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Array of block parameters |
||
| 64 | * |
||
| 65 | * (default value: array()) |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | * @access protected |
||
| 69 | */ |
||
| 70 | protected $blockinfo = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Rough estimate as to number of edits |
||
| 74 | * |
||
| 75 | * @var int |
||
| 76 | * @access protected |
||
| 77 | */ |
||
| 78 | protected $editcount; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * List of groups user is a member of |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | * @access protected |
||
| 85 | */ |
||
| 86 | protected $groups; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Whether or not user is an IP |
||
| 90 | * |
||
| 91 | * @var bool |
||
| 92 | * @access protected |
||
| 93 | */ |
||
| 94 | protected $ip = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Whether or not user has email enabled |
||
| 98 | * |
||
| 99 | * @var bool |
||
| 100 | * @access protected |
||
| 101 | */ |
||
| 102 | protected $hasemail = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Date the user registered |
||
| 106 | * |
||
| 107 | * @var string |
||
| 108 | * @access protected |
||
| 109 | */ |
||
| 110 | protected $registration; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Construction method for the User class |
||
| 114 | * |
||
| 115 | * @fixme Return in constructor method. |
||
| 116 | * |
||
| 117 | * @access public |
||
| 118 | * @param Wiki $wikiClass |
||
| 119 | * @param mixed $pgUsername Username |
||
| 120 | * @throws AssertFailure |
||
| 121 | * @throws LoggedOut |
||
| 122 | * @throws MWAPIError |
||
| 123 | */ |
||
| 124 | public function __construct( Wiki &$wikiClass, $pgUsername ) { |
||
| 125 | |||
| 126 | $this->wiki = & $wikiClass; |
||
| 127 | |||
| 128 | pecho( "Getting user information for $pgUsername...\n\n", PECHO_NORMAL ); |
||
| 129 | $uiProps = array( |
||
| 130 | 'action' => 'query', |
||
| 131 | 'list' => 'users|blocks', |
||
| 132 | 'ususers' => $pgUsername, |
||
| 133 | 'usprop' => 'editcount|groups|blockinfo|emailable|registration' |
||
| 134 | ); |
||
| 135 | if(is_numeric(ip2long( $pgUsername))) { |
||
| 136 | $uiProps['bkip'] = $pgUsername; |
||
| 137 | } else { |
||
| 138 | $uiProps['bkusers'] = $pgUsername; |
||
| 139 | } |
||
| 140 | $uiRes = $this->wiki->apiQuery( $uiProps ); |
||
| 141 | |||
| 142 | if ( !$uiRes ) { |
||
| 143 | $this->username = $pgUsername; |
||
| 144 | $this->exists = false; |
||
| 145 | } else { |
||
| 146 | $this->exists = true; |
||
| 147 | } |
||
| 148 | |||
| 149 | $this->username = $uiRes['query']['users'][0]['name']; |
||
| 150 | |||
| 151 | if(is_numeric(ip2long( $pgUsername))) { |
||
| 152 | $this->exists = false; |
||
| 153 | $this->ip = true; |
||
| 154 | |||
| 155 | if( isset( $uiRes['query']['blocks'][0]['expiry'] ) && isset($uiRes['query']['blocks'][0])) { |
||
| 156 | $this->blocked = true; |
||
| 157 | $this->blockinfo = array( |
||
| 158 | 'by' => $uiRes['query']['blocks'][0]['by'], |
||
| 159 | 'when' => $uiRes['query']['blocks'][0]['timestamp'], |
||
| 160 | 'reason' => $uiRes['query']['blocks'][0]['reason'], |
||
| 161 | 'expiry' => $uiRes['query']['blocks'][0]['expiry'] |
||
| 162 | ); |
||
| 163 | } else { |
||
| 164 | $this->blocked = false; |
||
| 165 | $this->blockinfo = array(); |
||
| 166 | } |
||
| 167 | } elseif( isset( $uiRes['query']['users'][0]['missing'] ) || isset( $uiRes['query']['users'][0]['invalid'] ) ) { |
||
| 168 | $this->exists = false; |
||
| 169 | |||
| 170 | return false; |
||
|
|
|||
| 171 | } else { |
||
| 172 | $this->editcount = $uiRes['query']['users'][0]['editcount']; |
||
| 173 | |||
| 174 | if( isset( $uiRes['query']['users'][0]['groups'] ) ) { |
||
| 175 | $this->groups = $uiRes['query']['users'][0]['groups']; |
||
| 176 | } |
||
| 177 | |||
| 178 | if( isset( $uiRes['query']['blocks'][0]['expiry'] ) && isset($uiRes['query']['blocks'][0])) { |
||
| 179 | $this->blocked = true; |
||
| 180 | $this->blockinfo = array( |
||
| 181 | 'by' => $uiRes['query']['blocks'][0]['by'], |
||
| 182 | 'when' => $uiRes['query']['blocks'][0]['timestamp'], |
||
| 183 | 'reason' => $uiRes['query']['blocks'][0]['reason'], |
||
| 184 | 'expiry' => $uiRes['query']['blocks'][0]['expiry'] |
||
| 185 | ); |
||
| 186 | } else { |
||
| 187 | $this->blocked = false; |
||
| 188 | $this->blockinfo = array(); |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | if( isset( $uiRes['query']['users'][0]['emailable'] ) ) { |
||
| 193 | $this->hasemail = true; |
||
| 194 | } |
||
| 195 | |||
| 196 | if( isset( $uiRes['query']['users'][0]['registration'] ) ) { |
||
| 197 | $this->registration = $uiRes['query']['users'][0]['registration']; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Creates the account with the specified parameters |
||
| 204 | * |
||
| 205 | * @access public |
||
| 206 | * @param string $password Password (ignored if mailpassword is set). Default null. |
||
| 207 | * @param string $email Email address of user (optional). Default null. |
||
| 208 | * @param bool $mailpassword If set to true, a random password will be emailed to the user. Default false. |
||
| 209 | * @param string $reason Optional reason for creating the account to be put in the logs. Default null. |
||
| 210 | * @param string $realname Real name of user (optional). Default null. |
||
| 211 | * @param bool $tboverride Override the title blacklist. Requires the tboverride right. Default false. |
||
| 212 | * @param string $language Language code to set as default for the user (optional, defaults to content language). Default null. |
||
| 213 | * @param string $domain Domain for external authentication (optional). Default null. |
||
| 214 | * @return bool True on success, false otherwise |
||
| 215 | */ |
||
| 216 | public function create( $password = null, $email = null, $mailpassword = false, $reason = null, $realname = null, $language = null, $domain = null ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Returns whether or not the user is blocked |
||
| 291 | * |
||
| 292 | * @access public |
||
| 293 | * @param bool $force Whether or not to use the locally stored cache. Default false. |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function is_blocked( $force = false ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * get_blockinfo function. |
||
| 311 | * |
||
| 312 | * @access public |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | public function get_blockinfo() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * is_ip function. |
||
| 321 | * |
||
| 322 | * @access public |
||
| 323 | * @return boolean |
||
| 324 | */ |
||
| 325 | public function is_ip() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Blocks the user |
||
| 331 | * |
||
| 332 | * @access public |
||
| 333 | * @param string $reason Reason for blocking. Default null |
||
| 334 | * @param string $expiry Expiry. Can be a date, {@link http://www.gnu.org/software/tar/manual/html_node/Date-input-formats.html GNU formatted date}, indefinite, or anything else that MediaWiki accepts. Default indefinite. |
||
| 335 | * @param array $params Parameters to set. Options are anononly, nocreate, autoblock, noemail, hidename, noallowusertalk. Defdault array(). |
||
| 336 | * @param bool $watch Watch the user/IP's user and talk pages. Default false. |
||
| 337 | * @param int $range The number of CIDR prefix bits to use for a rangeblock. Default null. |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | public function block( $reason = null, $expiry = 'indefinite', $params = array(), $watch = false, $range = null ) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Unblocks the user, or a block ID |
||
| 439 | * |
||
| 440 | * @access public |
||
| 441 | * @param string $reason Reason for unblocking. Default null |
||
| 442 | * @param int $id Block ID to unblock. Default null |
||
| 443 | * @return bool |
||
| 444 | */ |
||
| 445 | public function unblock( $reason = null, $id = null ) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Returns the editcount of the user |
||
| 500 | * |
||
| 501 | * @access public |
||
| 502 | * @param bool $force Whether or not to use the locally stored cache. Default false. |
||
| 503 | * @param Database &$database Use an instance of the mysqli class to get a more accurate count |
||
| 504 | * @param bool $liveonly Whether or not to only get the live edit count. Only works with $database. Default false. |
||
| 505 | * @return int Edit count |
||
| 506 | */ |
||
| 507 | public function get_editcount( $force = false, &$database = null, $liveonly = false ) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns a list of all user contributions |
||
| 539 | * |
||
| 540 | * @access public |
||
| 541 | * @param bool $mostrecentfirst Set to true to get the most recent edits first. Default true. |
||
| 542 | * @param bool $limit Only get this many edits. Default null. |
||
| 543 | * @return array Array, first level indexed, second level associative with keys user, pageid, revid, ns, title, timestamp, size and comment (edit summary). |
||
| 544 | */ |
||
| 545 | public function get_contribs( $mostrecentfirst = true, $limit = null ) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Returns whether or not the user has email enabled |
||
| 571 | * |
||
| 572 | * @access public |
||
| 573 | * @return bool |
||
| 574 | */ |
||
| 575 | public function has_email() { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Returns the usergroups, NULL if user is IP. |
||
| 581 | * |
||
| 582 | * @access public |
||
| 583 | * @param bool force Force use of the API. Default false; |
||
| 584 | * @return array |
||
| 585 | */ |
||
| 586 | public function get_usergroups( $force = false ) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Returns date the user registered |
||
| 605 | * |
||
| 606 | * @access public |
||
| 607 | * @return string |
||
| 608 | */ |
||
| 609 | public function get_registration() { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Returns whether or not the user exists |
||
| 615 | * |
||
| 616 | * @access public |
||
| 617 | * @return bool |
||
| 618 | */ |
||
| 619 | public function exists() { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Returns the raw username |
||
| 625 | * |
||
| 626 | * @access public |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | public function username() { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Send an email to another wiki user |
||
| 635 | * |
||
| 636 | * @access public |
||
| 637 | * @param string $text Text to send |
||
| 638 | * @param string $subject Subject of email. Default 'Wikipedia Email' |
||
| 639 | * @param bool $ccme Whether or not to send a copy of the email to "myself". Default false. |
||
| 640 | * @throws EmailError |
||
| 641 | * @return bool True on success, false otherwise. |
||
| 642 | */ |
||
| 643 | public function email( $text = null, $subject = "Wikipedia Email", $ccme = false ) { |
||
| 690 | |||
| 691 | public function userrights( $add = array(), $remove = array(), $reason = '' ) { |
||
| 733 | |||
| 734 | /** |
||
| 735 | * List all deleted contributions. |
||
| 736 | * The logged in user must have the 'deletedhistory' right |
||
| 737 | * |
||
| 738 | * @access public |
||
| 739 | * @param bool $content Whether or not to return content of each contribution. Default false |
||
| 740 | * @param string $start Timestamp to start at. Default null. |
||
| 741 | * @param string $end Timestamp to end at. Default null. |
||
| 742 | * @param string $dir Direction to list. Default 'older' |
||
| 743 | * @param array $prop Information to retrieve. Default array( 'revid', 'user', 'parsedcomment', 'minor', 'len', 'content', 'token' ) |
||
| 744 | * @return array |
||
| 745 | */ |
||
| 746 | public function deletedcontribs( $content = false, $start = null, $end = null, $dir = 'older', $prop = array( |
||
| 773 | |||
| 774 | /* |
||
| 775 | * Performs new message checking, etc |
||
| 776 | * |
||
| 777 | * @access public |
||
| 778 | * @return void |
||
| 779 | */ |
||
| 780 | protected function preEditChecks( $action = "Edit" ) { |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Returns a page class for the userpage |
||
| 786 | * |
||
| 787 | * @return Page |
||
| 788 | */ |
||
| 789 | public function &getPageclass() { |
||
| 793 | |||
| 794 | } |
||
| 795 |