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 Language 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 Language, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Language |
||
38 | { |
||
39 | /** |
||
40 | * The accepted language of the user agend. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | public $acceptedLanguage = ''; |
||
45 | |||
46 | /** |
||
47 | * The current language. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | public static $language = ''; |
||
52 | |||
53 | /** |
||
54 | * @var Configuration |
||
55 | */ |
||
56 | private $config; |
||
57 | |||
58 | /** |
||
59 | * Constructor. |
||
60 | * |
||
61 | * @param Configuration $config |
||
62 | */ |
||
63 | public function __construct(Configuration $config) |
||
67 | |||
68 | /** |
||
69 | * Returns an array of country codes for a specific FAQ record ID, |
||
70 | * specific category ID or all languages used by FAQ records , categories. |
||
71 | * |
||
72 | * @param int $id ID |
||
73 | * @param string $table Specifies table |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | public function languageAvailable(int $id, string $table = 'faqdata'): array |
||
115 | |||
116 | /** |
||
117 | * Sets the current language for phpMyFAQ user session. |
||
118 | * |
||
119 | * @param bool $configDetection Configuration detection |
||
120 | * @param string $configLanguage Language from configuration |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public function setLanguage(bool $configDetection, string $configLanguage): string |
||
187 | |||
188 | /** |
||
189 | * Returns the current language. |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public function getLanguage(): string |
||
197 | |||
198 | /** |
||
199 | * This function returns the available languages. |
||
200 | * |
||
201 | * @return array |
||
202 | */ |
||
203 | public static function getAvailableLanguages(): array |
||
232 | |||
233 | /** |
||
234 | * This function displays the <select> box for the available languages |
||
235 | * optionally filtered by excluding some provided languages. |
||
236 | * |
||
237 | * @param string $default |
||
238 | * @param bool $submitOnChange |
||
239 | * @param array $excludedLanguages |
||
240 | * @param string $id |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public static function selectLanguages( |
||
273 | |||
274 | /** |
||
275 | * Function for displaying all languages in <option>. |
||
276 | * |
||
277 | * @param string $lang the languange to be selected |
||
278 | * @param bool $onlyThisLang print only the passed language? |
||
279 | * @param bool $fileLanguageValue print the <language file> instead of the <language code> as value? |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | public static function languageOptions( |
||
316 | |||
317 | /** |
||
318 | * True if the language is supported by the current phpMyFAQ installation. |
||
319 | * |
||
320 | * @param string|null $langCode Language code |
||
321 | * |
||
322 | * @return bool |
||
323 | */ |
||
324 | public static function isASupportedLanguage($langCode): bool |
||
330 | |||
331 | /** |
||
332 | * True if the language is supported by the bundled TinyMCE editor. |
||
333 | * |
||
334 | * TinyMCE Language is supported if there is a language file present in |
||
335 | * ROOT/admin/editor/langs/$langcode.js |
||
336 | * |
||
337 | * TinyMCE language packs can be downloaded from |
||
338 | * http://tinymce.moxiecode.com/download_i18n.php |
||
339 | * and extracted to ROOT/admin/editor |
||
340 | * |
||
341 | * @param string|null $langCode Language code |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | public static function isASupportedTinyMCELanguage($langCode): bool |
||
351 | |||
352 | /** |
||
353 | * Gets the accepted language from the user agent. |
||
354 | * |
||
355 | * $_SERVER['HTTP_ACCEPT_LANGUAGE'] could be like the text below: |
||
356 | * it,pt-br;q=0.8,en-us;q=0.5,en;q=0.3 |
||
357 | */ |
||
358 | private function getUserAgentLanguage() |
||
388 | } |
||
389 |