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 ResourceLoaderModule 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 ResourceLoaderModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | abstract class ResourceLoaderModule implements LoggerAwareInterface { |
||
| 34 | # Type of resource |
||
| 35 | const TYPE_SCRIPTS = 'scripts'; |
||
| 36 | const TYPE_STYLES = 'styles'; |
||
| 37 | const TYPE_COMBINED = 'combined'; |
||
| 38 | |||
| 39 | # Desired load type |
||
| 40 | // Module only has styles (loaded via <style> or <link rel=stylesheet>) |
||
| 41 | const LOAD_STYLES = 'styles'; |
||
| 42 | // Module may have other resources (loaded via mw.loader from a script) |
||
| 43 | const LOAD_GENERAL = 'general'; |
||
| 44 | |||
| 45 | # sitewide core module like a skin file or jQuery component |
||
| 46 | const ORIGIN_CORE_SITEWIDE = 1; |
||
| 47 | |||
| 48 | # per-user module generated by the software |
||
| 49 | const ORIGIN_CORE_INDIVIDUAL = 2; |
||
| 50 | |||
| 51 | # sitewide module generated from user-editable files, like MediaWiki:Common.js, or |
||
| 52 | # modules accessible to multiple users, such as those generated by the Gadgets extension. |
||
| 53 | const ORIGIN_USER_SITEWIDE = 3; |
||
| 54 | |||
| 55 | # per-user module generated from user-editable files, like User:Me/vector.js |
||
| 56 | const ORIGIN_USER_INDIVIDUAL = 4; |
||
| 57 | |||
| 58 | # an access constant; make sure this is kept as the largest number in this group |
||
| 59 | const ORIGIN_ALL = 10; |
||
| 60 | |||
| 61 | # script and style modules form a hierarchy of trustworthiness, with core modules like |
||
| 62 | # skins and jQuery as most trustworthy, and user scripts as least trustworthy. We can |
||
| 63 | # limit the types of scripts and styles we allow to load on, say, sensitive special |
||
| 64 | # pages like Special:UserLogin and Special:Preferences |
||
| 65 | protected $origin = self::ORIGIN_CORE_SITEWIDE; |
||
| 66 | |||
| 67 | /* Protected Members */ |
||
| 68 | |||
| 69 | protected $name = null; |
||
| 70 | protected $targets = [ 'desktop' ]; |
||
| 71 | |||
| 72 | // In-object cache for file dependencies |
||
| 73 | protected $fileDeps = []; |
||
| 74 | // In-object cache for message blob (keyed by language) |
||
| 75 | protected $msgBlobs = []; |
||
| 76 | // In-object cache for version hash |
||
| 77 | protected $versionHash = []; |
||
| 78 | // In-object cache for module content |
||
| 79 | protected $contents = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var Config |
||
| 83 | */ |
||
| 84 | protected $config; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array|bool |
||
| 88 | */ |
||
| 89 | protected $deprecated = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var LoggerInterface |
||
| 93 | */ |
||
| 94 | protected $logger; |
||
| 95 | |||
| 96 | /* Methods */ |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get this module's name. This is set when the module is registered |
||
| 100 | * with ResourceLoader::register() |
||
| 101 | * |
||
| 102 | * @return string|null Name (string) or null if no name was set |
||
| 103 | */ |
||
| 104 | public function getName() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set this module's name. This is called by ResourceLoader::register() |
||
| 110 | * when registering the module. Other code should not call this. |
||
| 111 | * |
||
| 112 | * @param string $name Name |
||
| 113 | */ |
||
| 114 | public function setName( $name ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get this module's origin. This is set when the module is registered |
||
| 120 | * with ResourceLoader::register() |
||
| 121 | * |
||
| 122 | * @return int ResourceLoaderModule class constant, the subclass default |
||
| 123 | * if not set manually |
||
| 124 | */ |
||
| 125 | public function getOrigin() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param ResourceLoaderContext $context |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function getFlip( $context ) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get JS representing deprecation information for the current module if available |
||
| 141 | * |
||
| 142 | * @return string JavaScript code |
||
| 143 | */ |
||
| 144 | protected function getDeprecationInformation() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get all JS for this module for a given language and skin. |
||
| 163 | * Includes all relevant JS except loader scripts. |
||
| 164 | * |
||
| 165 | * @param ResourceLoaderContext $context |
||
| 166 | * @return string JavaScript code |
||
| 167 | */ |
||
| 168 | public function getScript( ResourceLoaderContext $context ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Takes named templates by the module and returns an array mapping. |
||
| 175 | * |
||
| 176 | * @return array of templates mapping template alias to content |
||
| 177 | */ |
||
| 178 | public function getTemplates() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return Config |
||
| 185 | * @since 1.24 |
||
| 186 | */ |
||
| 187 | View Code Duplication | public function getConfig() { |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param Config $config |
||
| 198 | * @since 1.24 |
||
| 199 | */ |
||
| 200 | public function setConfig( Config $config ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @since 1.27 |
||
| 206 | * @param LoggerInterface $logger |
||
| 207 | * @return null |
||
| 208 | */ |
||
| 209 | public function setLogger( LoggerInterface $logger ) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @since 1.27 |
||
| 215 | * @return LoggerInterface |
||
| 216 | */ |
||
| 217 | protected function getLogger() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get the URL or URLs to load for this module's JS in debug mode. |
||
| 226 | * The default behavior is to return a load.php?only=scripts URL for |
||
| 227 | * the module, but file-based modules will want to override this to |
||
| 228 | * load the files directly. |
||
| 229 | * |
||
| 230 | * This function is called only when 1) we're in debug mode, 2) there |
||
| 231 | * is no only= parameter and 3) supportsURLLoading() returns true. |
||
| 232 | * #2 is important to prevent an infinite loop, therefore this function |
||
| 233 | * MUST return either an only= URL or a non-load.php URL. |
||
| 234 | * |
||
| 235 | * @param ResourceLoaderContext $context |
||
| 236 | * @return array Array of URLs |
||
| 237 | */ |
||
| 238 | View Code Duplication | public function getScriptURLsForDebug( ResourceLoaderContext $context ) { |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Whether this module supports URL loading. If this function returns false, |
||
| 255 | * getScript() will be used even in cases (debug mode, no only param) where |
||
| 256 | * getScriptURLsForDebug() would normally be used instead. |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function supportsURLLoading() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get all CSS for this module for a given skin. |
||
| 265 | * |
||
| 266 | * @param ResourceLoaderContext $context |
||
| 267 | * @return array List of CSS strings or array of CSS strings keyed by media type. |
||
| 268 | * like [ 'screen' => '.foo { width: 0 }' ]; |
||
| 269 | * or [ 'screen' => [ '.foo { width: 0 }' ] ]; |
||
| 270 | */ |
||
| 271 | public function getStyles( ResourceLoaderContext $context ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the URL or URLs to load for this module's CSS in debug mode. |
||
| 278 | * The default behavior is to return a load.php?only=styles URL for |
||
| 279 | * the module, but file-based modules will want to override this to |
||
| 280 | * load the files directly. See also getScriptURLsForDebug() |
||
| 281 | * |
||
| 282 | * @param ResourceLoaderContext $context |
||
| 283 | * @return array [ mediaType => [ URL1, URL2, ... ], ... ] |
||
| 284 | */ |
||
| 285 | View Code Duplication | public function getStyleURLsForDebug( ResourceLoaderContext $context ) { |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get the messages needed for this module. |
||
| 302 | * |
||
| 303 | * To get a JSON blob with messages, use MessageBlobStore::get() |
||
| 304 | * |
||
| 305 | * @return array List of message keys. Keys may occur more than once |
||
| 306 | */ |
||
| 307 | public function getMessages() { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the group this module is in. |
||
| 314 | * |
||
| 315 | * @return string Group name |
||
| 316 | */ |
||
| 317 | public function getGroup() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get the origin of this module. Should only be overridden for foreign modules. |
||
| 324 | * |
||
| 325 | * @return string Origin name, 'local' for local modules |
||
| 326 | */ |
||
| 327 | public function getSource() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Where on the HTML page should this module's JS be loaded? |
||
| 334 | * - 'top': in the "<head>" |
||
| 335 | * - 'bottom': at the bottom of the "<body>" |
||
| 336 | * |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | public function getPosition() { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Whether this module's JS expects to work without the client-side ResourceLoader module. |
||
| 345 | * Returning true from this function will prevent mw.loader.state() call from being |
||
| 346 | * appended to the bottom of the script. |
||
| 347 | * |
||
| 348 | * @return bool |
||
| 349 | */ |
||
| 350 | public function isRaw() { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get a list of modules this module depends on. |
||
| 356 | * |
||
| 357 | * Dependency information is taken into account when loading a module |
||
| 358 | * on the client side. |
||
| 359 | * |
||
| 360 | * Note: It is expected that $context will be made non-optional in the near |
||
| 361 | * future. |
||
| 362 | * |
||
| 363 | * @param ResourceLoaderContext $context |
||
| 364 | * @return array List of module names as strings |
||
| 365 | */ |
||
| 366 | public function getDependencies( ResourceLoaderContext $context = null ) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile'] |
||
| 373 | * |
||
| 374 | * @return array Array of strings |
||
| 375 | */ |
||
| 376 | public function getTargets() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get the module's load type. |
||
| 382 | * |
||
| 383 | * @since 1.28 |
||
| 384 | * @return string ResourceLoaderModule LOAD_* constant |
||
| 385 | */ |
||
| 386 | public function getType() { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Get the skip function. |
||
| 392 | * |
||
| 393 | * Modules that provide fallback functionality can provide a "skip function". This |
||
| 394 | * function, if provided, will be passed along to the module registry on the client. |
||
| 395 | * When this module is loaded (either directly or as a dependency of another module), |
||
| 396 | * then this function is executed first. If the function returns true, the module will |
||
| 397 | * instantly be considered "ready" without requesting the associated module resources. |
||
| 398 | * |
||
| 399 | * The value returned here must be valid javascript for execution in a private function. |
||
| 400 | * It must not contain the "function () {" and "}" wrapper though. |
||
| 401 | * |
||
| 402 | * @return string|null A JavaScript function body returning a boolean value, or null |
||
| 403 | */ |
||
| 404 | public function getSkipFunction() { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get the files this module depends on indirectly for a given skin. |
||
| 410 | * |
||
| 411 | * These are only image files referenced by the module's stylesheet. |
||
| 412 | * |
||
| 413 | * @param ResourceLoaderContext $context |
||
| 414 | * @return array List of files |
||
| 415 | */ |
||
| 416 | protected function getFileDependencies( ResourceLoaderContext $context ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Set in-object cache for file dependencies. |
||
| 444 | * |
||
| 445 | * This is used to retrieve data in batches. See ResourceLoader::preloadModuleInfo(). |
||
| 446 | * To save the data, use saveFileDependencies(). |
||
| 447 | * |
||
| 448 | * @param ResourceLoaderContext $context |
||
| 449 | * @param string[] $files Array of file names |
||
| 450 | */ |
||
| 451 | public function setFileDependencies( ResourceLoaderContext $context, $files ) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Set the files this module depends on indirectly for a given skin. |
||
| 458 | * |
||
| 459 | * @since 1.27 |
||
| 460 | * @param ResourceLoaderContext $context |
||
| 461 | * @param array $localFileRefs List of files |
||
| 462 | */ |
||
| 463 | protected function saveFileDependencies( ResourceLoaderContext $context, $localFileRefs ) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Make file paths relative to MediaWiki directory. |
||
| 506 | * |
||
| 507 | * This is used to make file paths safe for storing in a database without the paths |
||
| 508 | * becoming stale or incorrect when MediaWiki is moved or upgraded (T111481). |
||
| 509 | * |
||
| 510 | * @since 1.27 |
||
| 511 | * @param array $filePaths |
||
| 512 | * @return array |
||
| 513 | */ |
||
| 514 | public static function getRelativePaths( array $filePaths ) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Expand directories relative to $IP. |
||
| 523 | * |
||
| 524 | * @since 1.27 |
||
| 525 | * @param array $filePaths |
||
| 526 | * @return array |
||
| 527 | */ |
||
| 528 | public static function expandRelativePaths( array $filePaths ) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Get the hash of the message blob. |
||
| 537 | * |
||
| 538 | * @since 1.27 |
||
| 539 | * @param ResourceLoaderContext $context |
||
| 540 | * @return string|null JSON blob or null if module has no messages |
||
| 541 | */ |
||
| 542 | protected function getMessageBlob( ResourceLoaderContext $context ) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Set in-object cache for message blobs. |
||
| 561 | * |
||
| 562 | * Used to allow fetching of message blobs in batches. See ResourceLoader::preloadModuleInfo(). |
||
| 563 | * |
||
| 564 | * @since 1.27 |
||
| 565 | * @param string|null $blob JSON blob or null |
||
| 566 | * @param string $lang Language code |
||
| 567 | */ |
||
| 568 | public function setMessageBlob( $blob, $lang ) { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Get module-specific LESS variables, if any. |
||
| 574 | * |
||
| 575 | * @since 1.27 |
||
| 576 | * @param ResourceLoaderContext $context |
||
| 577 | * @return array Module-specific LESS variables. |
||
| 578 | */ |
||
| 579 | protected function getLessVars( ResourceLoaderContext $context ) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Get an array of this module's resources. Ready for serving to the web. |
||
| 585 | * |
||
| 586 | * @since 1.26 |
||
| 587 | * @param ResourceLoaderContext $context |
||
| 588 | * @return array |
||
| 589 | */ |
||
| 590 | public function getModuleContent( ResourceLoaderContext $context ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Bundle all resources attached to this module into an array. |
||
| 602 | * |
||
| 603 | * @since 1.26 |
||
| 604 | * @param ResourceLoaderContext $context |
||
| 605 | * @return array |
||
| 606 | */ |
||
| 607 | final protected function buildContent( ResourceLoaderContext $context ) { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Get a string identifying the current version of this module in a given context. |
||
| 705 | * |
||
| 706 | * Whenever anything happens that changes the module's response (e.g. scripts, styles, and |
||
| 707 | * messages) this value must change. This value is used to store module responses in cache. |
||
| 708 | * (Both client-side and server-side.) |
||
| 709 | * |
||
| 710 | * It is not recommended to override this directly. Use getDefinitionSummary() instead. |
||
| 711 | * If overridden, one must call the parent getVersionHash(), append data and re-hash. |
||
| 712 | * |
||
| 713 | * This method should be quick because it is frequently run by ResourceLoaderStartUpModule to |
||
| 714 | * propagate changes to the client and effectively invalidate cache. |
||
| 715 | * |
||
| 716 | * For backward-compatibility, the following optional data providers are automatically included: |
||
| 717 | * |
||
| 718 | * - getModifiedTime() |
||
| 719 | * - getModifiedHash() |
||
| 720 | * |
||
| 721 | * @since 1.26 |
||
| 722 | * @param ResourceLoaderContext $context |
||
| 723 | * @return string Hash (should use ResourceLoader::makeHash) |
||
| 724 | */ |
||
| 725 | public function getVersionHash( ResourceLoaderContext $context ) { |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Whether to generate version hash based on module content. |
||
| 772 | * |
||
| 773 | * If a module requires database or file system access to build the module |
||
| 774 | * content, consider disabling this in favour of manually tracking relevant |
||
| 775 | * aspects in getDefinitionSummary(). See getVersionHash() for how this is used. |
||
| 776 | * |
||
| 777 | * @return bool |
||
| 778 | */ |
||
| 779 | public function enableModuleContentVersion() { |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Get the definition summary for this module. |
||
| 785 | * |
||
| 786 | * This is the method subclasses are recommended to use to track values in their |
||
| 787 | * version hash. Call this in getVersionHash() and pass it to e.g. json_encode. |
||
| 788 | * |
||
| 789 | * Subclasses must call the parent getDefinitionSummary() and build on that. |
||
| 790 | * It is recommended that each subclass appends its own new array. This prevents |
||
| 791 | * clashes or accidental overwrites of existing keys and gives each subclass |
||
| 792 | * its own scope for simple array keys. |
||
| 793 | * |
||
| 794 | * @code |
||
| 795 | * $summary = parent::getDefinitionSummary( $context ); |
||
| 796 | * $summary[] = [ |
||
| 797 | * 'foo' => 123, |
||
| 798 | * 'bar' => 'quux', |
||
| 799 | * ]; |
||
| 800 | * return $summary; |
||
| 801 | * @endcode |
||
| 802 | * |
||
| 803 | * Return an array containing values from all significant properties of this |
||
| 804 | * module's definition. |
||
| 805 | * |
||
| 806 | * Be careful not to normalise too much. Especially preserve the order of things |
||
| 807 | * that carry significance in getScript and getStyles (T39812). |
||
| 808 | * |
||
| 809 | * Avoid including things that are insiginificant (e.g. order of message keys is |
||
| 810 | * insignificant and should be sorted to avoid unnecessary cache invalidation). |
||
| 811 | * |
||
| 812 | * This data structure must exclusively contain arrays and scalars as values (avoid |
||
| 813 | * object instances) to allow simple serialisation using json_encode. |
||
| 814 | * |
||
| 815 | * If modules have a hash or timestamp from another source, that may be incuded as-is. |
||
| 816 | * |
||
| 817 | * A number of utility methods are available to help you gather data. These are not |
||
| 818 | * called by default and must be included by the subclass' getDefinitionSummary(). |
||
| 819 | * |
||
| 820 | * - getMessageBlob() |
||
| 821 | * |
||
| 822 | * @since 1.23 |
||
| 823 | * @param ResourceLoaderContext $context |
||
| 824 | * @return array|null |
||
| 825 | */ |
||
| 826 | public function getDefinitionSummary( ResourceLoaderContext $context ) { |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Get this module's last modification timestamp for a given context. |
||
| 835 | * |
||
| 836 | * @deprecated since 1.26 Use getDefinitionSummary() instead |
||
| 837 | * @param ResourceLoaderContext $context Context object |
||
| 838 | * @return int|null UNIX timestamp |
||
| 839 | */ |
||
| 840 | public function getModifiedTime( ResourceLoaderContext $context ) { |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Helper method for providing a version hash to getVersionHash(). |
||
| 846 | * |
||
| 847 | * @deprecated since 1.26 Use getDefinitionSummary() instead |
||
| 848 | * @param ResourceLoaderContext $context |
||
| 849 | * @return string|null Hash |
||
| 850 | */ |
||
| 851 | public function getModifiedHash( ResourceLoaderContext $context ) { |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Back-compat dummy for old subclass implementations of getModifiedTime(). |
||
| 857 | * |
||
| 858 | * This method used to use ObjectCache to track when a hash was first seen. That principle |
||
| 859 | * stems from a time that ResourceLoader could only identify module versions by timestamp. |
||
| 860 | * That is no longer the case. Use getDefinitionSummary() directly. |
||
| 861 | * |
||
| 862 | * @deprecated since 1.26 Superseded by getVersionHash() |
||
| 863 | * @param ResourceLoaderContext $context |
||
| 864 | * @return int UNIX timestamp |
||
| 865 | */ |
||
| 866 | public function getHashMtime( ResourceLoaderContext $context ) { |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Back-compat dummy for old subclass implementations of getModifiedTime(). |
||
| 876 | * |
||
| 877 | * @since 1.23 |
||
| 878 | * @deprecated since 1.26 Superseded by getVersionHash() |
||
| 879 | * @param ResourceLoaderContext $context |
||
| 880 | * @return int UNIX timestamp |
||
| 881 | */ |
||
| 882 | public function getDefinitionMtime( ResourceLoaderContext $context ) { |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Check whether this module is known to be empty. If a child class |
||
| 892 | * has an easy and cheap way to determine that this module is |
||
| 893 | * definitely going to be empty, it should override this method to |
||
| 894 | * return true in that case. Callers may optimize the request for this |
||
| 895 | * module away if this function returns true. |
||
| 896 | * @param ResourceLoaderContext $context |
||
| 897 | * @return bool |
||
| 898 | */ |
||
| 899 | public function isKnownEmpty( ResourceLoaderContext $context ) { |
||
| 902 | |||
| 903 | /** @var JSParser Lazy-initialized; use self::javaScriptParser() */ |
||
| 904 | private static $jsParser; |
||
| 905 | private static $parseCacheVersion = 1; |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Validate a given script file; if valid returns the original source. |
||
| 909 | * If invalid, returns replacement JS source that throws an exception. |
||
| 910 | * |
||
| 911 | * @param string $fileName |
||
| 912 | * @param string $contents |
||
| 913 | * @return string JS with the original, or a replacement error |
||
| 914 | */ |
||
| 915 | protected function validateScriptFile( $fileName, $contents ) { |
||
| 947 | |||
| 948 | /** |
||
| 949 | * @return JSParser |
||
| 950 | */ |
||
| 951 | protected static function javaScriptParser() { |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist. |
||
| 960 | * Defaults to 1. |
||
| 961 | * |
||
| 962 | * @param string $filePath File path |
||
| 963 | * @return int UNIX timestamp |
||
| 964 | */ |
||
| 965 | protected static function safeFilemtime( $filePath ) { |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Compute a non-cryptographic string hash of a file's contents. |
||
| 974 | * If the file does not exist or cannot be read, returns an empty string. |
||
| 975 | * |
||
| 976 | * @since 1.26 Uses MD4 instead of SHA1. |
||
| 977 | * @param string $filePath File path |
||
| 978 | * @return string Hash |
||
| 979 | */ |
||
| 980 | protected static function safeFileHash( $filePath ) { |
||
| 983 | } |
||
| 984 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: