Complex classes like ResourceLoaderWikiModule 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 ResourceLoaderWikiModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class ResourceLoaderWikiModule extends ResourceLoaderModule { |
||
| 46 | /** @var string Position on the page to load this module at */ |
||
| 47 | protected $position = 'bottom'; |
||
| 48 | |||
| 49 | // Origin defaults to users with sitewide authority |
||
| 50 | protected $origin = self::ORIGIN_USER_SITEWIDE; |
||
| 51 | |||
| 52 | // In-process cache for title info |
||
| 53 | protected $titleInfo = []; |
||
| 54 | |||
| 55 | // List of page names that contain CSS |
||
| 56 | protected $styles = []; |
||
| 57 | |||
| 58 | // List of page names that contain JavaScript |
||
| 59 | protected $scripts = []; |
||
| 60 | |||
| 61 | // Group of module |
||
| 62 | protected $group; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param array $options For back-compat, this can be omitted in favour of overwriting getPages. |
||
| 66 | */ |
||
| 67 | public function __construct( array $options = null ) { |
||
| 68 | if ( is_null( $options ) ) { |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | |||
| 72 | foreach ( $options as $member => $option ) { |
||
| 73 | switch ( $member ) { |
||
| 74 | case 'position': |
||
| 75 | case 'styles': |
||
| 76 | case 'scripts': |
||
| 77 | case 'group': |
||
| 78 | case 'targets': |
||
| 79 | $this->{$member} = $option; |
||
| 80 | break; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Subclasses should return an associative array of resources in the module. |
||
| 87 | * Keys should be the title of a page in the MediaWiki or User namespace. |
||
| 88 | * |
||
| 89 | * Values should be a nested array of options. The supported keys are 'type' and |
||
| 90 | * (CSS only) 'media'. |
||
| 91 | * |
||
| 92 | * For scripts, 'type' should be 'script'. |
||
| 93 | * |
||
| 94 | * For stylesheets, 'type' should be 'style'. |
||
| 95 | * There is an optional media key, the value of which can be the |
||
| 96 | * medium ('screen', 'print', etc.) of the stylesheet. |
||
| 97 | * |
||
| 98 | * @param ResourceLoaderContext $context |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | protected function getPages( ResourceLoaderContext $context ) { |
||
| 102 | $config = $this->getConfig(); |
||
| 103 | $pages = []; |
||
| 104 | |||
| 105 | // Filter out pages from origins not allowed by the current wiki configuration. |
||
| 106 | if ( $config->get( 'UseSiteJs' ) ) { |
||
| 107 | foreach ( $this->scripts as $script ) { |
||
| 108 | $pages[$script] = [ 'type' => 'script' ]; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | if ( $config->get( 'UseSiteCss' ) ) { |
||
| 113 | foreach ( $this->styles as $style ) { |
||
| 114 | $pages[$style] = [ 'type' => 'style' ]; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | return $pages; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get group name |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getGroup() { |
||
| 127 | return $this->group; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the Database object used in getTitleInfo(). |
||
| 132 | * |
||
| 133 | * Defaults to the local slave DB. Subclasses may want to override this to return a foreign |
||
| 134 | * database object, or null if getTitleInfo() shouldn't access the database. |
||
| 135 | * |
||
| 136 | * NOTE: This ONLY works for getTitleInfo() and isKnownEmpty(), NOT FOR ANYTHING ELSE. |
||
| 137 | * In particular, it doesn't work for getContent() or getScript() etc. |
||
| 138 | * |
||
| 139 | * @return IDatabase|null |
||
| 140 | */ |
||
| 141 | protected function getDB() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $title |
||
|
|
|||
| 147 | * @return null|string |
||
| 148 | */ |
||
| 149 | protected function getContent( $titleText ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param ResourceLoaderContext $context |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function getScript( ResourceLoaderContext $context ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param ResourceLoaderContext $context |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function getStyles( ResourceLoaderContext $context ) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Disable module content versioning. |
||
| 229 | * |
||
| 230 | * This class does not support generating content outside of a module |
||
| 231 | * request due to foreign database support. |
||
| 232 | * |
||
| 233 | * See getDefinitionSummary() for meta-data versioning. |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function enableModuleContentVersion() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param ResourceLoaderContext $context |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function getDefinitionSummary( ResourceLoaderContext $context ) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param ResourceLoaderContext $context |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function isKnownEmpty( ResourceLoaderContext $context ) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get the information about the wiki pages for a given context. |
||
| 281 | * @param ResourceLoaderContext $context |
||
| 282 | * @return array Keyed by page name. Contains arrays with 'rev_len' and 'rev_sha1' keys |
||
| 283 | */ |
||
| 284 | protected function getTitleInfo( ResourceLoaderContext $context ) { |
||
| 323 | |||
| 324 | public function getPosition() { |
||
| 327 | } |
||
| 328 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.