Complex classes like ResourceLoaderFileModule 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 ResourceLoaderFileModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ResourceLoaderFileModule extends ResourceLoaderModule { |
||
| 29 | /* Protected Members */ |
||
| 30 | |||
| 31 | /** @var string Local base path, see __construct() */ |
||
| 32 | protected $localBasePath = ''; |
||
| 33 | |||
| 34 | /** @var string Remote base path, see __construct() */ |
||
| 35 | protected $remoteBasePath = ''; |
||
| 36 | |||
| 37 | /** @var array Saves a list of the templates named by the modules. */ |
||
| 38 | protected $templates = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array List of paths to JavaScript files to always include |
||
| 42 | * @par Usage: |
||
| 43 | * @code |
||
| 44 | * array( [file-path], [file-path], ... ) |
||
| 45 | * @endcode |
||
| 46 | */ |
||
| 47 | protected $scripts = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array List of JavaScript files to include when using a specific language |
||
| 51 | * @par Usage: |
||
| 52 | * @code |
||
| 53 | * array( [language-code] => array( [file-path], [file-path], ... ), ... ) |
||
| 54 | * @endcode |
||
| 55 | */ |
||
| 56 | protected $languageScripts = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array List of JavaScript files to include when using a specific skin |
||
| 60 | * @par Usage: |
||
| 61 | * @code |
||
| 62 | * array( [skin-name] => array( [file-path], [file-path], ... ), ... ) |
||
| 63 | * @endcode |
||
| 64 | */ |
||
| 65 | protected $skinScripts = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array List of paths to JavaScript files to include in debug mode |
||
| 69 | * @par Usage: |
||
| 70 | * @code |
||
| 71 | * array( [skin-name] => array( [file-path], [file-path], ... ), ... ) |
||
| 72 | * @endcode |
||
| 73 | */ |
||
| 74 | protected $debugScripts = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array List of paths to CSS files to always include |
||
| 78 | * @par Usage: |
||
| 79 | * @code |
||
| 80 | * array( [file-path], [file-path], ... ) |
||
| 81 | * @endcode |
||
| 82 | */ |
||
| 83 | protected $styles = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array List of paths to CSS files to include when using specific skins |
||
| 87 | * @par Usage: |
||
| 88 | * @code |
||
| 89 | * array( [file-path], [file-path], ... ) |
||
| 90 | * @endcode |
||
| 91 | */ |
||
| 92 | protected $skinStyles = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array List of modules this module depends on |
||
| 96 | * @par Usage: |
||
| 97 | * @code |
||
| 98 | * array( [file-path], [file-path], ... ) |
||
| 99 | * @endcode |
||
| 100 | */ |
||
| 101 | protected $dependencies = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var string File name containing the body of the skip function |
||
| 105 | */ |
||
| 106 | protected $skipFunction = null; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var array List of message keys used by this module |
||
| 110 | * @par Usage: |
||
| 111 | * @code |
||
| 112 | * array( [message-key], [message-key], ... ) |
||
| 113 | * @endcode |
||
| 114 | */ |
||
| 115 | protected $messages = []; |
||
| 116 | |||
| 117 | /** @var string Name of group to load this module in */ |
||
| 118 | protected $group; |
||
| 119 | |||
| 120 | /** @var string Position on the page to load this module at */ |
||
| 121 | protected $position = 'bottom'; |
||
| 122 | |||
| 123 | /** @var bool Link to raw files in debug mode */ |
||
| 124 | protected $debugRaw = true; |
||
| 125 | |||
| 126 | /** @var bool Whether mw.loader.state() call should be omitted */ |
||
| 127 | protected $raw = false; |
||
| 128 | |||
| 129 | protected $targets = [ 'desktop' ]; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var bool Whether getStyleURLsForDebug should return raw file paths, |
||
| 133 | * or return load.php urls |
||
| 134 | */ |
||
| 135 | protected $hasGeneratedStyles = false; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var array Place where readStyleFile() tracks file dependencies |
||
| 139 | * @par Usage: |
||
| 140 | * @code |
||
| 141 | * array( [file-path], [file-path], ... ) |
||
| 142 | * @endcode |
||
| 143 | */ |
||
| 144 | protected $localFileRefs = []; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var array Place where readStyleFile() tracks file dependencies for non-existent files. |
||
| 148 | * Used in tests to detect missing dependencies. |
||
| 149 | */ |
||
| 150 | protected $missingLocalFileRefs = []; |
||
| 151 | |||
| 152 | /* Methods */ |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Constructs a new module from an options array. |
||
| 156 | * |
||
| 157 | * @param array $options List of options; if not given or empty, an empty module will be |
||
| 158 | * constructed |
||
| 159 | * @param string $localBasePath Base path to prepend to all local paths in $options. Defaults |
||
| 160 | * to $IP |
||
| 161 | * @param string $remoteBasePath Base path to prepend to all remote paths in $options. Defaults |
||
| 162 | * to $wgResourceBasePath |
||
| 163 | * |
||
| 164 | * Below is a description for the $options array: |
||
| 165 | * @throws InvalidArgumentException |
||
| 166 | * @par Construction options: |
||
| 167 | * @code |
||
| 168 | * array( |
||
| 169 | * // Base path to prepend to all local paths in $options. Defaults to $IP |
||
| 170 | * 'localBasePath' => [base path], |
||
| 171 | * // Base path to prepend to all remote paths in $options. Defaults to $wgResourceBasePath |
||
| 172 | * 'remoteBasePath' => [base path], |
||
| 173 | * // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath |
||
| 174 | * 'remoteExtPath' => [base path], |
||
| 175 | * // Equivalent of remoteBasePath, but relative to $wgStylePath |
||
| 176 | * 'remoteSkinPath' => [base path], |
||
| 177 | * // Scripts to always include |
||
| 178 | * 'scripts' => [file path string or array of file path strings], |
||
| 179 | * // Scripts to include in specific language contexts |
||
| 180 | * 'languageScripts' => array( |
||
| 181 | * [language code] => [file path string or array of file path strings], |
||
| 182 | * ), |
||
| 183 | * // Scripts to include in specific skin contexts |
||
| 184 | * 'skinScripts' => array( |
||
| 185 | * [skin name] => [file path string or array of file path strings], |
||
| 186 | * ), |
||
| 187 | * // Scripts to include in debug contexts |
||
| 188 | * 'debugScripts' => [file path string or array of file path strings], |
||
| 189 | * // Modules which must be loaded before this module |
||
| 190 | * 'dependencies' => [module name string or array of module name strings], |
||
| 191 | * 'templates' => array( |
||
| 192 | * [template alias with file.ext] => [file path to a template file], |
||
| 193 | * ), |
||
| 194 | * // Styles to always load |
||
| 195 | * 'styles' => [file path string or array of file path strings], |
||
| 196 | * // Styles to include in specific skin contexts |
||
| 197 | * 'skinStyles' => array( |
||
| 198 | * [skin name] => [file path string or array of file path strings], |
||
| 199 | * ), |
||
| 200 | * // Messages to always load |
||
| 201 | * 'messages' => [array of message key strings], |
||
| 202 | * // Group which this module should be loaded together with |
||
| 203 | * 'group' => [group name string], |
||
| 204 | * // Position on the page to load this module at |
||
| 205 | * 'position' => ['bottom' (default) or 'top'] |
||
| 206 | * // Function that, if it returns true, makes the loader skip this module. |
||
| 207 | * // The file must contain valid JavaScript for execution in a private function. |
||
| 208 | * // The file must not contain the "function () {" and "}" wrapper though. |
||
| 209 | * 'skipFunction' => [file path] |
||
| 210 | * ) |
||
| 211 | * @endcode |
||
| 212 | */ |
||
| 213 | public function __construct( |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Extract a pair of local and remote base paths from module definition information. |
||
| 300 | * Implementation note: the amount of global state used in this function is staggering. |
||
| 301 | * |
||
| 302 | * @param array $options Module definition |
||
| 303 | * @param string $localBasePath Path to use if not provided in module definition. Defaults |
||
| 304 | * to $IP |
||
| 305 | * @param string $remoteBasePath Path to use if not provided in module definition. Defaults |
||
| 306 | * to $wgResourceBasePath |
||
| 307 | * @return array Array( localBasePath, remoteBasePath ) |
||
| 308 | */ |
||
| 309 | public static function extractBasePaths( |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Gets all scripts for a given context concatenated together. |
||
| 349 | * |
||
| 350 | * @param ResourceLoaderContext $context Context in which to generate script |
||
| 351 | * @return string JavaScript code for $context |
||
| 352 | */ |
||
| 353 | public function getScript( ResourceLoaderContext $context ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param ResourceLoaderContext $context |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | public function getScriptURLsForDebug( ResourceLoaderContext $context ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | public function supportsURLLoading() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get all styles for a given context. |
||
| 382 | * |
||
| 383 | * @param ResourceLoaderContext $context |
||
| 384 | * @return array CSS code for $context as an associative array mapping media type to CSS text. |
||
| 385 | */ |
||
| 386 | public function getStyles( ResourceLoaderContext $context ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param ResourceLoaderContext $context |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | public function getStyleURLsForDebug( ResourceLoaderContext $context ) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Gets list of message keys used by this module. |
||
| 425 | * |
||
| 426 | * @return array List of message keys |
||
| 427 | */ |
||
| 428 | public function getMessages() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Gets the name of the group this module should be loaded in. |
||
| 434 | * |
||
| 435 | * @return string Group name |
||
| 436 | */ |
||
| 437 | public function getGroup() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | public function getPosition() { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Gets list of names of modules this module depends on. |
||
| 450 | * @param ResourceLoaderContext|null $context |
||
| 451 | * @return array List of module names |
||
| 452 | */ |
||
| 453 | public function getDependencies( ResourceLoaderContext $context = null ) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Get the skip function. |
||
| 459 | * @return null|string |
||
| 460 | * @throws MWException |
||
| 461 | */ |
||
| 462 | public function getSkipFunction() { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return bool |
||
| 480 | */ |
||
| 481 | public function isRaw() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Disable module content versioning. |
||
| 487 | * |
||
| 488 | * This class uses getDefinitionSummary() instead, to avoid filesystem overhead |
||
| 489 | * involved with building the full module content inside a startup request. |
||
| 490 | * |
||
| 491 | * @return bool |
||
| 492 | */ |
||
| 493 | public function enableModuleContentVersion() { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Helper method to gather file hashes for getDefinitionSummary. |
||
| 499 | * |
||
| 500 | * This function is context-sensitive, only computing hashes of files relevant to the |
||
| 501 | * given language, skin, etc. |
||
| 502 | * |
||
| 503 | * @see ResourceLoaderModule::getFileDependencies |
||
| 504 | * @param ResourceLoaderContext $context |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | protected function getFileHashes( ResourceLoaderContext $context ) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Get the definition summary for this module. |
||
| 553 | * |
||
| 554 | * @param ResourceLoaderContext $context |
||
| 555 | * @return array |
||
| 556 | */ |
||
| 557 | public function getDefinitionSummary( ResourceLoaderContext $context ) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @param string|ResourceLoaderFilePath $path |
||
| 595 | * @return string |
||
| 596 | */ |
||
| 597 | protected function getLocalPath( $path ) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @param string|ResourceLoaderFilePath $path |
||
| 607 | * @return string |
||
| 608 | */ |
||
| 609 | protected function getRemotePath( $path ) { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Infer the stylesheet language from a stylesheet file path. |
||
| 619 | * |
||
| 620 | * @since 1.22 |
||
| 621 | * @param string $path |
||
| 622 | * @return string The stylesheet language name |
||
| 623 | */ |
||
| 624 | public function getStyleSheetLang( $path ) { |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Collates file paths by option (where provided). |
||
| 630 | * |
||
| 631 | * @param array $list List of file paths in any combination of index/path |
||
| 632 | * or path/options pairs |
||
| 633 | * @param string $option Option name |
||
| 634 | * @param mixed $default Default value if the option isn't set |
||
| 635 | * @return array List of file paths, collated by $option |
||
| 636 | */ |
||
| 637 | protected static function collateFilePathListByOption( array $list, $option, $default ) { |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Get a list of element that match a key, optionally using a fallback key. |
||
| 660 | * |
||
| 661 | * @param array $list List of lists to select from |
||
| 662 | * @param string $key Key to look for in $map |
||
| 663 | * @param string $fallback Key to look for in $list if $key doesn't exist |
||
| 664 | * @return array List of elements from $map which matched $key or $fallback, |
||
| 665 | * or an empty list in case of no match |
||
| 666 | */ |
||
| 667 | protected static function tryForKey( array $list, $key, $fallback = null ) { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Get a list of file paths for all scripts in this module, in order of proper execution. |
||
| 681 | * |
||
| 682 | * @param ResourceLoaderContext $context |
||
| 683 | * @return array List of file paths |
||
| 684 | */ |
||
| 685 | protected function getScriptFiles( ResourceLoaderContext $context ) { |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Get the set of language scripts for the given language, |
||
| 700 | * possibly using a fallback language. |
||
| 701 | * |
||
| 702 | * @param string $lang |
||
| 703 | * @return array |
||
| 704 | */ |
||
| 705 | private function getLanguageScripts( $lang ) { |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Get a list of file paths for all styles in this module, in order of proper inclusion. |
||
| 723 | * |
||
| 724 | * @param ResourceLoaderContext $context |
||
| 725 | * @return array List of file paths |
||
| 726 | */ |
||
| 727 | public function getStyleFiles( ResourceLoaderContext $context ) { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Gets a list of file paths for all skin styles in the module used by |
||
| 740 | * the skin. |
||
| 741 | * |
||
| 742 | * @param string $skinName The name of the skin |
||
| 743 | * @return array A list of file paths collated by media type |
||
| 744 | */ |
||
| 745 | protected function getSkinStyleFiles( $skinName ) { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Gets a list of file paths for all skin style files in the module, |
||
| 755 | * for all available skins. |
||
| 756 | * |
||
| 757 | * @return array A list of file paths collated by media type |
||
| 758 | */ |
||
| 759 | protected function getAllSkinStyleFiles() { |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Returns all style files and all skin style files used by this module. |
||
| 776 | * |
||
| 777 | * @return array |
||
| 778 | */ |
||
| 779 | public function getAllStyleFiles() { |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Gets the contents of a list of JavaScript files. |
||
| 798 | * |
||
| 799 | * @param array $scripts List of file paths to scripts to read, remap and concetenate |
||
| 800 | * @throws MWException |
||
| 801 | * @return string Concatenated and remapped JavaScript data from $scripts |
||
| 802 | */ |
||
| 803 | protected function readScriptFiles( array $scripts ) { |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Gets the contents of a list of CSS files. |
||
| 827 | * |
||
| 828 | * @param array $styles List of media type/list of file paths pairs, to read, remap and |
||
| 829 | * concetenate |
||
| 830 | * @param bool $flip |
||
| 831 | * @param ResourceLoaderContext $context |
||
| 832 | * |
||
| 833 | * @throws MWException |
||
| 834 | * @return array List of concatenated and remapped CSS data from $styles, |
||
| 835 | * keyed by media type |
||
| 836 | * |
||
| 837 | * @since 1.27 Calling this method without a ResourceLoaderContext instance |
||
| 838 | * is deprecated. |
||
| 839 | */ |
||
| 840 | public function readStyleFiles( array $styles, $flip, $context = null ) { |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Reads a style file. |
||
| 862 | * |
||
| 863 | * This method can be used as a callback for array_map() |
||
| 864 | * |
||
| 865 | * @param string $path File path of style file to read |
||
| 866 | * @param bool $flip |
||
| 867 | * @param ResourceLoaderContext $context |
||
| 868 | * |
||
| 869 | * @return string CSS data in script file |
||
| 870 | * @throws MWException If the file doesn't exist |
||
| 871 | */ |
||
| 872 | protected function readStyleFile( $path, $flip, $context ) { |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Get whether CSS for this module should be flipped |
||
| 909 | * @param ResourceLoaderContext $context |
||
| 910 | * @return bool |
||
| 911 | */ |
||
| 912 | public function getFlip( $context ) { |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile'] |
||
| 918 | * |
||
| 919 | * @return array Array of strings |
||
| 920 | */ |
||
| 921 | public function getTargets() { |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Get the module's load type. |
||
| 927 | * |
||
| 928 | * @since 1.28 |
||
| 929 | * @return string |
||
| 930 | */ |
||
| 931 | public function getType() { |
||
| 946 | |||
| 947 | /** |
||
| 948 | * Compile a LESS file into CSS. |
||
| 949 | * |
||
| 950 | * Keeps track of all used files and adds them to localFileRefs. |
||
| 951 | * |
||
| 952 | * @since 1.22 |
||
| 953 | * @since 1.27 Added $context paramter. |
||
| 954 | * @throws Exception If less.php encounters a parse error |
||
| 955 | * @param string $fileName File path of LESS source |
||
| 956 | * @param ResourceLoaderContext $context Context in which to generate script |
||
| 957 | * @return string CSS source |
||
| 958 | */ |
||
| 959 | protected function compileLessFile( $fileName, ResourceLoaderContext $context ) { |
||
| 998 | |||
| 999 | /** |
||
| 1000 | * Takes named templates by the module and returns an array mapping. |
||
| 1001 | * @return array of templates mapping template alias to content |
||
| 1002 | * @throws MWException |
||
| 1003 | */ |
||
| 1004 | public function getTemplates() { |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Takes an input string and removes the UTF-8 BOM character if present |
||
| 1027 | * |
||
| 1028 | * We need to remove these after reading a file, because we concatenate our files and |
||
| 1029 | * the BOM character is not valid in the middle of a string. |
||
| 1030 | * We already assume UTF-8 everywhere, so this should be safe. |
||
| 1031 | * |
||
| 1032 | * @return string input minus the intial BOM char |
||
| 1033 | */ |
||
| 1034 | protected function stripBom( $input ) { |
||
| 1040 | } |
||
| 1041 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: