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 SiteConfiguration 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 SiteConfiguration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
122 | class SiteConfiguration { |
||
123 | |||
124 | /** |
||
125 | * Array of suffixes, for self::siteFromDB() |
||
126 | */ |
||
127 | public $suffixes = []; |
||
128 | |||
129 | /** |
||
130 | * Array of wikis, should be the same as $wgLocalDatabases |
||
131 | */ |
||
132 | public $wikis = []; |
||
133 | |||
134 | /** |
||
135 | * The whole array of settings |
||
136 | */ |
||
137 | public $settings = []; |
||
138 | |||
139 | /** |
||
140 | * Array of domains that are local and can be handled by the same server |
||
141 | * |
||
142 | * @deprecated since 1.25; use $wgLocalVirtualHosts instead. |
||
143 | */ |
||
144 | public $localVHosts = []; |
||
145 | |||
146 | /** |
||
147 | * Optional callback to load full configuration data. |
||
148 | * @var string|array |
||
149 | */ |
||
150 | public $fullLoadCallback = null; |
||
151 | |||
152 | /** Whether or not all data has been loaded */ |
||
153 | public $fullLoadDone = false; |
||
154 | |||
155 | /** |
||
156 | * A callback function that returns an array with the following keys (all |
||
157 | * optional): |
||
158 | * - suffix: site's suffix |
||
159 | * - lang: site's lang |
||
160 | * - tags: array of wiki tags |
||
161 | * - params: array of parameters to be replaced |
||
162 | * The function will receive the SiteConfiguration instance in the first |
||
163 | * argument and the wiki in the second one. |
||
164 | * if suffix and lang are passed they will be used for the return value of |
||
165 | * self::siteFromDB() and self::$suffixes will be ignored |
||
166 | * |
||
167 | * @var string|array |
||
168 | */ |
||
169 | public $siteParamsCallback = null; |
||
170 | |||
171 | /** |
||
172 | * Configuration cache for getConfig() |
||
173 | * @var array |
||
174 | */ |
||
175 | protected $cfgCache = []; |
||
176 | |||
177 | /** |
||
178 | * Retrieves a configuration setting for a given wiki. |
||
179 | * @param string $settingName ID of the setting name to retrieve |
||
180 | * @param string $wiki Wiki ID of the wiki in question. |
||
181 | * @param string $suffix The suffix of the wiki in question. |
||
182 | * @param array $params List of parameters. $.'key' is replaced by $value in all returned data. |
||
183 | * @param array $wikiTags The tags assigned to the wiki. |
||
184 | * @return mixed The value of the setting requested. |
||
185 | */ |
||
186 | public function get( $settingName, $wiki, $suffix = null, $params = [], |
||
192 | |||
193 | /** |
||
194 | * Really retrieves a configuration setting for a given wiki. |
||
195 | * |
||
196 | * @param string $settingName ID of the setting name to retrieve. |
||
197 | * @param string $wiki Wiki ID of the wiki in question. |
||
198 | * @param array $params Array of parameters. |
||
199 | * @return mixed The value of the setting requested. |
||
200 | */ |
||
201 | protected function getSetting( $settingName, $wiki, array $params ) { |
||
269 | |||
270 | /** |
||
271 | * Type-safe string replace; won't do replacements on non-strings |
||
272 | * private? |
||
273 | * |
||
274 | * @param string $from |
||
275 | * @param string $to |
||
276 | * @param string|array $in |
||
277 | * @return string |
||
278 | */ |
||
279 | function doReplace( $from, $to, $in ) { |
||
291 | |||
292 | /** |
||
293 | * Gets all settings for a wiki |
||
294 | * @param string $wiki Wiki ID of the wiki in question. |
||
295 | * @param string $suffix The suffix of the wiki in question. |
||
296 | * @param array $params List of parameters. $.'key' is replaced by $value in all returned data. |
||
297 | * @param array $wikiTags The tags assigned to the wiki. |
||
298 | * @return array Array of settings requested. |
||
299 | */ |
||
300 | public function getAll( $wiki, $suffix = null, $params = [], $wikiTags = [] ) { |
||
321 | |||
322 | /** |
||
323 | * Retrieves a configuration setting for a given wiki, forced to a boolean. |
||
324 | * @param string $setting ID of the setting name to retrieve |
||
325 | * @param string $wiki Wiki ID of the wiki in question. |
||
326 | * @param string $suffix The suffix of the wiki in question. |
||
327 | * @param array $wikiTags The tags assigned to the wiki. |
||
328 | * @return bool The value of the setting requested. |
||
329 | */ |
||
330 | public function getBool( $setting, $wiki, $suffix = null, $wikiTags = [] ) { |
||
333 | |||
334 | /** |
||
335 | * Retrieves an array of local databases |
||
336 | * |
||
337 | * @return array |
||
338 | */ |
||
339 | function &getLocalDatabases() { |
||
342 | |||
343 | /** |
||
344 | * Retrieves the value of a given setting, and places it in a variable passed by reference. |
||
345 | * @param string $setting ID of the setting name to retrieve |
||
346 | * @param string $wiki Wiki ID of the wiki in question. |
||
347 | * @param string $suffix The suffix of the wiki in question. |
||
348 | * @param array $var Reference The variable to insert the value into. |
||
349 | * @param array $params List of parameters. $.'key' is replaced by $value in all returned data. |
||
350 | * @param array $wikiTags The tags assigned to the wiki. |
||
351 | */ |
||
352 | public function extractVar( $setting, $wiki, $suffix, &$var, |
||
360 | |||
361 | /** |
||
362 | * Retrieves the value of a given setting, and places it in its corresponding global variable. |
||
363 | * @param string $setting ID of the setting name to retrieve |
||
364 | * @param string $wiki Wiki ID of the wiki in question. |
||
365 | * @param string $suffix The suffix of the wiki in question. |
||
366 | * @param array $params List of parameters. $.'key' is replaced by $value in all returned data. |
||
367 | * @param array $wikiTags The tags assigned to the wiki. |
||
368 | */ |
||
369 | public function extractGlobal( $setting, $wiki, $suffix = null, |
||
375 | |||
376 | /** |
||
377 | * @param string $setting |
||
378 | * @param string $wiki |
||
379 | * @param array $params |
||
380 | */ |
||
381 | public function extractGlobalSetting( $setting, $wiki, $params ) { |
||
396 | |||
397 | /** |
||
398 | * Retrieves the values of all settings, and places them in their corresponding global variables. |
||
399 | * @param string $wiki Wiki ID of the wiki in question. |
||
400 | * @param string $suffix The suffix of the wiki in question. |
||
401 | * @param array $params List of parameters. $.'key' is replaced by $value in all returned data. |
||
402 | * @param array $wikiTags The tags assigned to the wiki. |
||
403 | */ |
||
404 | public function extractAllGlobals( $wiki, $suffix = null, $params = [], |
||
412 | |||
413 | /** |
||
414 | * Return specific settings for $wiki |
||
415 | * See the documentation of self::$siteParamsCallback for more in-depth |
||
416 | * documentation about this function |
||
417 | * |
||
418 | * @param string $wiki |
||
419 | * @return array |
||
420 | */ |
||
421 | protected function getWikiParams( $wiki ) { |
||
447 | |||
448 | /** |
||
449 | * Merge params between the ones passed to the function and the ones given |
||
450 | * by self::$siteParamsCallback for backward compatibility |
||
451 | * Values returned by self::getWikiParams() have the priority. |
||
452 | * |
||
453 | * @param string $wiki Wiki ID of the wiki in question. |
||
454 | * @param string $suffix The suffix of the wiki in question. |
||
455 | * @param array $params List of parameters. $.'key' is replaced by $value in |
||
456 | * all returned data. |
||
457 | * @param array $wikiTags The tags assigned to the wiki. |
||
458 | * @return array |
||
459 | */ |
||
460 | protected function mergeParams( $wiki, $suffix, array $params, array $wikiTags ) { |
||
481 | |||
482 | /** |
||
483 | * Work out the site and language name from a database name |
||
484 | * @param string $db |
||
485 | * |
||
486 | * @return array |
||
487 | */ |
||
488 | public function siteFromDB( $db ) { |
||
511 | |||
512 | /** |
||
513 | * Get the resolved (post-setup) configuration of a potentially foreign wiki. |
||
514 | * For foreign wikis, this is expensive, and only works if maintenance |
||
515 | * scripts are setup to handle the --wiki parameter such as in wiki farms. |
||
516 | * |
||
517 | * @param string $wiki |
||
518 | * @param array|string $settings A setting name or array of setting names |
||
519 | * @return mixed|mixed[] Array if $settings is an array, otherwise the value |
||
520 | * @throws MWException |
||
521 | * @since 1.21 |
||
522 | */ |
||
523 | public function getConfig( $wiki, $settings ) { |
||
572 | |||
573 | /** |
||
574 | * Merge multiple arrays together. |
||
575 | * On encountering duplicate keys, merge the two, but ONLY if they're arrays. |
||
576 | * PHP's array_merge_recursive() merges ANY duplicate values into arrays, |
||
577 | * which is not fun |
||
578 | * |
||
579 | * @param array $array1 |
||
580 | * |
||
581 | * @return array |
||
582 | */ |
||
583 | static function arrayMerge( $array1/* ... */ ) { |
||
584 | $out = $array1; |
||
585 | $argsCount = func_num_args(); |
||
586 | for ( $i = 1; $i < $argsCount; $i++ ) { |
||
587 | foreach ( func_get_arg( $i ) as $key => $value ) { |
||
588 | if ( isset( $out[$key] ) && is_array( $out[$key] ) && is_array( $value ) ) { |
||
589 | $out[$key] = self::arrayMerge( $out[$key], $value ); |
||
590 | } elseif ( !isset( $out[$key] ) || !$out[$key] && !is_numeric( $key ) ) { |
||
591 | // Values that evaluate to true given precedence, for the |
||
592 | // primary purpose of merging permissions arrays. |
||
593 | $out[$key] = $value; |
||
594 | } elseif ( is_numeric( $key ) ) { |
||
595 | $out[] = $value; |
||
596 | } |
||
597 | } |
||
598 | } |
||
599 | |||
600 | return $out; |
||
601 | } |
||
602 | |||
603 | public function loadFullData() { |
||
609 | } |
||
610 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: