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 | 31 | 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 | 53 | 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 | 53 | 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) |
|
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 | /** |
||
370 | * @param string $dirname |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | 3 | protected static function getClassFromDirname($dirname) |
|
378 | |||
379 | /** |
||
380 | * @param string $dirname |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | 1 | protected static function getThemeClassFromDirname($dirname = '') |
|
391 | |||
392 | /** |
||
393 | * getUserLocales() |
||
394 | * Returns the user locales |
||
395 | * Normally it returns an array like this: |
||
396 | * 1. Forced language |
||
397 | * 2. Language in $_GET['lang'] |
||
398 | * 3. Language in $_SESSION['lang'] |
||
399 | * 4. HTTP_ACCEPT_LANGUAGE |
||
400 | * 5. Fallback language |
||
401 | * Note: duplicate values are deleted. |
||
402 | * |
||
403 | * @return array with the user locales sorted by priority. Highest is best. |
||
404 | */ |
||
405 | 11 | public static function getUserLocales() |
|
446 | |||
447 | /** |
||
448 | * Convert a locale designation to a normal form ll_Ssss_CC, where |
||
449 | * ll is language code |
||
450 | * Ssss is the script code, if specified |
||
451 | * CC is the country code, if specified |
||
452 | * |
||
453 | * @param string $locale locale code |
||
454 | * @param string $separator string to use to join locale parts |
||
455 | * @param bool $withScript include script if specified, always remove if false |
||
456 | * |
||
457 | * @return string normalized locale, or empty string on error |
||
458 | */ |
||
459 | 70 | public static function normalizeLocale($locale, $separator = '_', $withScript = true) |
|
473 | |||
474 | /** |
||
475 | * Return a normalized form of a resource domain. A resource domain is always lowercase. |
||
476 | * |
||
477 | * @param string $domain resource domain (usually a module dirname) |
||
478 | * |
||
479 | * @return string normalized resource domain |
||
480 | */ |
||
481 | 1 | public static function normalizeDomain($domain) |
|
485 | } |
||
486 |
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.