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 ) { |
||
211 | |||
212 | /** |
||
213 | * Creates the account with the specified parameters |
||
214 | * |
||
215 | * @access public |
||
216 | * @param string $password Password (ignored if mailpassword is set). Default null. |
||
217 | * @param string $email Email address of user (optional). Default null. |
||
218 | * @param bool $mailpassword If set to true, a random password will be emailed to the user. Default false. |
||
219 | * @param string $reason Optional reason for creating the account to be put in the logs. Default null. |
||
220 | * @param string $realname Real name of user (optional). Default null. |
||
221 | * @param bool $tboverride Override the title blacklist. Requires the tboverride right. Default false. |
||
222 | * @param string $language Language code to set as default for the user (optional, defaults to content language). Default null. |
||
223 | * @param string $domain Domain for external authentication (optional). Default null. |
||
224 | * @return bool True on success, false otherwise |
||
225 | */ |
||
226 | public function create( $password = null, $email = null, $mailpassword = false, $reason = null, $realname = null, $language = null, $domain = null ) { |
||
298 | |||
299 | /** |
||
300 | * Returns whether or not the user is blocked |
||
301 | * |
||
302 | * @access public |
||
303 | * @param bool $force Whether or not to use the locally stored cache. Default false. |
||
304 | * @return bool |
||
305 | */ |
||
306 | public function is_blocked( $force = false ) { |
||
318 | |||
319 | /** |
||
320 | * get_blockinfo function. |
||
321 | * |
||
322 | * @access public |
||
323 | * @return array |
||
324 | */ |
||
325 | public function get_blockinfo() { |
||
328 | |||
329 | /** |
||
330 | * is_ip function. |
||
331 | * |
||
332 | * @access public |
||
333 | * @return boolean |
||
334 | */ |
||
335 | public function is_ip() { |
||
338 | |||
339 | /** |
||
340 | * Blocks the user |
||
341 | * |
||
342 | * @access public |
||
343 | * @param string $reason Reason for blocking. Default null |
||
344 | * @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. |
||
345 | * @param array $params Parameters to set. Options are anononly, nocreate, autoblock, noemail, hidename, noallowusertalk. Defdault array(). |
||
346 | * @param bool $watch Watch the user/IP's user and talk pages. Default false. |
||
347 | * @param int $range The number of CIDR prefix bits to use for a rangeblock. Default null. |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function block( $reason = null, $expiry = 'indefinite', $params = array(), $watch = false, $range = null ) { |
||
446 | |||
447 | /** |
||
448 | * Unblocks the user, or a block ID |
||
449 | * |
||
450 | * @access public |
||
451 | * @param string $reason Reason for unblocking. Default null |
||
452 | * @param int $id Block ID to unblock. Default null |
||
453 | * @return bool |
||
454 | */ |
||
455 | public function unblock( $reason = null, $id = null ) { |
||
507 | |||
508 | /** |
||
509 | * Returns the editcount of the user |
||
510 | * |
||
511 | * @access public |
||
512 | * @param bool $force Whether or not to use the locally stored cache. Default false. |
||
513 | * @param Database &$database Use an instance of the mysqli class to get a more accurate count |
||
514 | * @param bool $liveonly Whether or not to only get the live edit count. Only works with $database. Default false. |
||
515 | * @return int Edit count |
||
516 | */ |
||
517 | public function get_editcount( $force = false, &$database = null, $liveonly = false ) { |
||
546 | |||
547 | /** |
||
548 | * Returns a list of all user contributions |
||
549 | * |
||
550 | * @access public |
||
551 | * @param bool $mostrecentfirst Set to true to get the most recent edits first. Default true. |
||
552 | * @param bool $limit Only get this many edits. Default null. |
||
553 | * @return array Array, first level indexed, second level associative with keys user, pageid, revid, ns, title, timestamp, size and comment (edit summary). |
||
554 | */ |
||
555 | public function get_contribs( $mostrecentfirst = true, $limit = null ) { |
||
578 | |||
579 | /** |
||
580 | * Returns whether or not the user has email enabled |
||
581 | * |
||
582 | * @access public |
||
583 | * @return bool |
||
584 | */ |
||
585 | public function has_email() { |
||
588 | |||
589 | /** |
||
590 | * Returns the usergroups, NULL if user is IP. |
||
591 | * |
||
592 | * @access public |
||
593 | * @param bool force Force use of the API. Default false; |
||
594 | * @return array |
||
595 | */ |
||
596 | public function get_usergroups( $force = false ) { |
||
612 | |||
613 | /** |
||
614 | * Returns date the user registered |
||
615 | * |
||
616 | * @access public |
||
617 | * @return string |
||
618 | */ |
||
619 | public function get_registration() { |
||
622 | |||
623 | /** |
||
624 | * Returns whether or not the user exists |
||
625 | * |
||
626 | * @access public |
||
627 | * @return bool |
||
628 | */ |
||
629 | public function exists() { |
||
632 | |||
633 | /** |
||
634 | * Returns the raw username |
||
635 | * |
||
636 | * @access public |
||
637 | * @return string |
||
638 | */ |
||
639 | public function username() { |
||
642 | |||
643 | /** |
||
644 | * Send an email to another wiki user |
||
645 | * |
||
646 | * @access public |
||
647 | * @param string $text Text to send |
||
648 | * @param string $subject Subject of email. Default 'Wikipedia Email' |
||
649 | * @param bool $ccme Whether or not to send a copy of the email to "myself". Default false. |
||
650 | * @throws EmailError |
||
651 | * @return bool True on success, false otherwise. |
||
652 | */ |
||
653 | public function email( $text = null, $subject = "Wikipedia Email", $ccme = false ) { |
||
700 | |||
701 | public function userrights( $add = array(), $remove = array(), $reason = '' ) { |
||
743 | |||
744 | /** |
||
745 | * List all deleted contributions. |
||
746 | * The logged in user must have the 'deletedhistory' right |
||
747 | * |
||
748 | * @access public |
||
749 | * @param bool $content Whether or not to return content of each contribution. Default false |
||
750 | * @param string $start Timestamp to start at. Default null. |
||
751 | * @param string $end Timestamp to end at. Default null. |
||
752 | * @param string $dir Direction to list. Default 'older' |
||
753 | * @param array $prop Information to retrieve. Default array( 'revid', 'user', 'parsedcomment', 'minor', 'len', 'content', 'token' ) |
||
754 | * @return array |
||
755 | */ |
||
756 | public function deletedcontribs( $content = false, $start = null, $end = null, $dir = 'older', $prop = array( |
||
783 | |||
784 | /* |
||
785 | * Performs new message checking, etc |
||
786 | * |
||
787 | * @access public |
||
788 | * @return void |
||
789 | */ |
||
790 | protected function preEditChecks( $action = "Edit" ) { |
||
793 | |||
794 | /** |
||
795 | * Returns a page class for the userpage |
||
796 | * |
||
797 | * @return Page |
||
798 | */ |
||
799 | public function &getPageclass() { |
||
803 | |||
804 | } |
||
805 |