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 | * @access public |
||
| 116 | * @param Wiki $wikiClass |
||
| 117 | * @param mixed $pgUsername Username |
||
| 118 | * @throws AssertFailure |
||
| 119 | * @throws LoggedOut |
||
| 120 | * @throws MWAPIError |
||
| 121 | */ |
||
| 122 | public function __construct( Wiki &$wikiClass, $pgUsername ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Creates the account with the specified parameters |
||
| 202 | * |
||
| 203 | * @access public |
||
| 204 | * @param string $password Password (ignored if mailpassword is set). Default null. |
||
| 205 | * @param string $email Email address of user (optional). Default null. |
||
| 206 | * @param bool $mailpassword If set to true, a random password will be emailed to the user. Default false. |
||
| 207 | * @param string $reason Optional reason for creating the account to be put in the logs. Default null. |
||
| 208 | * @param string $realname Real name of user (optional). Default null. |
||
| 209 | * @param bool $tboverride Override the title blacklist. Requires the tboverride right. Default false. |
||
|
|
|||
| 210 | * @param string $language Language code to set as default for the user (optional, defaults to content language). Default null. |
||
| 211 | * @param string $domain Domain for external authentication (optional). Default null. |
||
| 212 | * @return bool True on success, false otherwise |
||
| 213 | */ |
||
| 214 | public function create( $password = null, $email = null, $mailpassword = false, $reason = null, $realname = null, $language = null, $domain = null ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns whether or not the user is blocked |
||
| 289 | * |
||
| 290 | * @access public |
||
| 291 | * @param bool $force Whether or not to use the locally stored cache. Default false. |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | public function is_blocked( $force = false ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * get_blockinfo function. |
||
| 309 | * |
||
| 310 | * @access public |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public function get_blockinfo() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * is_ip function. |
||
| 319 | * |
||
| 320 | * @access public |
||
| 321 | * @return boolean |
||
| 322 | */ |
||
| 323 | public function is_ip() { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Blocks the user |
||
| 329 | * |
||
| 330 | * @access public |
||
| 331 | * @param string $reason Reason for blocking. Default null |
||
| 332 | * @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. |
||
| 333 | * @param array $params Parameters to set. Options are anononly, nocreate, autoblock, noemail, hidename, noallowusertalk. Defdault array(). |
||
| 334 | * @param bool $watch Watch the user/IP's user and talk pages. Default false. |
||
| 335 | * @param int $range The number of CIDR prefix bits to use for a rangeblock. Default null. |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function block( $reason = null, $expiry = 'indefinite', $params = array(), $watch = false, $range = null ) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Unblocks the user, or a block ID |
||
| 437 | * |
||
| 438 | * @access public |
||
| 439 | * @param string $reason Reason for unblocking. Default null |
||
| 440 | * @param int $id Block ID to unblock. Default null |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | public function unblock( $reason = null, $id = null ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Returns the editcount of the user |
||
| 498 | * |
||
| 499 | * @access public |
||
| 500 | * @param bool $force Whether or not to use the locally stored cache. Default false. |
||
| 501 | * @param Database &$database Use an instance of the mysqli class to get a more accurate count |
||
| 502 | * @param bool $liveonly Whether or not to only get the live edit count. Only works with $database. Default false. |
||
| 503 | * @return int Edit count |
||
| 504 | */ |
||
| 505 | public function get_editcount( $force = false, &$database = null, $liveonly = false ) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Returns a list of all user contributions |
||
| 537 | * |
||
| 538 | * @access public |
||
| 539 | * @param bool $mostrecentfirst Set to true to get the most recent edits first. Default true. |
||
| 540 | * @param bool $limit Only get this many edits. Default null. |
||
| 541 | * @return array Array, first level indexed, second level associative with keys user, pageid, revid, ns, title, timestamp, size and comment (edit summary). |
||
| 542 | */ |
||
| 543 | public function get_contribs( $mostrecentfirst = true, $limit = null ) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Returns whether or not the user has email enabled |
||
| 569 | * |
||
| 570 | * @access public |
||
| 571 | * @return bool |
||
| 572 | */ |
||
| 573 | public function has_email() { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Returns the usergroups, NULL if user is IP. |
||
| 579 | * |
||
| 580 | * @access public |
||
| 581 | * @param bool force Force use of the API. Default false; |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | public function get_usergroups( $force = false ) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Returns date the user registered |
||
| 603 | * |
||
| 604 | * @access public |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | public function get_registration() { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Returns whether or not the user exists |
||
| 613 | * |
||
| 614 | * @access public |
||
| 615 | * @return bool |
||
| 616 | */ |
||
| 617 | public function exists() { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Returns the raw username |
||
| 623 | * |
||
| 624 | * @access public |
||
| 625 | * @return string |
||
| 626 | */ |
||
| 627 | public function username() { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Send an email to another wiki user |
||
| 633 | * |
||
| 634 | * @access public |
||
| 635 | * @param string $text Text to send |
||
| 636 | * @param string $subject Subject of email. Default 'Wikipedia Email' |
||
| 637 | * @param bool $ccme Whether or not to send a copy of the email to "myself". Default false. |
||
| 638 | * @throws EmailError |
||
| 639 | * @return bool True on success, false otherwise. |
||
| 640 | */ |
||
| 641 | public function email( $text = null, $subject = "Wikipedia Email", $ccme = false ) { |
||
| 688 | |||
| 689 | public function userrights( $add = array(), $remove = array(), $reason = '' ) { |
||
| 731 | |||
| 732 | /** |
||
| 733 | * List all deleted contributions. |
||
| 734 | * The logged in user must have the 'deletedhistory' right |
||
| 735 | * |
||
| 736 | * @access public |
||
| 737 | * @param bool $content Whether or not to return content of each contribution. Default false |
||
| 738 | * @param string $start Timestamp to start at. Default null. |
||
| 739 | * @param string $end Timestamp to end at. Default null. |
||
| 740 | * @param string $dir Direction to list. Default 'older' |
||
| 741 | * @param array $prop Information to retrieve. Default array( 'revid', 'user', 'parsedcomment', 'minor', 'len', 'content', 'token' ) |
||
| 742 | * @return array |
||
| 743 | */ |
||
| 744 | public function deletedcontribs( $content = false, $start = null, $end = null, $dir = 'older', $prop = array( |
||
| 771 | |||
| 772 | /* |
||
| 773 | * Performs new message checking, etc |
||
| 774 | * |
||
| 775 | * @access public |
||
| 776 | * @return void |
||
| 777 | */ |
||
| 778 | protected function preEditChecks( $action = "Edit" ) { |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Returns a page class for the userpage |
||
| 784 | * |
||
| 785 | * @return Page |
||
| 786 | */ |
||
| 787 | public function &getPageclass() { |
||
| 791 | |||
| 792 | } |
||
| 793 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.