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 PMF_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 PMF_Language, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class PMF_Language |
||
40 | { |
||
41 | /** |
||
42 | * The accepted language of the user agend |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | public $acceptedLanguage = ''; |
||
47 | |||
48 | /** |
||
49 | * The current language |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | public static $language = ''; |
||
54 | |||
55 | /** |
||
56 | * @var PMF_Configuration |
||
57 | */ |
||
58 | private $config; |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * |
||
63 | * @param PMF_Configuration $config |
||
64 | * |
||
65 | * @return PMF_Language |
||
66 | */ |
||
67 | public function __construct(PMF_Configuration $config) |
||
68 | { |
||
69 | $this->config = $config; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns an array of country codes for a specific FAQ record ID, |
||
74 | * specific category ID or all languages used by FAQ records , categories |
||
75 | * |
||
76 | * @param integer $id ID |
||
77 | * @param string $table Specifies table |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | public function languageAvailable($id, $table = 'faqdata') |
||
82 | { |
||
83 | $output = array(); |
||
84 | |||
85 | if (isset($id)) { |
||
86 | if ($id == 0) { |
||
87 | // get languages for all ids |
||
88 | $distinct = ' DISTINCT '; |
||
89 | $where = ''; |
||
90 | } else { |
||
91 | // get languages for specified id |
||
92 | $distinct = ''; |
||
93 | $where = " WHERE id = ".$id; |
||
94 | } |
||
95 | |||
96 | $query = sprintf(" |
||
97 | SELECT %s |
||
98 | lang |
||
99 | FROM |
||
100 | %s%s |
||
101 | %s", |
||
102 | $distinct, |
||
103 | PMF_Db::getTablePrefix(), |
||
104 | $table, |
||
105 | $where |
||
106 | ); |
||
107 | |||
108 | $result = $this->config->getDb()->query($query); |
||
109 | |||
110 | if ($this->config->getDb()->numRows($result) > 0) { |
||
111 | while ($row = $this->config->getDb()->fetchObject($result)) { |
||
112 | $output[] = $row->lang; |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | return $output; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Sets the current language for phpMyFAQ user session |
||
122 | * |
||
123 | * @param bool $configDetection Configuration detection |
||
124 | * @param string $configLanguage Language from configuration |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public function setLanguage($configDetection, $configLanguage) |
||
191 | |||
192 | /** |
||
193 | * Returns the current language |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getLanguage() |
||
201 | |||
202 | /** |
||
203 | * This function returns the available languages |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | public static function getAvailableLanguages() |
||
235 | |||
236 | /** |
||
237 | * This function displays the <select> box for the available languages |
||
238 | * optionally filtered by excluding some provided languages |
||
239 | * |
||
240 | * @param string $default |
||
241 | * @param boolean $submitOnChange |
||
242 | * @param array $excludedLanguages |
||
243 | * @param string $id |
||
244 | * @return string |
||
245 | */ |
||
246 | public static function selectLanguages($default, $submitOnChange = false, Array $excludedLanguages = array(), $id = 'language') |
||
270 | |||
271 | /** |
||
272 | * Function for displaying all languages in <option> |
||
273 | * |
||
274 | * @param string $lang the languange to be selected |
||
275 | * @param bool $onlyThisLang print only the passed language? |
||
276 | * @param bool $fileLanguageValue print the <language file> instead of the <language code> as value? |
||
277 | * @return string |
||
278 | */ |
||
279 | public static function languageOptions($lang = '', $onlyThisLang = false, $fileLanguageValue = false) |
||
308 | |||
309 | /** |
||
310 | * True if the language is supported by the current phpMyFAQ installation |
||
311 | * |
||
312 | * @param string $langcode Language code |
||
313 | * @return boolean |
||
314 | */ |
||
315 | public static function isASupportedLanguage($langcode) |
||
320 | |||
321 | /** |
||
322 | * True if the language is supported by the bundled TinyMCE editor |
||
323 | * |
||
324 | * TinyMCE Language is supported if there is a language file present in |
||
325 | * PMF_ROOT/admin/editor/langs/$langcode.js |
||
326 | * |
||
327 | * TinyMCE language packs can be downloaded from |
||
328 | * http://tinymce.moxiecode.com/download_i18n.php |
||
329 | * and extracted to PMF_ROOT/admin/editor |
||
330 | * |
||
331 | * @param string $langcode Language code |
||
332 | * |
||
333 | * @return boolean |
||
334 | */ |
||
335 | public static function isASupportedTinyMCELanguage($langcode) |
||
341 | |||
342 | |||
343 | /** |
||
344 | * Gets the accepted language from the user agent |
||
345 | * |
||
346 | * $_SERVER['HTTP_ACCEPT_LANGUAGE'] could be like the text below: |
||
347 | * it,pt-br;q=0.8,en-us;q=0.5,en;q=0.3 |
||
348 | * |
||
349 | * @return void |
||
350 | */ |
||
351 | private function _getUserAgentLanguage() |
||
379 | } |