Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like UserrightsPage 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 UserrightsPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class UserrightsPage extends SpecialPage { |
||
| 30 | # The target of the local right-adjuster's interest. Can be gotten from |
||
| 31 | # either a GET parameter or a subpage-style parameter, so have a member |
||
| 32 | # variable for it. |
||
| 33 | protected $mTarget; |
||
| 34 | /* |
||
| 35 | * @var null|User $mFetchedUser The user object of the target username or null. |
||
| 36 | */ |
||
| 37 | protected $mFetchedUser = null; |
||
| 38 | protected $isself = false; |
||
| 39 | |||
| 40 | public function __construct() { |
||
| 41 | parent::__construct( 'Userrights' ); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function doesWrites() { |
||
| 45 | return true; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function isRestricted() { |
||
| 49 | return true; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function userCanExecute( User $user ) { |
||
| 53 | return $this->userCanChangeRights( $user, false ); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param User $user |
||
| 58 | * @param bool $checkIfSelf |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | public function userCanChangeRights( $user, $checkIfSelf = true ) { |
||
| 62 | $available = $this->changeableGroups(); |
||
| 63 | if ( $user->getId() == 0 ) { |
||
| 64 | return false; |
||
| 65 | } |
||
| 66 | |||
| 67 | return !empty( $available['add'] ) |
||
| 68 | || !empty( $available['remove'] ) |
||
| 69 | || ( ( $this->isself || !$checkIfSelf ) && |
||
| 70 | ( !empty( $available['add-self'] ) |
||
| 71 | || !empty( $available['remove-self'] ) ) ); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Manage forms to be shown according to posted data. |
||
| 76 | * Depending on the submit button used, call a form or a save function. |
||
| 77 | * |
||
| 78 | * @param string|null $par String if any subpage provided, else null |
||
| 79 | * @throws UserBlockedError|PermissionsError |
||
| 80 | */ |
||
| 81 | public function execute( $par ) { |
||
| 82 | // If the visitor doesn't have permissions to assign or remove |
||
| 83 | // any groups, it's a bit silly to give them the user search prompt. |
||
| 84 | |||
| 85 | $user = $this->getUser(); |
||
| 86 | $request = $this->getRequest(); |
||
| 87 | $out = $this->getOutput(); |
||
| 88 | |||
| 89 | /* |
||
| 90 | * If the user is blocked and they only have "partial" access |
||
| 91 | * (e.g. they don't have the userrights permission), then don't |
||
| 92 | * allow them to use Special:UserRights. |
||
| 93 | */ |
||
| 94 | if ( $user->isBlocked() && !$user->isAllowed( 'userrights' ) ) { |
||
| 95 | throw new UserBlockedError( $user->getBlock() ); |
||
|
|
|||
| 96 | } |
||
| 97 | |||
| 98 | View Code Duplication | if ( $par !== null ) { |
|
| 99 | $this->mTarget = $par; |
||
| 100 | } else { |
||
| 101 | $this->mTarget = $request->getVal( 'user' ); |
||
| 102 | } |
||
| 103 | |||
| 104 | $available = $this->changeableGroups(); |
||
| 105 | |||
| 106 | if ( $this->mTarget === null ) { |
||
| 107 | /* |
||
| 108 | * If the user specified no target, and they can only |
||
| 109 | * edit their own groups, automatically set them as the |
||
| 110 | * target. |
||
| 111 | */ |
||
| 112 | if ( !count( $available['add'] ) && !count( $available['remove'] ) ) { |
||
| 113 | $this->mTarget = $user->getName(); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | if ( $this->mTarget !== null && User::getCanonicalName( $this->mTarget ) === $user->getName() ) { |
||
| 118 | $this->isself = true; |
||
| 119 | } |
||
| 120 | |||
| 121 | $fetchedStatus = $this->fetchUser( $this->mTarget ); |
||
| 122 | if ( $fetchedStatus->isOK() ) { |
||
| 123 | $this->mFetchedUser = $fetchedStatus->value; |
||
| 124 | if ( $this->mFetchedUser instanceof User ) { |
||
| 125 | // Set the 'relevant user' in the skin, so it displays links like Contributions, |
||
| 126 | // User logs, UserRights, etc. |
||
| 127 | $this->getSkin()->setRelevantUser( $this->mFetchedUser ); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if ( !$this->userCanChangeRights( $user, true ) ) { |
||
| 132 | if ( $this->isself && $request->getCheck( 'success' ) ) { |
||
| 133 | // bug 48609: if the user just removed its own rights, this would |
||
| 134 | // leads it in a "permissions error" page. In that case, show a |
||
| 135 | // message that it can't anymore use this page instead of an error |
||
| 136 | $this->setHeaders(); |
||
| 137 | $out->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>", 'userrights-removed-self' ); |
||
| 138 | $out->returnToMain(); |
||
| 139 | |||
| 140 | return; |
||
| 141 | } |
||
| 142 | |||
| 143 | // @todo FIXME: There may be intermediate groups we can mention. |
||
| 144 | $msg = $user->isAnon() ? 'userrights-nologin' : 'userrights-notallowed'; |
||
| 145 | throw new PermissionsError( null, [ [ $msg ] ] ); |
||
| 146 | } |
||
| 147 | |||
| 148 | // show a successbox, if the user rights was saved successfully |
||
| 149 | if ( $request->getCheck( 'success' ) && $this->mFetchedUser !== null ) { |
||
| 150 | $out->wrapWikiMsg( |
||
| 151 | "<div class=\"successbox\">\n$1\n</div>", |
||
| 152 | [ 'savedrights', $this->mFetchedUser->getName() ] |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->checkReadOnly(); |
||
| 157 | |||
| 158 | $this->setHeaders(); |
||
| 159 | $this->outputHeader(); |
||
| 160 | |||
| 161 | $out->addModuleStyles( 'mediawiki.special' ); |
||
| 162 | $this->addHelpLink( 'Help:Assigning permissions' ); |
||
| 163 | |||
| 164 | // show the general form |
||
| 165 | if ( count( $available['add'] ) || count( $available['remove'] ) ) { |
||
| 166 | $this->switchForm(); |
||
| 167 | } |
||
| 168 | |||
| 169 | if ( |
||
| 170 | $request->wasPosted() && |
||
| 171 | $request->getCheck( 'saveusergroups' ) && |
||
| 172 | $this->mTarget !== null && |
||
| 173 | $user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget ) |
||
| 174 | ) { |
||
| 175 | // save settings |
||
| 176 | if ( !$fetchedStatus->isOK() ) { |
||
| 177 | $this->getOutput()->addWikiText( $fetchedStatus->getWikiText() ); |
||
| 178 | |||
| 179 | return; |
||
| 180 | } |
||
| 181 | |||
| 182 | $targetUser = $this->mFetchedUser; |
||
| 183 | if ( $targetUser instanceof User ) { // UserRightsProxy doesn't have this method (bug 61252) |
||
| 184 | $targetUser->clearInstanceCache(); // bug 38989 |
||
| 185 | } |
||
| 186 | |||
| 187 | if ( $request->getVal( 'conflictcheck-originalgroups' ) |
||
| 188 | !== implode( ',', $targetUser->getGroups() ) |
||
| 189 | ) { |
||
| 190 | $out->addWikiMsg( 'userrights-conflict' ); |
||
| 191 | } else { |
||
| 192 | $this->saveUserGroups( |
||
| 193 | $this->mTarget, |
||
| 194 | $request->getVal( 'user-reason' ), |
||
| 195 | $targetUser |
||
| 196 | ); |
||
| 197 | |||
| 198 | $out->redirect( $this->getSuccessURL() ); |
||
| 199 | |||
| 200 | return; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | // show some more forms |
||
| 205 | if ( $this->mTarget !== null ) { |
||
| 206 | $this->editUserGroupsForm( $this->mTarget ); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | function getSuccessURL() { |
||
| 211 | return $this->getPageTitle( $this->mTarget )->getFullURL( [ 'success' => 1 ] ); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Save user groups changes in the database. |
||
| 216 | * Data comes from the editUserGroupsForm() form function |
||
| 217 | * |
||
| 218 | * @param string $username Username to apply changes to. |
||
| 219 | * @param string $reason Reason for group change |
||
| 220 | * @param User|UserRightsProxy $user Target user object. |
||
| 221 | * @return null |
||
| 222 | */ |
||
| 223 | function saveUserGroups( $username, $reason, $user ) { |
||
| 224 | $allgroups = $this->getAllGroups(); |
||
| 225 | $addgroup = []; |
||
| 226 | $removegroup = []; |
||
| 227 | |||
| 228 | // This could possibly create a highly unlikely race condition if permissions are changed between |
||
| 229 | // when the form is loaded and when the form is saved. Ignoring it for the moment. |
||
| 230 | foreach ( $allgroups as $group ) { |
||
| 231 | // We'll tell it to remove all unchecked groups, and add all checked groups. |
||
| 232 | // Later on, this gets filtered for what can actually be removed |
||
| 233 | if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) { |
||
| 234 | $addgroup[] = $group; |
||
| 235 | } else { |
||
| 236 | $removegroup[] = $group; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason ); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Save user groups changes in the database. |
||
| 245 | * |
||
| 246 | * @param User|UserRightsProxy $user |
||
| 247 | * @param array $add Array of groups to add |
||
| 248 | * @param array $remove Array of groups to remove |
||
| 249 | * @param string $reason Reason for group change |
||
| 250 | * @return array Tuple of added, then removed groups |
||
| 251 | */ |
||
| 252 | function doSaveUserGroups( $user, $add, $remove, $reason = '' ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Add a rights log entry for an action. |
||
| 312 | * @param User $user |
||
| 313 | * @param array $oldGroups |
||
| 314 | * @param array $newGroups |
||
| 315 | * @param array $reason |
||
| 316 | */ |
||
| 317 | function addLogEntry( $user, $oldGroups, $newGroups, $reason ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Edit user groups membership |
||
| 332 | * @param string $username Name of the user. |
||
| 333 | */ |
||
| 334 | function editUserGroupsForm( $username ) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Normalize the input username, which may be local or remote, and |
||
| 355 | * return a user (or proxy) object for manipulating it. |
||
| 356 | * |
||
| 357 | * Side effects: error output for invalid access |
||
| 358 | * @param string $username |
||
| 359 | * @return Status |
||
| 360 | */ |
||
| 361 | public function fetchUser( $username ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @since 1.15 |
||
| 422 | * |
||
| 423 | * @param array $ids |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function makeGroupNameList( $ids ) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Output a form to allow searching for a user |
||
| 437 | */ |
||
| 438 | function switchForm() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Go through used and available groups and return the ones that this |
||
| 479 | * form will be able to manipulate based on the current user's system |
||
| 480 | * permissions. |
||
| 481 | * |
||
| 482 | * @param array $groups List of groups the given user is in |
||
| 483 | * @return array Tuple of addable, then removable groups |
||
| 484 | */ |
||
| 485 | protected function splitGroups( $groups ) { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Show the form to edit group memberships. |
||
| 502 | * |
||
| 503 | * @param User|UserRightsProxy $user User or UserRightsProxy you're editing |
||
| 504 | * @param array $groups Array of groups the user is in |
||
| 505 | */ |
||
| 506 | protected function showEditUserGroupsForm( $user, $groups ) { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Format a link to a group description page |
||
| 615 | * |
||
| 616 | * @param string $group |
||
| 617 | * @return string |
||
| 618 | */ |
||
| 619 | private static function buildGroupLink( $group ) { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Format a link to a group member description page |
||
| 625 | * |
||
| 626 | * @param string $group |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | private static function buildGroupMemberLink( $group ) { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Returns an array of all groups that may be edited |
||
| 635 | * @return array Array of groups that may be edited. |
||
| 636 | */ |
||
| 637 | protected static function getAllGroups() { |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Adds a table with checkboxes where you can select what groups to add/remove |
||
| 643 | * |
||
| 644 | * @todo Just pass the username string? |
||
| 645 | * @param array $usergroups Groups the user belongs to |
||
| 646 | * @param User $user |
||
| 647 | * @return string XHTML table element with checkboxes |
||
| 648 | */ |
||
| 649 | private function groupCheckboxes( $usergroups, $user ) { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @param string $group The name of the group to check |
||
| 727 | * @return bool Can we remove the group? |
||
| 728 | */ |
||
| 729 | View Code Duplication | private function canRemove( $group ) { |
|
| 738 | |||
| 739 | /** |
||
| 740 | * @param string $group The name of the group to check |
||
| 741 | * @return bool Can we add the group? |
||
| 742 | */ |
||
| 743 | View Code Duplication | private function canAdd( $group ) { |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Returns $this->getUser()->changeableGroups() |
||
| 754 | * |
||
| 755 | * @return array Array( |
||
| 756 | * 'add' => array( addablegroups ), |
||
| 757 | * 'remove' => array( removablegroups ), |
||
| 758 | * 'add-self' => array( addablegroups to self ), |
||
| 759 | * 'remove-self' => array( removable groups from self ) |
||
| 760 | * ) |
||
| 761 | */ |
||
| 762 | function changeableGroups() { |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Show a rights log fragment for the specified user |
||
| 768 | * |
||
| 769 | * @param User $user User to show log for |
||
| 770 | * @param OutputPage $output OutputPage to use |
||
| 771 | */ |
||
| 772 | protected function showLogFragment( $user, $output ) { |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Return an array of subpages beginning with $search that this special page will accept. |
||
| 780 | * |
||
| 781 | * @param string $search Prefix to search for |
||
| 782 | * @param int $limit Maximum number of results to return (usually 10) |
||
| 783 | * @param int $offset Number of results to skip (usually 0) |
||
| 784 | * @return string[] Matching subpages |
||
| 785 | */ |
||
| 786 | View Code Duplication | public function prefixSearchSubpages( $search, $limit, $offset ) { |
|
| 795 | |||
| 796 | protected function getGroupName() { |
||
| 799 | } |
||
| 800 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: