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 ExtensionProcessor 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 ExtensionProcessor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class ExtensionProcessor implements Processor { |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Keys that should be set to $GLOBALS |
||
| 7 | * |
||
| 8 | * @var array |
||
| 9 | */ |
||
| 10 | protected static $globalSettings = [ |
||
| 11 | 'ResourceLoaderSources', |
||
| 12 | 'ResourceLoaderLESSVars', |
||
| 13 | 'ResourceLoaderLESSImportPaths', |
||
| 14 | 'DefaultUserOptions', |
||
| 15 | 'HiddenPrefs', |
||
| 16 | 'GroupPermissions', |
||
| 17 | 'RevokePermissions', |
||
| 18 | 'ImplicitGroups', |
||
| 19 | 'GroupsAddToSelf', |
||
| 20 | 'GroupsRemoveFromSelf', |
||
| 21 | 'AddGroups', |
||
| 22 | 'RemoveGroups', |
||
| 23 | 'AvailableRights', |
||
| 24 | 'ContentHandlers', |
||
| 25 | 'ConfigRegistry', |
||
| 26 | 'CentralIdLookupProviders', |
||
| 27 | 'RateLimits', |
||
| 28 | 'RecentChangesFlags', |
||
| 29 | 'MediaHandlers', |
||
| 30 | 'ExtensionFunctions', |
||
| 31 | 'ExtensionEntryPointListFiles', |
||
| 32 | 'SpecialPages', |
||
| 33 | 'JobClasses', |
||
| 34 | 'LogTypes', |
||
| 35 | 'LogRestrictions', |
||
| 36 | 'FilterLogTypes', |
||
| 37 | 'ActionFilteredLogs', |
||
| 38 | 'LogNames', |
||
| 39 | 'LogHeaders', |
||
| 40 | 'LogActions', |
||
| 41 | 'LogActionsHandlers', |
||
| 42 | 'Actions', |
||
| 43 | 'APIModules', |
||
| 44 | 'APIFormatModules', |
||
| 45 | 'APIMetaModules', |
||
| 46 | 'APIPropModules', |
||
| 47 | 'APIListModules', |
||
| 48 | 'ValidSkinNames', |
||
| 49 | 'FeedClasses', |
||
| 50 | ]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Mapping of global settings to their specific merge strategies. |
||
| 54 | * |
||
| 55 | * @see ExtensionRegistry::exportExtractedData |
||
| 56 | * @see getExtractedInfo |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected static $mergeStrategies = [ |
||
| 60 | 'wgGroupPermissions' => 'array_plus_2d', |
||
| 61 | 'wgRevokePermissions' => 'array_plus_2d', |
||
| 62 | 'wgHooks' => 'array_merge_recursive', |
||
| 63 | 'wgExtensionCredits' => 'array_merge_recursive', |
||
| 64 | 'wgExtraGenderNamespaces' => 'array_plus', |
||
| 65 | 'wgNamespacesWithSubpages' => 'array_plus', |
||
| 66 | 'wgNamespaceContentModels' => 'array_plus', |
||
| 67 | 'wgNamespaceProtection' => 'array_plus', |
||
| 68 | 'wgCapitalLinkOverrides' => 'array_plus', |
||
| 69 | 'wgRateLimits' => 'array_plus_2d', |
||
| 70 | ]; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Keys that are part of the extension credits |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | protected static $creditsAttributes = [ |
||
| 78 | 'name', |
||
| 79 | 'namemsg', |
||
| 80 | 'author', |
||
| 81 | 'version', |
||
| 82 | 'url', |
||
| 83 | 'description', |
||
| 84 | 'descriptionmsg', |
||
| 85 | 'license-name', |
||
| 86 | ]; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Things that are not 'attributes', but are not in |
||
| 90 | * $globalSettings or $creditsAttributes. |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | protected static $notAttributes = [ |
||
| 95 | 'callback', |
||
| 96 | 'Hooks', |
||
| 97 | 'namespaces', |
||
| 98 | 'ResourceFileModulePaths', |
||
| 99 | 'ResourceModules', |
||
| 100 | 'ResourceModuleSkinStyles', |
||
| 101 | 'ExtensionMessagesFiles', |
||
| 102 | 'MessagesDirs', |
||
| 103 | 'type', |
||
| 104 | 'config', |
||
| 105 | 'ParserTestFiles', |
||
| 106 | 'AutoloadClasses', |
||
| 107 | 'manifest_version', |
||
| 108 | 'load_composer_autoloader', |
||
| 109 | ]; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Stuff that is going to be set to $GLOBALS |
||
| 113 | * |
||
| 114 | * Some keys are pre-set to arrays so we can += to them |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | protected $globals = [ |
||
| 119 | 'wgExtensionMessagesFiles' => [], |
||
| 120 | 'wgMessagesDirs' => [], |
||
| 121 | ]; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Things that should be define()'d |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected $defines = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Things to be called once registration of these extensions are done |
||
| 132 | * |
||
| 133 | * @var callable[] |
||
| 134 | */ |
||
| 135 | protected $callbacks = []; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | protected $credits = []; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Any thing else in the $info that hasn't |
||
| 144 | * already been processed |
||
| 145 | * |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $attributes = []; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $path |
||
| 152 | * @param array $info |
||
| 153 | * @param int $version manifest_version for info |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function extractInfo( $path, array $info, $version ) { |
||
| 181 | |||
| 182 | public function getExtractedInfo() { |
||
| 198 | |||
| 199 | public function getRequirements( array $info ) { |
||
| 208 | |||
| 209 | protected function extractHooks( array $info ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Register namespaces with the appropriate global settings |
||
| 225 | * |
||
| 226 | * @param array $info |
||
| 227 | */ |
||
| 228 | protected function extractNamespaces( array $info ) { |
||
| 255 | |||
| 256 | protected function extractResourceLoaderModules( $dir, array $info ) { |
||
| 288 | |||
| 289 | protected function extractExtensionMessagesFiles( $dir, array $info ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Set message-related settings, which need to be expanded to use |
||
| 299 | * absolute paths |
||
| 300 | * |
||
| 301 | * @param string $dir |
||
| 302 | * @param array $info |
||
| 303 | */ |
||
| 304 | protected function extractMessagesDirs( $dir, array $info ) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $path |
||
| 316 | * @param array $info |
||
| 317 | * @throws Exception |
||
| 318 | */ |
||
| 319 | protected function extractCredits( $path, array $info ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Set configuration settings |
||
| 346 | * @todo In the future, this should be done via Config interfaces |
||
| 347 | * |
||
| 348 | * @param array $info |
||
| 349 | */ |
||
| 350 | protected function extractConfig( array $info ) { |
||
| 365 | |||
| 366 | protected function extractParserTestFiles( $dir, array $info ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $path |
||
| 376 | * @param string $name |
||
| 377 | * @param array $value |
||
| 378 | * @param array &$array |
||
| 379 | * @throws InvalidArgumentException |
||
| 380 | */ |
||
| 381 | protected function storeToArray( $path, $name, $value, &$array ) { |
||
| 391 | |||
| 392 | public function getExtraAutoloaderPaths( $dir, array $info ) { |
||
| 402 | } |
||
| 403 |