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 Locale 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 Locale, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Locale |
||
29 | { |
||
30 | const FALLBACK_LOCALE = 'en_US'; |
||
31 | |||
32 | protected static $currentLocale = null; |
||
33 | protected static $currentTimeZone = null; |
||
34 | protected static $defaultTimeZone = null; |
||
35 | protected static $systemTimeZone = null; |
||
36 | protected static $userLocales = array(); |
||
37 | |||
38 | /** |
||
39 | * get the current active locale |
||
40 | * |
||
41 | * @return string current locale |
||
42 | */ |
||
43 | 30 | public static function getCurrent() |
|
52 | |||
53 | /** |
||
54 | * Set the current locale |
||
55 | * |
||
56 | * @param string $locale local code |
||
57 | * |
||
58 | * @return void |
||
59 | * |
||
60 | * @throws InvalidLocale |
||
61 | */ |
||
62 | 52 | public static function setCurrent($locale) |
|
67 | |||
68 | /** |
||
69 | * Get the current timezone |
||
70 | * |
||
71 | * @return \DateTimeZone current timezone |
||
72 | */ |
||
73 | 41 | public static function getTimeZone() |
|
89 | |||
90 | /** |
||
91 | * Set the current timezone |
||
92 | * |
||
93 | * @param \DateTimeZone $timeZone |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | 52 | public static function setTimeZone(\DateTimeZone $timeZone) |
|
101 | |||
102 | /** |
||
103 | * Instantiate a new DateTimeZone object for a timezone name, with fallback to UTC on error |
||
104 | * |
||
105 | * @param string $timeZoneName name of timezone |
||
106 | * |
||
107 | * @return \DateTimeZone |
||
108 | */ |
||
109 | 1 | protected static function newDateTimeZone($timeZoneName) |
|
119 | |||
120 | /** |
||
121 | * Get the default timezone as set in default_TZ config |
||
122 | * |
||
123 | * @return \DateTimeZone |
||
124 | */ |
||
125 | 1 | View Code Duplication | public static function getDefaultTimeZone() |
136 | |||
137 | /** |
||
138 | * Get the server timezone as set in server_TZ config |
||
139 | * |
||
140 | * @return \DateTimeZone |
||
141 | */ |
||
142 | View Code Duplication | public static function getSystemTimeZone() |
|
153 | |||
154 | /** |
||
155 | * @param string $name Name of language file to be loaded, without extension |
||
156 | * @param mixed $domain string: Module dirname; global language file will be loaded if |
||
157 | * $domain is set to 'global' or not specified |
||
158 | * array: example; array('Frameworks/moduleclasses/moduleadmin') |
||
159 | * @param string $language Language to be loaded, current language content will be loaded if not specified |
||
160 | * |
||
161 | * @return boolean |
||
162 | */ |
||
163 | 1 | public static function loadLanguage($name, $domain = '', $language = null) |
|
183 | |||
184 | /** |
||
185 | * @param string $domain module dirname to load, if null will load global locale |
||
186 | * @param string $forcedLocale Locale to be loaded, current language content will be loaded if not specified |
||
187 | * |
||
188 | * @return boolean |
||
189 | */ |
||
190 | 5 | public static function loadLocale($domain = null, $forcedLocale = null) |
|
233 | |||
234 | /** |
||
235 | * load locale for theme |
||
236 | * |
||
237 | * @param XoopsTheme $theme |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | 4 | public static function loadThemeLocale(XoopsTheme $theme) |
|
258 | |||
259 | /** |
||
260 | * @return boolean |
||
261 | */ |
||
262 | 2 | public static function loadMailerLocale() |
|
275 | |||
276 | /** |
||
277 | * @param string $key |
||
278 | * @param string $dirname |
||
279 | * @param array $params |
||
280 | * @return string |
||
281 | */ |
||
282 | 3 | public static function translate($key, $dirname = 'xoops', $params = []) |
|
288 | |||
289 | /** |
||
290 | * @param string $key |
||
291 | * @param string $dirname |
||
292 | * @param array $params |
||
293 | * @return string |
||
294 | */ |
||
295 | 1 | public static function translateTheme($key, $dirname = '', $params = []) |
|
301 | |||
302 | /** |
||
303 | * Returns the raw translation |
||
304 | * |
||
305 | * @param string $class |
||
306 | * @param string $key |
||
307 | * @return string |
||
308 | */ |
||
309 | 4 | private static function getMessage($class, $key) { |
|
317 | |||
318 | /** |
||
319 | * Formats a message using [[MessageFormatter]]. |
||
320 | * |
||
321 | * @copyright Copyright (c) 2008 Yii Software LLC |
||
322 | * |
||
323 | * @param string $message the message to be formatted. |
||
324 | * @param array $params the parameters that will be used to replace the corresponding placeholders in the message. |
||
325 | * @param string $language the language code (e.g. `en-US`, `en`). |
||
326 | * @return string the formatted message. |
||
327 | */ |
||
328 | 4 | private static function format($message, $params, $language) |
|
329 | { |
||
330 | 4 | $params = (array)$params; |
|
331 | 4 | if ($params === []) { |
|
332 | 3 | return $message; |
|
333 | } |
||
334 | |||
335 | 1 | if (preg_match('~{\s*[\d\w]+\s*,~u', $message)) { |
|
336 | $formatter = self::getMessageFormatter(); |
||
337 | $result = $formatter->format($message, $params, $language); |
||
338 | if ($result === false) { |
||
339 | $errorMessage = $formatter->getErrorMessage(); |
||
340 | \Xoops::getInstance()->logger()->warning("Formatting message for language '$language' failed with error: $errorMessage. The message being formatted was: $message.", [__METHOD__]); |
||
341 | return $message; |
||
342 | } else { |
||
343 | return $result; |
||
344 | } |
||
345 | } |
||
346 | |||
347 | 1 | $p = []; |
|
348 | 1 | foreach ($params as $name => $value) { |
|
349 | 1 | $p['{' . $name . '}'] = $value; |
|
350 | } |
||
351 | |||
352 | 1 | return strtr($message, $p); |
|
353 | } |
||
354 | |||
355 | /** |
||
356 | * Returns the message formatter instance. |
||
357 | * @return MessageFormatter the message formatter to be used to format message via ICU message format. |
||
358 | */ |
||
359 | private static function getMessageFormatter() |
||
367 | |||
368 | /** |
||
369 | * @param string $dirname |
||
370 | * |
||
371 | * @return string |
||
372 | */ |
||
373 | 3 | protected static function getClassFromDirname($dirname) |
|
377 | |||
378 | /** |
||
379 | * @param string $dirname |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | 1 | protected static function getThemeClassFromDirname($dirname = '') |
|
390 | |||
391 | /** |
||
392 | * getUserLocales() |
||
393 | * Returns the user locales |
||
394 | * Normally it returns an array like this: |
||
395 | * 1. Forced language |
||
396 | * 2. Language in $_GET['lang'] |
||
397 | * 3. Language in $_SESSION['lang'] |
||
398 | * 4. HTTP_ACCEPT_LANGUAGE |
||
399 | * 5. Fallback language |
||
400 | * Note: duplicate values are deleted. |
||
401 | * |
||
402 | * @return array with the user locales sorted by priority. Highest is best. |
||
403 | */ |
||
404 | 11 | public static function getUserLocales() |
|
445 | |||
446 | /** |
||
447 | * Convert a locale designation to a normal form ll_Ssss_CC, where |
||
448 | * ll is language code |
||
449 | * Ssss is the script code, if specified |
||
450 | * CC is the country code, if specified |
||
451 | * |
||
452 | * @param string $locale locale code |
||
453 | * @param string $separator string to use to join locale parts |
||
454 | * @param bool $withScript include script if specified, always remove if false |
||
455 | * |
||
456 | * @return string normalized locale, or empty string on error |
||
457 | */ |
||
458 | 69 | public static function normalizeLocale($locale, $separator = '_', $withScript = true) |
|
472 | |||
473 | /** |
||
474 | * Return a normalized form of a resource domain. A resource domain is always lowercase. |
||
475 | * |
||
476 | * @param string $domain resource domain (usually a module dirname) |
||
477 | * |
||
478 | * @return string normalized resource domain |
||
479 | */ |
||
480 | 1 | public static function normalizeDomain($domain) |
|
484 | } |
||
485 |
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: