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 SpecialPageFactory 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 SpecialPageFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class SpecialPageFactory { |
||
| 48 | /** |
||
| 49 | * List of special page names to the subclass of SpecialPage which handles them. |
||
| 50 | */ |
||
| 51 | private static $coreList = [ |
||
| 52 | // Maintenance Reports |
||
| 53 | 'BrokenRedirects' => 'BrokenRedirectsPage', |
||
| 54 | 'Deadendpages' => 'DeadendPagesPage', |
||
| 55 | 'DoubleRedirects' => 'DoubleRedirectsPage', |
||
| 56 | 'Longpages' => 'LongPagesPage', |
||
| 57 | 'Ancientpages' => 'AncientPagesPage', |
||
| 58 | 'Lonelypages' => 'LonelyPagesPage', |
||
| 59 | 'Fewestrevisions' => 'FewestrevisionsPage', |
||
| 60 | 'Withoutinterwiki' => 'WithoutInterwikiPage', |
||
| 61 | 'Protectedpages' => 'SpecialProtectedpages', |
||
| 62 | 'Protectedtitles' => 'SpecialProtectedtitles', |
||
| 63 | 'Shortpages' => 'ShortPagesPage', |
||
| 64 | 'Uncategorizedcategories' => 'UncategorizedCategoriesPage', |
||
| 65 | 'Uncategorizedimages' => 'UncategorizedImagesPage', |
||
| 66 | 'Uncategorizedpages' => 'UncategorizedPagesPage', |
||
| 67 | 'Uncategorizedtemplates' => 'UncategorizedTemplatesPage', |
||
| 68 | 'Unusedcategories' => 'UnusedCategoriesPage', |
||
| 69 | 'Unusedimages' => 'UnusedimagesPage', |
||
| 70 | 'Unusedtemplates' => 'UnusedtemplatesPage', |
||
| 71 | 'Unwatchedpages' => 'UnwatchedpagesPage', |
||
| 72 | 'Wantedcategories' => 'WantedCategoriesPage', |
||
| 73 | 'Wantedfiles' => 'WantedFilesPage', |
||
| 74 | 'Wantedpages' => 'WantedPagesPage', |
||
| 75 | 'Wantedtemplates' => 'WantedTemplatesPage', |
||
| 76 | |||
| 77 | // List of pages |
||
| 78 | 'Allpages' => 'SpecialAllPages', |
||
| 79 | 'Prefixindex' => 'SpecialPrefixindex', |
||
| 80 | 'Categories' => 'SpecialCategories', |
||
| 81 | 'Listredirects' => 'ListredirectsPage', |
||
| 82 | 'PagesWithProp' => 'SpecialPagesWithProp', |
||
| 83 | 'TrackingCategories' => 'SpecialTrackingCategories', |
||
| 84 | |||
| 85 | // Authentication |
||
| 86 | 'Userlogin' => 'SpecialUserLogin', |
||
| 87 | 'Userlogout' => 'SpecialUserLogout', |
||
| 88 | 'CreateAccount' => 'SpecialCreateAccount', |
||
| 89 | 'LinkAccounts' => 'SpecialLinkAccounts', |
||
| 90 | 'UnlinkAccounts' => 'SpecialUnlinkAccounts', |
||
| 91 | 'ChangeCredentials' => 'SpecialChangeCredentials', |
||
| 92 | 'RemoveCredentials' => 'SpecialRemoveCredentials', |
||
| 93 | |||
| 94 | // Users and rights |
||
| 95 | 'Activeusers' => 'SpecialActiveUsers', |
||
| 96 | 'Block' => 'SpecialBlock', |
||
| 97 | 'Unblock' => 'SpecialUnblock', |
||
| 98 | 'BlockList' => 'SpecialBlockList', |
||
| 99 | 'ChangePassword' => 'SpecialChangePassword', |
||
| 100 | 'BotPasswords' => 'SpecialBotPasswords', |
||
| 101 | 'PasswordReset' => 'SpecialPasswordReset', |
||
| 102 | 'DeletedContributions' => 'DeletedContributionsPage', |
||
| 103 | 'Preferences' => 'SpecialPreferences', |
||
| 104 | 'ResetTokens' => 'SpecialResetTokens', |
||
| 105 | 'Contributions' => 'SpecialContributions', |
||
| 106 | 'Listgrouprights' => 'SpecialListGroupRights', |
||
| 107 | 'Listgrants' => 'SpecialListGrants', |
||
| 108 | 'Listusers' => 'SpecialListUsers', |
||
| 109 | 'Listadmins' => 'SpecialListAdmins', |
||
| 110 | 'Listbots' => 'SpecialListBots', |
||
| 111 | 'Userrights' => 'UserrightsPage', |
||
| 112 | 'EditWatchlist' => 'SpecialEditWatchlist', |
||
| 113 | |||
| 114 | // Recent changes and logs |
||
| 115 | 'Newimages' => 'SpecialNewFiles', |
||
| 116 | 'Log' => 'SpecialLog', |
||
| 117 | 'Watchlist' => 'SpecialWatchlist', |
||
| 118 | 'Newpages' => 'SpecialNewpages', |
||
| 119 | 'Recentchanges' => 'SpecialRecentChanges', |
||
| 120 | 'Recentchangeslinked' => 'SpecialRecentChangesLinked', |
||
| 121 | 'Tags' => 'SpecialTags', |
||
| 122 | |||
| 123 | // Media reports and uploads |
||
| 124 | 'Listfiles' => 'SpecialListFiles', |
||
| 125 | 'Filepath' => 'SpecialFilepath', |
||
| 126 | 'MediaStatistics' => 'MediaStatisticsPage', |
||
| 127 | 'MIMEsearch' => 'MIMEsearchPage', |
||
| 128 | 'FileDuplicateSearch' => 'FileDuplicateSearchPage', |
||
| 129 | 'Upload' => 'SpecialUpload', |
||
| 130 | 'UploadStash' => 'SpecialUploadStash', |
||
| 131 | 'ListDuplicatedFiles' => 'ListDuplicatedFilesPage', |
||
| 132 | |||
| 133 | // Data and tools |
||
| 134 | 'ApiSandbox' => 'SpecialApiSandbox', |
||
| 135 | 'Statistics' => 'SpecialStatistics', |
||
| 136 | 'Allmessages' => 'SpecialAllMessages', |
||
| 137 | 'Version' => 'SpecialVersion', |
||
| 138 | 'Lockdb' => 'SpecialLockdb', |
||
| 139 | 'Unlockdb' => 'SpecialUnlockdb', |
||
| 140 | |||
| 141 | // Redirecting special pages |
||
| 142 | 'LinkSearch' => 'LinkSearchPage', |
||
| 143 | 'Randompage' => 'RandomPage', |
||
| 144 | 'RandomInCategory' => 'SpecialRandomInCategory', |
||
| 145 | 'Randomredirect' => 'SpecialRandomredirect', |
||
| 146 | 'Randomrootpage' => 'SpecialRandomrootpage', |
||
| 147 | |||
| 148 | // High use pages |
||
| 149 | 'Mostlinkedcategories' => 'MostlinkedCategoriesPage', |
||
| 150 | 'Mostimages' => 'MostimagesPage', |
||
| 151 | 'Mostinterwikis' => 'MostinterwikisPage', |
||
| 152 | 'Mostlinked' => 'MostlinkedPage', |
||
| 153 | 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage', |
||
| 154 | 'Mostcategories' => 'MostcategoriesPage', |
||
| 155 | 'Mostrevisions' => 'MostrevisionsPage', |
||
| 156 | |||
| 157 | // Page tools |
||
| 158 | 'ComparePages' => 'SpecialComparePages', |
||
| 159 | 'Export' => 'SpecialExport', |
||
| 160 | 'Import' => 'SpecialImport', |
||
| 161 | 'Undelete' => 'SpecialUndelete', |
||
| 162 | 'Whatlinkshere' => 'SpecialWhatLinksHere', |
||
| 163 | 'MergeHistory' => 'SpecialMergeHistory', |
||
| 164 | 'ExpandTemplates' => 'SpecialExpandTemplates', |
||
| 165 | |||
| 166 | // Other |
||
| 167 | 'Booksources' => 'SpecialBookSources', |
||
| 168 | |||
| 169 | // Unlisted / redirects |
||
| 170 | 'ApiHelp' => 'SpecialApiHelp', |
||
| 171 | 'Blankpage' => 'SpecialBlankpage', |
||
| 172 | 'Diff' => 'SpecialDiff', |
||
| 173 | 'EditTags' => 'SpecialEditTags', |
||
| 174 | 'Emailuser' => 'SpecialEmailUser', |
||
| 175 | 'Movepage' => 'MovePageForm', |
||
| 176 | 'Mycontributions' => 'SpecialMycontributions', |
||
| 177 | 'MyLanguage' => 'SpecialMyLanguage', |
||
| 178 | 'Mypage' => 'SpecialMypage', |
||
| 179 | 'Mytalk' => 'SpecialMytalk', |
||
| 180 | 'Myuploads' => 'SpecialMyuploads', |
||
| 181 | 'AllMyUploads' => 'SpecialAllMyUploads', |
||
| 182 | 'PermanentLink' => 'SpecialPermanentLink', |
||
| 183 | 'Redirect' => 'SpecialRedirect', |
||
| 184 | 'Revisiondelete' => 'SpecialRevisionDelete', |
||
| 185 | 'RunJobs' => 'SpecialRunJobs', |
||
| 186 | 'Specialpages' => 'SpecialSpecialpages', |
||
| 187 | ]; |
||
| 188 | |||
| 189 | private static $list; |
||
| 190 | private static $aliases; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Reset the internal list of special pages. Useful when changing $wgSpecialPages after |
||
| 194 | * the internal list has already been initialized, e.g. during testing. |
||
| 195 | */ |
||
| 196 | public static function resetList() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns a list of canonical special page names. |
||
| 203 | * May be used to iterate over all registered special pages. |
||
| 204 | * |
||
| 205 | * @return string[] |
||
| 206 | */ |
||
| 207 | public static function getNames() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the special page list as an array |
||
| 213 | * |
||
| 214 | * @deprecated since 1.24, use getNames() instead. |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | public static function getList() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get the special page list as an array |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | private static function getPageList() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Initialise and return the list of special page aliases. Returns an array where |
||
| 275 | * the key is an alias, and the value is the canonical name of the special page. |
||
| 276 | * All registered special pages are guaranteed to map to themselves. |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | private static function getAliasList() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Given a special page name with a possible subpage, return an array |
||
| 330 | * where the first element is the special page name and the second is the |
||
| 331 | * subpage. |
||
| 332 | * |
||
| 333 | * @param string $alias |
||
| 334 | * @return array Array( String, String|null ), or array( null, null ) if the page is invalid |
||
| 335 | */ |
||
| 336 | public static function resolveAlias( $alias ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Check if a given name exist as a special page or as a special page alias |
||
| 360 | * |
||
| 361 | * @param string $name Name of a special page |
||
| 362 | * @return bool True if a special page exists with this name |
||
| 363 | */ |
||
| 364 | public static function exists( $name ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Find the object with a given name and return it (or NULL) |
||
| 373 | * |
||
| 374 | * @param string $name Special page name, may be localised and/or an alias |
||
| 375 | * @return SpecialPage|null SpecialPage object or null if the page doesn't exist |
||
| 376 | */ |
||
| 377 | public static function getPage( $name ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Return categorised listable special pages which are available |
||
| 423 | * for the current user, and everyone. |
||
| 424 | * |
||
| 425 | * @param User $user User object to check permissions, $wgUser will be used |
||
| 426 | * if not provided |
||
| 427 | * @return array ( string => Specialpage ) |
||
| 428 | */ |
||
| 429 | public static function getUsablePages( User $user = null ) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Return categorised listable special pages for all users |
||
| 452 | * |
||
| 453 | * @return array ( string => Specialpage ) |
||
| 454 | */ |
||
| 455 | public static function getRegularPages() { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Return categorised listable special pages which are available |
||
| 469 | * for the current user, but not for everyone |
||
| 470 | * |
||
| 471 | * @param User|null $user User object to use or null for $wgUser |
||
| 472 | * @return array ( string => Specialpage ) |
||
| 473 | */ |
||
| 474 | public static function getRestrictedPages( User $user = null ) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Execute a special page path. |
||
| 496 | * The path may contain parameters, e.g. Special:Name/Params |
||
| 497 | * Extracts the special page name and call the execute method, passing the parameters |
||
| 498 | * |
||
| 499 | * Returns a title object if the page is redirected, false if there was no such special |
||
| 500 | * page, and true if it was successful. |
||
| 501 | * |
||
| 502 | * @param Title $title |
||
| 503 | * @param IContextSource $context |
||
| 504 | * @param bool $including Bool output is being captured for use in {{special:whatever}} |
||
| 505 | * @param LinkRenderer|null $linkRenderer (since 1.28) |
||
| 506 | * |
||
| 507 | * @return bool |
||
| 508 | */ |
||
| 509 | public static function executePath( Title &$title, IContextSource &$context, $including = false, |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Just like executePath() but will override global variables and execute |
||
| 583 | * the page in "inclusion" mode. Returns true if the execution was |
||
| 584 | * successful or false if there was no such special page, or a title object |
||
| 585 | * if it was a redirect. |
||
| 586 | * |
||
| 587 | * Also saves the current $wgTitle, $wgOut, $wgRequest, $wgUser and $wgLang |
||
| 588 | * variables so that the special page will get the context it'd expect on a |
||
| 589 | * normal request, and then restores them to their previous values after. |
||
| 590 | * |
||
| 591 | * @param Title $title |
||
| 592 | * @param IContextSource $context |
||
| 593 | * @param LinkRenderer|null $linkRenderer (since 1.28) |
||
| 594 | * @return string HTML fragment |
||
| 595 | */ |
||
| 596 | public static function capturePath( |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Get the local name for a specified canonical name |
||
| 650 | * |
||
| 651 | * @param string $name |
||
| 652 | * @param string|bool $subpage |
||
| 653 | * @return string |
||
| 654 | */ |
||
| 655 | public static function getLocalNameFor( $name, $subpage = false ) { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Get a title for a given alias |
||
| 705 | * |
||
| 706 | * @param string $alias |
||
| 707 | * @return Title|null Title or null if there is no such alias |
||
| 708 | */ |
||
| 709 | public static function getTitleForAlias( $alias ) { |
||
| 717 | } |
||
| 718 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: