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 ResourceLoader 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 ResourceLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class ResourceLoader implements LoggerAwareInterface { |
||
| 37 | /** @var int */ |
||
| 38 | protected static $filterCacheVersion = 7; |
||
| 39 | |||
| 40 | /** @var bool */ |
||
| 41 | protected static $debugMode = null; |
||
| 42 | |||
| 43 | /** @var array */ |
||
| 44 | private $lessVars = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Module name/ResourceLoaderModule object pairs |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $modules = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Associative array mapping module name to info associative array |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $moduleInfos = []; |
||
| 57 | |||
| 58 | /** @var Config $config */ |
||
| 59 | private $config; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Associative array mapping framework ids to a list of names of test suite modules |
||
| 63 | * like array( 'qunit' => array( 'mediawiki.tests.qunit.suites', 'ext.foo.tests', .. ), .. ) |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $testModuleNames = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * E.g. array( 'source-id' => 'http://.../load.php' ) |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $sources = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Errors accumulated during current respond() call. |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $errors = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var MessageBlobStore |
||
| 82 | */ |
||
| 83 | protected $blobStore; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var LoggerInterface |
||
| 87 | */ |
||
| 88 | private $logger; |
||
| 89 | |||
| 90 | /** @var string JavaScript / CSS pragma to disable minification. **/ |
||
| 91 | const FILTER_NOMIN = '/*@nomin*/'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Load information stored in the database about modules. |
||
| 95 | * |
||
| 96 | * This method grabs modules dependencies from the database and updates modules |
||
| 97 | * objects. |
||
| 98 | * |
||
| 99 | * This is not inside the module code because it is much faster to |
||
| 100 | * request all of the information at once than it is to have each module |
||
| 101 | * requests its own information. This sacrifice of modularity yields a substantial |
||
| 102 | * performance improvement. |
||
| 103 | * |
||
| 104 | * @param array $moduleNames List of module names to preload information for |
||
| 105 | * @param ResourceLoaderContext $context Context to load the information within |
||
| 106 | */ |
||
| 107 | public function preloadModuleInfo( array $moduleNames, ResourceLoaderContext $context ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Run JavaScript or CSS data through a filter, caching the filtered result for future calls. |
||
| 160 | * |
||
| 161 | * Available filters are: |
||
| 162 | * |
||
| 163 | * - minify-js \see JavaScriptMinifier::minify |
||
| 164 | * - minify-css \see CSSMin::minify |
||
| 165 | * |
||
| 166 | * If $data is empty, only contains whitespace or the filter was unknown, |
||
| 167 | * $data is returned unmodified. |
||
| 168 | * |
||
| 169 | * @param string $filter Name of filter to run |
||
| 170 | * @param string $data Text to filter, such as JavaScript or CSS text |
||
| 171 | * @param array $options Keys: |
||
| 172 | * - (bool) cache: Whether to allow caching this data. Default: true. |
||
| 173 | * @return string Filtered data, or a comment containing an error message |
||
| 174 | */ |
||
| 175 | public static function filter( $filter, $data, array $options = [] ) { |
||
| 209 | |||
| 210 | private static function applyFilter( $filter, $data ) { |
||
| 224 | |||
| 225 | /* Methods */ |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Register core modules and runs registration hooks. |
||
| 229 | * @param Config $config [optional] |
||
| 230 | * @param LoggerInterface $logger [optional] |
||
| 231 | */ |
||
| 232 | public function __construct( Config $config = null, LoggerInterface $logger = null ) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return Config |
||
| 265 | */ |
||
| 266 | public function getConfig() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @since 1.26 |
||
| 272 | * @param LoggerInterface $logger |
||
| 273 | */ |
||
| 274 | public function setLogger( LoggerInterface $logger ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @since 1.27 |
||
| 280 | * @return LoggerInterface |
||
| 281 | */ |
||
| 282 | public function getLogger() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @since 1.26 |
||
| 288 | * @return MessageBlobStore |
||
| 289 | */ |
||
| 290 | public function getMessageBlobStore() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @since 1.25 |
||
| 296 | * @param MessageBlobStore $blobStore |
||
| 297 | */ |
||
| 298 | public function setMessageBlobStore( MessageBlobStore $blobStore ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Register a module with the ResourceLoader system. |
||
| 304 | * |
||
| 305 | * @param mixed $name Name of module as a string or List of name/object pairs as an array |
||
| 306 | * @param array $info Module info array. For backwards compatibility with 1.17alpha, |
||
| 307 | * this may also be a ResourceLoaderModule object. Optional when using |
||
| 308 | * multiple-registration calling style. |
||
| 309 | * @throws MWException If a duplicate module registration is attempted |
||
| 310 | * @throws MWException If a module name contains illegal characters (pipes or commas) |
||
| 311 | * @throws MWException If something other than a ResourceLoaderModule is being registered |
||
| 312 | * @return bool False if there were any errors, in which case one or more modules were |
||
| 313 | * not registered |
||
| 314 | */ |
||
| 315 | public function register( $name, $info = null ) { |
||
| 316 | $moduleSkinStyles = $this->config->get( 'ResourceModuleSkinStyles' ); |
||
| 317 | |||
| 318 | // Allow multiple modules to be registered in one call |
||
| 319 | $registrations = is_array( $name ) ? $name : [ $name => $info ]; |
||
| 320 | foreach ( $registrations as $name => $info ) { |
||
| 321 | // Warn on duplicate registrations |
||
| 322 | if ( isset( $this->moduleInfos[$name] ) ) { |
||
| 323 | // A module has already been registered by this name |
||
| 324 | $this->logger->warning( |
||
| 325 | 'ResourceLoader duplicate registration warning. ' . |
||
| 326 | 'Another module has already been registered as ' . $name |
||
| 327 | ); |
||
| 328 | } |
||
| 329 | |||
| 330 | // Check $name for validity |
||
| 331 | if ( !self::isValidModuleName( $name ) ) { |
||
| 332 | throw new MWException( "ResourceLoader module name '$name' is invalid, " |
||
| 333 | . "see ResourceLoader::isValidModuleName()" ); |
||
| 334 | } |
||
| 335 | |||
| 336 | // Attach module |
||
| 337 | if ( $info instanceof ResourceLoaderModule ) { |
||
| 338 | $this->moduleInfos[$name] = [ 'object' => $info ]; |
||
| 339 | $info->setName( $name ); |
||
| 340 | $this->modules[$name] = $info; |
||
| 341 | } elseif ( is_array( $info ) ) { |
||
| 342 | // New calling convention |
||
| 343 | $this->moduleInfos[$name] = $info; |
||
| 344 | } else { |
||
| 345 | throw new MWException( |
||
| 346 | 'ResourceLoader module info type error for module \'' . $name . |
||
| 347 | '\': expected ResourceLoaderModule or array (got: ' . gettype( $info ) . ')' |
||
| 348 | ); |
||
| 349 | } |
||
| 350 | |||
| 351 | // Last-minute changes |
||
| 352 | |||
| 353 | // Apply custom skin-defined styles to existing modules. |
||
| 354 | if ( $this->isFileModule( $name ) ) { |
||
| 355 | foreach ( $moduleSkinStyles as $skinName => $skinStyles ) { |
||
| 356 | // If this module already defines skinStyles for this skin, ignore $wgResourceModuleSkinStyles. |
||
| 357 | if ( isset( $this->moduleInfos[$name]['skinStyles'][$skinName] ) ) { |
||
| 358 | continue; |
||
| 359 | } |
||
| 360 | |||
| 361 | // If $name is preceded with a '+', the defined style files will be added to 'default' |
||
| 362 | // skinStyles, otherwise 'default' will be ignored as it normally would be. |
||
| 363 | if ( isset( $skinStyles[$name] ) ) { |
||
| 364 | $paths = (array)$skinStyles[$name]; |
||
| 365 | $styleFiles = []; |
||
| 366 | } elseif ( isset( $skinStyles['+' . $name] ) ) { |
||
| 367 | $paths = (array)$skinStyles['+' . $name]; |
||
| 368 | $styleFiles = isset( $this->moduleInfos[$name]['skinStyles']['default'] ) ? |
||
| 369 | (array)$this->moduleInfos[$name]['skinStyles']['default'] : |
||
| 370 | []; |
||
| 371 | } else { |
||
| 372 | continue; |
||
| 373 | } |
||
| 374 | |||
| 375 | // Add new file paths, remapping them to refer to our directories and not use settings |
||
| 376 | // from the module we're modifying, which come from the base definition. |
||
| 377 | list( $localBasePath, $remoteBasePath ) = |
||
| 378 | ResourceLoaderFileModule::extractBasePaths( $skinStyles ); |
||
| 379 | |||
| 380 | foreach ( $paths as $path ) { |
||
| 381 | $styleFiles[] = new ResourceLoaderFilePath( $path, $localBasePath, $remoteBasePath ); |
||
| 382 | } |
||
| 383 | |||
| 384 | $this->moduleInfos[$name]['skinStyles'][$skinName] = $styleFiles; |
||
| 385 | } |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | */ |
||
| 393 | public function registerTestModules() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Add a foreign source of modules. |
||
| 433 | * |
||
| 434 | * Source IDs are typically the same as the Wiki ID or database name (e.g. lowercase a-z). |
||
| 435 | * |
||
| 436 | * @param array|string $id Source ID (string), or array( id1 => loadUrl, id2 => loadUrl, ... ) |
||
| 437 | * @param string|array $loadUrl load.php url (string), or array with loadUrl key for |
||
| 438 | * backwards-compatibility. |
||
| 439 | * @throws MWException |
||
| 440 | */ |
||
| 441 | public function addSource( $id, $loadUrl = null ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get a list of module names. |
||
| 474 | * |
||
| 475 | * @return array List of module names |
||
| 476 | */ |
||
| 477 | public function getModuleNames() { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get a list of test module names for one (or all) frameworks. |
||
| 483 | * |
||
| 484 | * If the given framework id is unknkown, or if the in-object variable is not an array, |
||
| 485 | * then it will return an empty array. |
||
| 486 | * |
||
| 487 | * @param string $framework Get only the test module names for one |
||
| 488 | * particular framework (optional) |
||
| 489 | * @return array |
||
| 490 | */ |
||
| 491 | public function getTestModuleNames( $framework = 'all' ) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Check whether a ResourceLoader module is registered |
||
| 506 | * |
||
| 507 | * @since 1.25 |
||
| 508 | * @param string $name |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | public function isModuleRegistered( $name ) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Get the ResourceLoaderModule object for a given module name. |
||
| 517 | * |
||
| 518 | * If an array of module parameters exists but a ResourceLoaderModule object has not |
||
| 519 | * yet been instantiated, this method will instantiate and cache that object such that |
||
| 520 | * subsequent calls simply return the same object. |
||
| 521 | * |
||
| 522 | * @param string $name Module name |
||
| 523 | * @return ResourceLoaderModule|null If module has been registered, return a |
||
| 524 | * ResourceLoaderModule instance. Otherwise, return null. |
||
| 525 | */ |
||
| 526 | public function getModule( $name ) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Return whether the definition of a module corresponds to a simple ResourceLoaderFileModule. |
||
| 558 | * |
||
| 559 | * @param string $name Module name |
||
| 560 | * @return bool |
||
| 561 | */ |
||
| 562 | protected function isFileModule( $name ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Get the list of sources. |
||
| 575 | * |
||
| 576 | * @return array Like array( id => load.php url, .. ) |
||
| 577 | */ |
||
| 578 | public function getSources() { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Get the URL to the load.php endpoint for the given |
||
| 584 | * ResourceLoader source |
||
| 585 | * |
||
| 586 | * @since 1.24 |
||
| 587 | * @param string $source |
||
| 588 | * @throws MWException On an invalid $source name |
||
| 589 | * @return string |
||
| 590 | */ |
||
| 591 | public function getLoadScript( $source ) { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @since 1.26 |
||
| 600 | * @param string $value |
||
| 601 | * @return string Hash |
||
| 602 | */ |
||
| 603 | public static function makeHash( $value ) { |
||
| 604 | $hash = hash( 'fnv132', $value ); |
||
| 605 | return Wikimedia\base_convert( $hash, 16, 36, 7 ); |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Helper method to get and combine versions of multiple modules. |
||
| 610 | * |
||
| 611 | * @since 1.26 |
||
| 612 | * @param ResourceLoaderContext $context |
||
| 613 | * @param array $modules List of ResourceLoaderModule objects |
||
| 614 | * @return string Hash |
||
| 615 | */ |
||
| 616 | public function getCombinedVersion( ResourceLoaderContext $context, array $modules ) { |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Output a response to a load request, including the content-type header. |
||
| 628 | * |
||
| 629 | * @param ResourceLoaderContext $context Context in which a response should be formed |
||
| 630 | */ |
||
| 631 | public function respond( ResourceLoaderContext $context ) { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Send main response headers to the client. |
||
| 752 | * |
||
| 753 | * Deals with Content-Type, CORS (for stylesheets), and caching. |
||
| 754 | * |
||
| 755 | * @param ResourceLoaderContext $context |
||
| 756 | * @param string $etag ETag header value |
||
| 757 | * @param bool $errors Whether there are errors in the response |
||
| 758 | * @return void |
||
| 759 | */ |
||
| 760 | protected function sendResponseHeaders( ResourceLoaderContext $context, $etag, $errors ) { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Respond with HTTP 304 Not Modified if appropiate. |
||
| 803 | * |
||
| 804 | * If there's an If-None-Match header, respond with a 304 appropriately |
||
| 805 | * and clear out the output buffer. If the client cache is too old then do nothing. |
||
| 806 | * |
||
| 807 | * @param ResourceLoaderContext $context |
||
| 808 | * @param string $etag ETag header value |
||
| 809 | * @return bool True if HTTP 304 was sent and output handled |
||
| 810 | */ |
||
| 811 | protected function tryRespondNotModified( ResourceLoaderContext $context, $etag ) { |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Send out code for a response from file cache if possible. |
||
| 838 | * |
||
| 839 | * @param ResourceFileCache $fileCache Cache object for this request URL |
||
| 840 | * @param ResourceLoaderContext $context Context in which to generate a response |
||
| 841 | * @param string $etag ETag header value |
||
| 842 | * @return bool If this found a cache file and handled the response |
||
| 843 | */ |
||
| 844 | protected function tryRespondFromFileCache( |
||
| 888 | |||
| 889 | /** |
||
| 890 | * Generate a CSS or JS comment block. |
||
| 891 | * |
||
| 892 | * Only use this for public data, not error message details. |
||
| 893 | * |
||
| 894 | * @param string $text |
||
| 895 | * @return string |
||
| 896 | */ |
||
| 897 | public static function makeComment( $text ) { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Handle exception display. |
||
| 904 | * |
||
| 905 | * @param Exception $e Exception to be shown to the user |
||
| 906 | * @return string Sanitized text in a CSS/JS comment that can be returned to the user |
||
| 907 | */ |
||
| 908 | public static function formatException( $e ) { |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Handle exception display. |
||
| 914 | * |
||
| 915 | * @since 1.25 |
||
| 916 | * @param Exception $e Exception to be shown to the user |
||
| 917 | * @return string Sanitized text that can be returned to the user |
||
| 918 | */ |
||
| 919 | protected static function formatExceptionNoComment( $e ) { |
||
| 928 | |||
| 929 | /** |
||
| 930 | * Generate code for a response. |
||
| 931 | * |
||
| 932 | * @param ResourceLoaderContext $context Context in which to generate a response |
||
| 933 | * @param ResourceLoaderModule[] $modules List of module objects keyed by module name |
||
| 934 | * @param string[] $missing List of requested module names that are unregistered (optional) |
||
| 935 | * @return string Response data |
||
| 936 | */ |
||
| 937 | public function makeModuleResponse( ResourceLoaderContext $context, |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Get names of modules that use a certain message. |
||
| 1055 | * |
||
| 1056 | * @param string $messageKey |
||
| 1057 | * @return array List of module names |
||
| 1058 | */ |
||
| 1059 | public function getModulesByMessage( $messageKey ) { |
||
| 1069 | |||
| 1070 | /* Static Methods */ |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Return JS code that calls mw.loader.implement with given module properties. |
||
| 1074 | * |
||
| 1075 | * @param string $name Module name |
||
| 1076 | * @param mixed $scripts List of URLs to JavaScript files or String of JavaScript code |
||
| 1077 | * @param mixed $styles Array of CSS strings keyed by media type, or an array of lists of URLs |
||
| 1078 | * to CSS files keyed by media type |
||
| 1079 | * @param mixed $messages List of messages associated with this module. May either be an |
||
| 1080 | * associative array mapping message key to value, or a JSON-encoded message blob containing |
||
| 1081 | * the same data, wrapped in an XmlJsCode object. |
||
| 1082 | * @param array $templates Keys are name of templates and values are the source of |
||
| 1083 | * the template. |
||
| 1084 | * @throws MWException |
||
| 1085 | * @return string |
||
| 1086 | */ |
||
| 1087 | public static function makeLoaderImplementScript( |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Returns JS code which, when called, will register a given list of messages. |
||
| 1122 | * |
||
| 1123 | * @param mixed $messages Either an associative array mapping message key to value, or a |
||
| 1124 | * JSON-encoded message blob containing the same data, wrapped in an XmlJsCode object. |
||
| 1125 | * @return string |
||
| 1126 | */ |
||
| 1127 | public static function makeMessageSetScript( $messages ) { |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Combines an associative array mapping media type to CSS into a |
||
| 1137 | * single stylesheet with "@media" blocks. |
||
| 1138 | * |
||
| 1139 | * @param array $stylePairs Array keyed by media type containing (arrays of) CSS strings |
||
| 1140 | * @return array |
||
| 1141 | */ |
||
| 1142 | public static function makeCombinedStyles( array $stylePairs ) { |
||
| 1168 | |||
| 1169 | /** |
||
| 1170 | * Returns a JS call to mw.loader.state, which sets the state of a |
||
| 1171 | * module or modules to a given value. Has two calling conventions: |
||
| 1172 | * |
||
| 1173 | * - ResourceLoader::makeLoaderStateScript( $name, $state ): |
||
| 1174 | * Set the state of a single module called $name to $state |
||
| 1175 | * |
||
| 1176 | * - ResourceLoader::makeLoaderStateScript( array( $name => $state, ... ) ): |
||
| 1177 | * Set the state of modules with the given names to the given states |
||
| 1178 | * |
||
| 1179 | * @param string $name |
||
| 1180 | * @param string $state |
||
| 1181 | * @return string |
||
| 1182 | */ |
||
| 1183 | View Code Duplication | public static function makeLoaderStateScript( $name, $state = null ) { |
|
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Returns JS code which calls the script given by $script. The script will |
||
| 1201 | * be called with local variables name, version, dependencies and group, |
||
| 1202 | * which will have values corresponding to $name, $version, $dependencies |
||
| 1203 | * and $group as supplied. |
||
| 1204 | * |
||
| 1205 | * @param string $name Module name |
||
| 1206 | * @param string $version Module version hash |
||
| 1207 | * @param array $dependencies List of module names on which this module depends |
||
| 1208 | * @param string $group Group which the module is in. |
||
| 1209 | * @param string $source Source of the module, or 'local' if not foreign. |
||
| 1210 | * @param string $script JavaScript code |
||
| 1211 | * @return string |
||
| 1212 | */ |
||
| 1213 | public static function makeCustomLoaderScript( $name, $version, $dependencies, |
||
| 1223 | |||
| 1224 | private static function isEmptyObject( stdClass $obj ) { |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Remove empty values from the end of an array. |
||
| 1233 | * |
||
| 1234 | * Values considered empty: |
||
| 1235 | * |
||
| 1236 | * - null |
||
| 1237 | * - array() |
||
| 1238 | * - new XmlJsCode( '{}' ) |
||
| 1239 | * - new stdClass() // (object) array() |
||
| 1240 | * |
||
| 1241 | * @param Array $array |
||
| 1242 | */ |
||
| 1243 | private static function trimArray( array &$array ) { |
||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * Returns JS code which calls mw.loader.register with the given |
||
| 1260 | * parameters. Has three calling conventions: |
||
| 1261 | * |
||
| 1262 | * - ResourceLoader::makeLoaderRegisterScript( $name, $version, |
||
| 1263 | * $dependencies, $group, $source, $skip |
||
| 1264 | * ): |
||
| 1265 | * Register a single module. |
||
| 1266 | * |
||
| 1267 | * - ResourceLoader::makeLoaderRegisterScript( array( $name1, $name2 ) ): |
||
| 1268 | * Register modules with the given names. |
||
| 1269 | * |
||
| 1270 | * - ResourceLoader::makeLoaderRegisterScript( array( |
||
| 1271 | * array( $name1, $version1, $dependencies1, $group1, $source1, $skip1 ), |
||
| 1272 | * array( $name2, $version2, $dependencies1, $group2, $source2, $skip2 ), |
||
| 1273 | * ... |
||
| 1274 | * ) ): |
||
| 1275 | * Registers modules with the given names and parameters. |
||
| 1276 | * |
||
| 1277 | * @param string $name Module name |
||
| 1278 | * @param string $version Module version hash |
||
| 1279 | * @param array $dependencies List of module names on which this module depends |
||
| 1280 | * @param string $group Group which the module is in |
||
| 1281 | * @param string $source Source of the module, or 'local' if not foreign |
||
| 1282 | * @param string $skip Script body of the skip function |
||
| 1283 | * @return string |
||
| 1284 | */ |
||
| 1285 | public static function makeLoaderRegisterScript( $name, $version = null, |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * Returns JS code which calls mw.loader.addSource() with the given |
||
| 1327 | * parameters. Has two calling conventions: |
||
| 1328 | * |
||
| 1329 | * - ResourceLoader::makeLoaderSourcesScript( $id, $properties ): |
||
| 1330 | * Register a single source |
||
| 1331 | * |
||
| 1332 | * - ResourceLoader::makeLoaderSourcesScript( array( $id1 => $loadUrl, $id2 => $loadUrl, ... ) ); |
||
| 1333 | * Register sources with the given IDs and properties. |
||
| 1334 | * |
||
| 1335 | * @param string $id Source ID |
||
| 1336 | * @param array $properties Source properties (see addSource()) |
||
| 1337 | * @return string |
||
| 1338 | */ |
||
| 1339 | View Code Duplication | public static function makeLoaderSourcesScript( $id, $properties = null ) { |
|
| 1354 | |||
| 1355 | /** |
||
| 1356 | * Returns JS code which runs given JS code if the client-side framework is |
||
| 1357 | * present. |
||
| 1358 | * |
||
| 1359 | * @deprecated since 1.25; use makeInlineScript instead |
||
| 1360 | * @param string $script JavaScript code |
||
| 1361 | * @return string |
||
| 1362 | */ |
||
| 1363 | public static function makeLoaderConditionalScript( $script ) { |
||
| 1367 | |||
| 1368 | /** |
||
| 1369 | * Construct an inline script tag with given JS code. |
||
| 1370 | * |
||
| 1371 | * The code will be wrapped in a closure, and it will be executed by ResourceLoader |
||
| 1372 | * only if the client has adequate support for MediaWiki JavaScript code. |
||
| 1373 | * |
||
| 1374 | * @param string $script JavaScript code |
||
| 1375 | * @return WrappedString HTML |
||
| 1376 | */ |
||
| 1377 | public static function makeInlineScript( $script ) { |
||
| 1385 | |||
| 1386 | /** |
||
| 1387 | * Returns JS code which will set the MediaWiki configuration array to |
||
| 1388 | * the given value. |
||
| 1389 | * |
||
| 1390 | * @param array $configuration List of configuration values keyed by variable name |
||
| 1391 | * @return string |
||
| 1392 | */ |
||
| 1393 | public static function makeConfigSetScript( array $configuration ) { |
||
| 1400 | |||
| 1401 | /** |
||
| 1402 | * Convert an array of module names to a packed query string. |
||
| 1403 | * |
||
| 1404 | * For example, array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ) |
||
| 1405 | * becomes 'foo.bar,baz|bar.baz,quux' |
||
| 1406 | * @param array $modules List of module names (strings) |
||
| 1407 | * @return string Packed query string |
||
| 1408 | */ |
||
| 1409 | public static function makePackedModulesString( $modules ) { |
||
| 1426 | |||
| 1427 | /** |
||
| 1428 | * Determine whether debug mode was requested |
||
| 1429 | * Order of priority is 1) request param, 2) cookie, 3) $wg setting |
||
| 1430 | * @return bool |
||
| 1431 | */ |
||
| 1432 | public static function inDebugMode() { |
||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * Reset static members used for caching. |
||
| 1444 | * |
||
| 1445 | * Global state and $wgRequest are evil, but we're using it right |
||
| 1446 | * now and sometimes we need to be able to force ResourceLoader to |
||
| 1447 | * re-evaluate the context because it has changed (e.g. in the test suite). |
||
| 1448 | */ |
||
| 1449 | public static function clearCache() { |
||
| 1452 | |||
| 1453 | /** |
||
| 1454 | * Build a load.php URL |
||
| 1455 | * |
||
| 1456 | * @since 1.24 |
||
| 1457 | * @param string $source Name of the ResourceLoader source |
||
| 1458 | * @param ResourceLoaderContext $context |
||
| 1459 | * @param array $extraQuery |
||
| 1460 | * @return string URL to load.php. May be protocol-relative if $wgLoadScript is, too. |
||
| 1461 | */ |
||
| 1462 | public function createLoaderURL( $source, ResourceLoaderContext $context, |
||
| 1470 | |||
| 1471 | /** |
||
| 1472 | * Helper for createLoaderURL() |
||
| 1473 | * |
||
| 1474 | * @since 1.24 |
||
| 1475 | * @see makeLoaderQuery |
||
| 1476 | * @param ResourceLoaderContext $context |
||
| 1477 | * @param array $extraQuery |
||
| 1478 | * @return array |
||
| 1479 | */ |
||
| 1480 | protected static function createLoaderQuery( ResourceLoaderContext $context, $extraQuery = [] ) { |
||
| 1494 | |||
| 1495 | /** |
||
| 1496 | * Build a query array (array representation of query string) for load.php. Helper |
||
| 1497 | * function for createLoaderURL(). |
||
| 1498 | * |
||
| 1499 | * @param array $modules |
||
| 1500 | * @param string $lang |
||
| 1501 | * @param string $skin |
||
| 1502 | * @param string $user |
||
| 1503 | * @param string $version |
||
| 1504 | * @param bool $debug |
||
| 1505 | * @param string $only |
||
| 1506 | * @param bool $printable |
||
| 1507 | * @param bool $handheld |
||
| 1508 | * @param array $extraQuery |
||
| 1509 | * |
||
| 1510 | * @return array |
||
| 1511 | */ |
||
| 1512 | public static function makeLoaderQuery( $modules, $lang, $skin, $user = null, |
||
| 1543 | |||
| 1544 | /** |
||
| 1545 | * Check a module name for validity. |
||
| 1546 | * |
||
| 1547 | * Module names may not contain pipes (|), commas (,) or exclamation marks (!) and can be |
||
| 1548 | * at most 255 bytes. |
||
| 1549 | * |
||
| 1550 | * @param string $moduleName Module name to check |
||
| 1551 | * @return bool Whether $moduleName is a valid module name |
||
| 1552 | */ |
||
| 1553 | public static function isValidModuleName( $moduleName ) { |
||
| 1556 | |||
| 1557 | /** |
||
| 1558 | * Returns LESS compiler set up for use with MediaWiki |
||
| 1559 | * |
||
| 1560 | * @since 1.27 |
||
| 1561 | * @param array $extraVars Associative array of extra (i.e., other than the |
||
| 1562 | * globally-configured ones) that should be used for compilation. |
||
| 1563 | * @throws MWException |
||
| 1564 | * @return Less_Parser |
||
| 1565 | */ |
||
| 1566 | public function getLessCompiler( $extraVars = [] ) { |
||
| 1584 | |||
| 1585 | /** |
||
| 1586 | * Get global LESS variables. |
||
| 1587 | * |
||
| 1588 | * @since 1.27 |
||
| 1589 | * @return array Map of variable names to string CSS values. |
||
| 1590 | */ |
||
| 1591 | public function getLessVars() { |
||
| 1599 | } |
||
| 1600 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.