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:
1 | <?php |
||
26 | class LocaleHelper { |
||
27 | /** |
||
28 | * @var string[] |
||
29 | */ |
||
30 | private $commonLanguages = [ |
||
31 | 'en', |
||
32 | 'es', |
||
33 | 'fr', |
||
34 | 'de', |
||
35 | 'de_DE', |
||
36 | 'ja', |
||
37 | 'ar', |
||
38 | 'ru', |
||
39 | 'nl', |
||
40 | 'it', |
||
41 | 'pt_BR', |
||
42 | 'pt_PT', |
||
43 | 'da', |
||
44 | 'fi_FI', |
||
45 | 'nb_NO', |
||
46 | 'sv', |
||
47 | 'tr', |
||
48 | 'zh_CN', |
||
49 | 'ko' |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @var string[] |
||
54 | */ |
||
55 | private $languageCodes = [ |
||
56 | 'el' => 'Ελληνικά', |
||
57 | 'en' => 'English', |
||
58 | 'fa' => 'فارسى', |
||
59 | 'fi_FI' => 'Suomi', |
||
60 | 'hi' => 'हिन्दी', |
||
61 | 'id' => 'Bahasa Indonesia', |
||
62 | 'lb' => 'Lëtzebuergesch', |
||
63 | 'ms_MY' => 'Bahasa Melayu', |
||
64 | 'nb_NO' => 'Norwegian Bokmål', |
||
65 | 'pt_BR' => 'Português brasileiro', |
||
66 | 'pt_PT' => 'Português', |
||
67 | 'ro' => 'română', |
||
68 | 'sr@latin' => 'Srpski', |
||
69 | 'sv' => 'Svenska', |
||
70 | 'hu_HU' => 'Magyar', |
||
71 | 'hr' => 'Hrvatski', |
||
72 | 'ar' => 'العربية', |
||
73 | 'lv' => 'Latviešu', |
||
74 | 'mk' => 'македонски', |
||
75 | 'uk' => 'Українська', |
||
76 | 'vi' => 'Tiếng Việt', |
||
77 | 'zh_TW' => '正體中文(臺灣)', |
||
78 | 'af_ZA' => 'Afrikaans', |
||
79 | 'bn_BD' => 'Bengali', |
||
80 | 'ta_LK' => 'தமிழ்', |
||
81 | 'zh_HK' => '繁體中文(香港)', |
||
82 | 'is' => 'Icelandic', |
||
83 | 'ka_GE' => 'Georgian for Georgia', |
||
84 | 'ku_IQ' => 'Kurdish Iraq', |
||
85 | 'si_LK' => 'Sinhala', |
||
86 | 'be' => 'Belarusian', |
||
87 | 'ka' => 'Kartuli (Georgian)', |
||
88 | 'my_MM' => 'Burmese - MYANMAR', |
||
89 | 'ur_PK' => 'Urdu (Pakistan)' |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * Get available languages split to the current, common and the rest |
||
94 | * |
||
95 | * @param IFactory $langFactory |
||
96 | * @param string $activeLangCode |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | public function getNormalizedLanguages(IFactory $langFactory, $activeLangCode) { |
||
146 | |||
147 | /** |
||
148 | * Get common language weight or false if language is not common |
||
149 | * |
||
150 | * @param string $languageCode |
||
151 | * |
||
152 | * @return false|int |
||
153 | */ |
||
154 | public function getCommonLanguageWeight($languageCode) { |
||
157 | |||
158 | /** |
||
159 | * Get language name by code |
||
160 | * |
||
161 | * @param string $code |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | public function getLanguageNameByCode($code) { |
||
170 | |||
171 | /** |
||
172 | * Compare language arrays by name |
||
173 | * both arrays should have 'code' and 'name' keys |
||
174 | * |
||
175 | * @param string[] $a |
||
176 | * @param string[] $b |
||
177 | * |
||
178 | * @return int |
||
179 | */ |
||
180 | protected function compareLanguagesByName($a, $b) { |
||
192 | } |
||
193 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.