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 | 'DefaultUserOptions', |
||
| 14 | 'HiddenPrefs', |
||
| 15 | 'GroupPermissions', |
||
| 16 | 'RevokePermissions', |
||
| 17 | 'GrantPermissions', |
||
| 18 | 'GrantPermissionGroups', |
||
| 19 | 'ImplicitGroups', |
||
| 20 | 'GroupsAddToSelf', |
||
| 21 | 'GroupsRemoveFromSelf', |
||
| 22 | 'AddGroups', |
||
| 23 | 'RemoveGroups', |
||
| 24 | 'AvailableRights', |
||
| 25 | 'ContentHandlers', |
||
| 26 | 'ConfigRegistry', |
||
| 27 | 'SessionProviders', |
||
| 28 | 'AuthManagerAutoConfig', |
||
| 29 | 'CentralIdLookupProviders', |
||
| 30 | 'ChangeCredentialsBlacklist', |
||
| 31 | 'RemoveCredentialsBlacklist', |
||
| 32 | 'RateLimits', |
||
| 33 | 'RecentChangesFlags', |
||
| 34 | 'MediaHandlers', |
||
| 35 | 'ExtensionFunctions', |
||
| 36 | 'ExtensionEntryPointListFiles', |
||
| 37 | 'SpecialPages', |
||
| 38 | 'JobClasses', |
||
| 39 | 'LogTypes', |
||
| 40 | 'LogRestrictions', |
||
| 41 | 'FilterLogTypes', |
||
| 42 | 'ActionFilteredLogs', |
||
| 43 | 'LogNames', |
||
| 44 | 'LogHeaders', |
||
| 45 | 'LogActions', |
||
| 46 | 'LogActionsHandlers', |
||
| 47 | 'Actions', |
||
| 48 | 'APIModules', |
||
| 49 | 'APIFormatModules', |
||
| 50 | 'APIMetaModules', |
||
| 51 | 'APIPropModules', |
||
| 52 | 'APIListModules', |
||
| 53 | 'ValidSkinNames', |
||
| 54 | 'FeedClasses', |
||
| 55 | ]; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Mapping of global settings to their specific merge strategies. |
||
| 59 | * |
||
| 60 | * @see ExtensionRegistry::exportExtractedData |
||
| 61 | * @see getExtractedInfo |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected static $mergeStrategies = [ |
||
| 65 | 'wgGroupPermissions' => 'array_plus_2d', |
||
| 66 | 'wgRevokePermissions' => 'array_plus_2d', |
||
| 67 | 'wgGrantPermissions' => 'array_plus_2d', |
||
| 68 | 'wgHooks' => 'array_merge_recursive', |
||
| 69 | 'wgExtensionCredits' => 'array_merge_recursive', |
||
| 70 | 'wgExtraGenderNamespaces' => 'array_plus', |
||
| 71 | 'wgNamespacesWithSubpages' => 'array_plus', |
||
| 72 | 'wgNamespaceContentModels' => 'array_plus', |
||
| 73 | 'wgNamespaceProtection' => 'array_plus', |
||
| 74 | 'wgCapitalLinkOverrides' => 'array_plus', |
||
| 75 | 'wgRateLimits' => 'array_plus_2d', |
||
| 76 | 'wgAuthManagerAutoConfig' => 'array_plus_2d', |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Keys that are part of the extension credits |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected static $creditsAttributes = [ |
||
| 85 | 'name', |
||
| 86 | 'namemsg', |
||
| 87 | 'author', |
||
| 88 | 'version', |
||
| 89 | 'url', |
||
| 90 | 'description', |
||
| 91 | 'descriptionmsg', |
||
| 92 | 'license-name', |
||
| 93 | ]; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Things that are not 'attributes', but are not in |
||
| 97 | * $globalSettings or $creditsAttributes. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected static $notAttributes = [ |
||
| 102 | 'callback', |
||
| 103 | 'Hooks', |
||
| 104 | 'namespaces', |
||
| 105 | 'ResourceFileModulePaths', |
||
| 106 | 'ResourceModules', |
||
| 107 | 'ResourceModuleSkinStyles', |
||
| 108 | 'ExtensionMessagesFiles', |
||
| 109 | 'MessagesDirs', |
||
| 110 | 'type', |
||
| 111 | 'config', |
||
| 112 | 'config_prefix', |
||
| 113 | 'ParserTestFiles', |
||
| 114 | 'AutoloadClasses', |
||
| 115 | 'manifest_version', |
||
| 116 | 'load_composer_autoloader', |
||
| 117 | ]; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Stuff that is going to be set to $GLOBALS |
||
| 121 | * |
||
| 122 | * Some keys are pre-set to arrays so we can += to them |
||
| 123 | * |
||
| 124 | * @var array |
||
| 125 | */ |
||
| 126 | protected $globals = [ |
||
| 127 | 'wgExtensionMessagesFiles' => [], |
||
| 128 | 'wgMessagesDirs' => [], |
||
| 129 | ]; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Things that should be define()'d |
||
| 133 | * |
||
| 134 | * @var array |
||
| 135 | */ |
||
| 136 | protected $defines = []; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Things to be called once registration of these extensions are done |
||
| 140 | * |
||
| 141 | * @var callable[] |
||
| 142 | */ |
||
| 143 | protected $callbacks = []; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $credits = []; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Any thing else in the $info that hasn't |
||
| 152 | * already been processed |
||
| 153 | * |
||
| 154 | * @var array |
||
| 155 | */ |
||
| 156 | protected $attributes = []; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $path |
||
| 160 | * @param array $info |
||
| 161 | * @param int $version manifest_version for info |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function extractInfo( $path, array $info, $version ) { |
||
| 194 | |||
| 195 | public function getExtractedInfo() { |
||
| 211 | |||
| 212 | public function getRequirements( array $info ) { |
||
| 221 | |||
| 222 | protected function extractHooks( array $info ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Register namespaces with the appropriate global settings |
||
| 238 | * |
||
| 239 | * @param array $info |
||
| 240 | */ |
||
| 241 | protected function extractNamespaces( array $info ) { |
||
| 271 | |||
| 272 | protected function extractResourceLoaderModules( $dir, array $info ) { |
||
| 304 | |||
| 305 | protected function extractExtensionMessagesFiles( $dir, array $info ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set message-related settings, which need to be expanded to use |
||
| 315 | * absolute paths |
||
| 316 | * |
||
| 317 | * @param string $dir |
||
| 318 | * @param array $info |
||
| 319 | */ |
||
| 320 | protected function extractMessagesDirs( $dir, array $info ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param string $path |
||
| 332 | * @param array $info |
||
| 333 | * @throws Exception |
||
| 334 | */ |
||
| 335 | protected function extractCredits( $path, array $info ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Set configuration settings for manifest_version == 1 |
||
| 362 | * @todo In the future, this should be done via Config interfaces |
||
| 363 | * |
||
| 364 | * @param array $info |
||
| 365 | */ |
||
| 366 | protected function extractConfig1( array $info ) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set configuration settings for manifest_version == 2 |
||
| 384 | * @todo In the future, this should be done via Config interfaces |
||
| 385 | * |
||
| 386 | * @param array $info |
||
| 387 | * @param string $dir |
||
| 388 | */ |
||
| 389 | protected function extractConfig2( array $info, $dir ) { |
||
| 408 | |||
| 409 | protected function extractParserTestFiles( $dir, array $info ) { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param string $path |
||
| 419 | * @param string $name |
||
| 420 | * @param array $value |
||
| 421 | * @param array &$array |
||
| 422 | * @throws InvalidArgumentException |
||
| 423 | */ |
||
| 424 | protected function storeToArray( $path, $name, $value, &$array ) { |
||
| 434 | |||
| 435 | public function getExtraAutoloaderPaths( $dir, array $info ) { |
||
| 445 | } |
||
| 446 |