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 LocalisationCache 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 LocalisationCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class LocalisationCache { |
||
| 42 | const VERSION = 4; |
||
| 43 | |||
| 44 | /** Configuration associative array */ |
||
| 45 | private $conf; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * True if recaching should only be done on an explicit call to recache(). |
||
| 49 | * Setting this reduces the overhead of cache freshness checking, which |
||
| 50 | * requires doing a stat() for every extension i18n file. |
||
| 51 | */ |
||
| 52 | private $manualRecache = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * True to treat all files as expired until they are regenerated by this object. |
||
| 56 | */ |
||
| 57 | private $forceRecache = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The cache data. 3-d array, where the first key is the language code, |
||
| 61 | * the second key is the item key e.g. 'messages', and the third key is |
||
| 62 | * an item specific subkey index. Some items are not arrays and so for those |
||
| 63 | * items, there are no subkeys. |
||
| 64 | */ |
||
| 65 | protected $data = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The persistent store object. An instance of LCStore. |
||
| 69 | * |
||
| 70 | * @var LCStore |
||
| 71 | */ |
||
| 72 | private $store; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * A 2-d associative array, code/key, where presence indicates that the item |
||
| 76 | * is loaded. Value arbitrary. |
||
| 77 | * |
||
| 78 | * For split items, if set, this indicates that all of the subitems have been |
||
| 79 | * loaded. |
||
| 80 | */ |
||
| 81 | private $loadedItems = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * A 3-d associative array, code/key/subkey, where presence indicates that |
||
| 85 | * the subitem is loaded. Only used for the split items, i.e. messages. |
||
| 86 | */ |
||
| 87 | private $loadedSubitems = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * An array where presence of a key indicates that that language has been |
||
| 91 | * initialised. Initialisation includes checking for cache expiry and doing |
||
| 92 | * any necessary updates. |
||
| 93 | */ |
||
| 94 | private $initialisedLangs = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * An array mapping non-existent pseudo-languages to fallback languages. This |
||
| 98 | * is filled by initShallowFallback() when data is requested from a language |
||
| 99 | * that lacks a Messages*.php file. |
||
| 100 | */ |
||
| 101 | private $shallowFallbacks = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * An array where the keys are codes that have been recached by this instance. |
||
| 105 | */ |
||
| 106 | private $recachedLangs = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * All item keys |
||
| 110 | */ |
||
| 111 | static public $allKeys = [ |
||
| 112 | 'fallback', 'namespaceNames', 'bookstoreList', |
||
| 113 | 'magicWords', 'messages', 'rtl', 'capitalizeAllNouns', 'digitTransformTable', |
||
| 114 | 'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension', |
||
| 115 | 'linkTrail', 'linkPrefixCharset', 'namespaceAliases', |
||
| 116 | 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap', |
||
| 117 | 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases', |
||
| 118 | 'imageFiles', 'preloadedMessages', 'namespaceGenderAliases', |
||
| 119 | 'digitGroupingPattern', 'pluralRules', 'pluralRuleTypes', 'compiledPluralRules', |
||
| 120 | ]; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Keys for items which consist of associative arrays, which may be merged |
||
| 124 | * by a fallback sequence. |
||
| 125 | */ |
||
| 126 | static public $mergeableMapKeys = [ 'messages', 'namespaceNames', |
||
| 127 | 'namespaceAliases', 'dateFormats', 'imageFiles', 'preloadedMessages' |
||
| 128 | ]; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Keys for items which are a numbered array. |
||
| 132 | */ |
||
| 133 | static public $mergeableListKeys = [ 'extraUserToggles' ]; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Keys for items which contain an array of arrays of equivalent aliases |
||
| 137 | * for each subitem. The aliases may be merged by a fallback sequence. |
||
| 138 | */ |
||
| 139 | static public $mergeableAliasListKeys = [ 'specialPageAliases' ]; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Keys for items which contain an associative array, and may be merged if |
||
| 143 | * the primary value contains the special array key "inherit". That array |
||
| 144 | * key is removed after the first merge. |
||
| 145 | */ |
||
| 146 | static public $optionalMergeKeys = [ 'bookstoreList' ]; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Keys for items that are formatted like $magicWords |
||
| 150 | */ |
||
| 151 | static public $magicWordKeys = [ 'magicWords' ]; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Keys for items where the subitems are stored in the backend separately. |
||
| 155 | */ |
||
| 156 | static public $splitKeys = [ 'messages' ]; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Keys which are loaded automatically by initLanguage() |
||
| 160 | */ |
||
| 161 | static public $preloadedKeys = [ 'dateFormats', 'namespaceNames' ]; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Associative array of cached plural rules. The key is the language code, |
||
| 165 | * the value is an array of plural rules for that language. |
||
| 166 | */ |
||
| 167 | private $pluralRules = null; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Associative array of cached plural rule types. The key is the language |
||
| 171 | * code, the value is an array of plural rule types for that language. For |
||
| 172 | * example, $pluralRuleTypes['ar'] = ['zero', 'one', 'two', 'few', 'many']. |
||
| 173 | * The index for each rule type matches the index for the rule in |
||
| 174 | * $pluralRules, thus allowing correlation between the two. The reason we |
||
| 175 | * don't just use the type names as the keys in $pluralRules is because |
||
| 176 | * Language::convertPlural applies the rules based on numeric order (or |
||
| 177 | * explicit numeric parameter), not based on the name of the rule type. For |
||
| 178 | * example, {{plural:count|wordform1|wordform2|wordform3}}, rather than |
||
| 179 | * {{plural:count|one=wordform1|two=wordform2|many=wordform3}}. |
||
| 180 | */ |
||
| 181 | private $pluralRuleTypes = null; |
||
| 182 | |||
| 183 | private $mergeableKeys = null; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Constructor. |
||
| 187 | * For constructor parameters, see the documentation in DefaultSettings.php |
||
| 188 | * for $wgLocalisationCacheConf. |
||
| 189 | * |
||
| 190 | * @param array $conf |
||
| 191 | * @throws MWException |
||
| 192 | */ |
||
| 193 | function __construct( $conf ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns true if the given key is mergeable, that is, if it is an associative |
||
| 246 | * array which can be merged through a fallback sequence. |
||
| 247 | * @param string $key |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function isMergeableKey( $key ) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get a cache item. |
||
| 266 | * |
||
| 267 | * Warning: this may be slow for split items (messages), since it will |
||
| 268 | * need to fetch all of the subitems from the cache individually. |
||
| 269 | * @param string $code |
||
| 270 | * @param string $key |
||
| 271 | * @return mixed |
||
| 272 | */ |
||
| 273 | public function getItem( $code, $key ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get a subitem, for instance a single message for a given language. |
||
| 287 | * @param string $code |
||
| 288 | * @param string $key |
||
| 289 | * @param string $subkey |
||
| 290 | * @return mixed|null |
||
| 291 | */ |
||
| 292 | public function getSubitem( $code, $key, $subkey ) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get the list of subitem keys for a given item. |
||
| 308 | * |
||
| 309 | * This is faster than array_keys($lc->getItem(...)) for the items listed in |
||
| 310 | * self::$splitKeys. |
||
| 311 | * |
||
| 312 | * Will return null if the item is not found, or false if the item is not an |
||
| 313 | * array. |
||
| 314 | * @param string $code |
||
| 315 | * @param string $key |
||
| 316 | * @return bool|null|string |
||
| 317 | */ |
||
| 318 | public function getSubitemList( $code, $key ) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Load an item into the cache. |
||
| 333 | * @param string $code |
||
| 334 | * @param string $key |
||
| 335 | */ |
||
| 336 | protected function loadItem( $code, $key ) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Load a subitem into the cache |
||
| 369 | * @param string $code |
||
| 370 | * @param string $key |
||
| 371 | * @param string $subkey |
||
| 372 | */ |
||
| 373 | protected function loadSubitem( $code, $key, $subkey ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Returns true if the cache identified by $code is missing or expired. |
||
| 404 | * |
||
| 405 | * @param string $code |
||
| 406 | * |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | public function isExpired( $code ) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Initialise a language in this object. Rebuild the cache if necessary. |
||
| 444 | * @param string $code |
||
| 445 | * @throws MWException |
||
| 446 | */ |
||
| 447 | protected function initLanguage( $code ) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Create a fallback from one language to another, without creating a |
||
| 504 | * complete persistent cache. |
||
| 505 | * @param string $primaryCode |
||
| 506 | * @param string $fallbackCode |
||
| 507 | */ |
||
| 508 | public function initShallowFallback( $primaryCode, $fallbackCode ) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Read a PHP file containing localisation data. |
||
| 517 | * @param string $_fileName |
||
| 518 | * @param string $_fileType |
||
| 519 | * @throws MWException |
||
| 520 | * @return array |
||
| 521 | */ |
||
| 522 | protected function readPHPFile( $_fileName, $_fileType ) { |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Read a JSON file containing localisation messages. |
||
| 547 | * @param string $fileName Name of file to read |
||
| 548 | * @throws MWException If there is a syntax error in the JSON file |
||
| 549 | * @return array Array with a 'messages' key, or empty array if the file doesn't exist |
||
| 550 | */ |
||
| 551 | public function readJSONFile( $fileName ) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Get the compiled plural rules for a given language from the XML files. |
||
| 579 | * @since 1.20 |
||
| 580 | * @param string $code |
||
| 581 | * @return array|null |
||
| 582 | */ |
||
| 583 | public function getCompiledPluralRules( $code ) { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Get the plural rules for a given language from the XML files. |
||
| 601 | * Cached. |
||
| 602 | * @since 1.20 |
||
| 603 | * @param string $code |
||
| 604 | * @return array|null |
||
| 605 | */ |
||
| 606 | View Code Duplication | public function getPluralRules( $code ) { |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Get the plural rule types for a given language from the XML files. |
||
| 619 | * Cached. |
||
| 620 | * @since 1.22 |
||
| 621 | * @param string $code |
||
| 622 | * @return array|null |
||
| 623 | */ |
||
| 624 | View Code Duplication | public function getPluralRuleTypes( $code ) { |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Load the plural XML files. |
||
| 637 | */ |
||
| 638 | protected function loadPluralFiles() { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Load a plural XML file with the given filename, compile the relevant |
||
| 652 | * rules, and save the compiled rules in a process-local cache. |
||
| 653 | * |
||
| 654 | * @param string $fileName |
||
| 655 | * @throws MWException |
||
| 656 | */ |
||
| 657 | protected function loadPluralFile( $fileName ) { |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Read the data from the source files for a given language, and register |
||
| 689 | * the relevant dependencies in the $deps array. If the localisation |
||
| 690 | * exists, the data array is returned, otherwise false is returned. |
||
| 691 | * |
||
| 692 | * @param string $code |
||
| 693 | * @param array $deps |
||
| 694 | * @return array |
||
| 695 | */ |
||
| 696 | protected function readSourceFilesAndRegisterDeps( $code, &$deps ) { |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Merge two localisation values, a primary and a fallback, overwriting the |
||
| 723 | * primary value in place. |
||
| 724 | * @param string $key |
||
| 725 | * @param mixed $value |
||
| 726 | * @param mixed $fallbackValue |
||
| 727 | */ |
||
| 728 | protected function mergeItem( $key, &$value, $fallbackValue ) { |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @param mixed $value |
||
| 756 | * @param mixed $fallbackValue |
||
| 757 | */ |
||
| 758 | protected function mergeMagicWords( &$value, $fallbackValue ) { |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Given an array mapping language code to localisation value, such as is |
||
| 774 | * found in extension *.i18n.php files, iterate through a fallback sequence |
||
| 775 | * to merge the given data with an existing primary value. |
||
| 776 | * |
||
| 777 | * Returns true if any data from the extension array was used, false |
||
| 778 | * otherwise. |
||
| 779 | * @param array $codeSequence |
||
| 780 | * @param string $key |
||
| 781 | * @param mixed $value |
||
| 782 | * @param mixed $fallbackValue |
||
| 783 | * @return bool |
||
| 784 | */ |
||
| 785 | protected function mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue ) { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Gets the combined list of messages dirs from |
||
| 799 | * core and extensions |
||
| 800 | * |
||
| 801 | * @since 1.25 |
||
| 802 | * @return array |
||
| 803 | */ |
||
| 804 | public function getMessagesDirs() { |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Load localisation data for a given language for both core and extensions |
||
| 818 | * and save it to the persistent cache store and the process cache |
||
| 819 | * @param string $code |
||
| 820 | * @throws MWException |
||
| 821 | */ |
||
| 822 | public function recache( $code ) { |
||
| 1038 | |||
| 1039 | /** |
||
| 1040 | * Build the preload item from the given pre-cache data. |
||
| 1041 | * |
||
| 1042 | * The preload item will be loaded automatically, improving performance |
||
| 1043 | * for the commonly-requested items it contains. |
||
| 1044 | * @param array $data |
||
| 1045 | * @return array |
||
| 1046 | */ |
||
| 1047 | protected function buildPreload( $data ) { |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Unload the data for a given language from the object cache. |
||
| 1067 | * Reduces memory usage. |
||
| 1068 | * @param string $code |
||
| 1069 | */ |
||
| 1070 | public function unload( $code ) { |
||
| 1083 | |||
| 1084 | /** |
||
| 1085 | * Unload all data |
||
| 1086 | */ |
||
| 1087 | public function unloadAll() { |
||
| 1092 | |||
| 1093 | /** |
||
| 1094 | * Disable the storage backend |
||
| 1095 | */ |
||
| 1096 | public function disableBackend() { |
||
| 1100 | |||
| 1101 | } |
||
| 1102 |
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: