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 Loader { |
||
27 | /** |
||
28 | * Loader instance |
||
29 | * |
||
30 | * @access private |
||
31 | * @static |
||
32 | * @var Loader |
||
33 | */ |
||
34 | private static $_instance; |
||
35 | |||
36 | /** |
||
37 | * Default gettext domain to use. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | private $default_domain = ''; |
||
42 | |||
43 | /** |
||
44 | * Configured locale. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | private $locale = ''; |
||
49 | |||
50 | /** |
||
51 | * Loaded domains |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | private $domains = array(); |
||
56 | |||
57 | /** |
||
58 | * Bound paths for domains |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | private $paths = array('' => './'); |
||
63 | |||
64 | /** |
||
65 | * Returns the singleton Loader object |
||
66 | * |
||
67 | * @return Loader object |
||
68 | */ |
||
69 | 4 | public static function getInstance() |
|
76 | |||
77 | /** |
||
78 | * Loads global localizaton functions. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | 2 | public static function load_functions() |
|
86 | |||
87 | /** |
||
88 | * Figure out all possible locale names and start with the most |
||
89 | * specific ones. I.e. for sr_CS.UTF-8@latin, look through all of |
||
90 | * sr_CS.UTF-8@latin, sr_CS@latin, sr@latin, sr_CS.UTF-8, sr_CS, sr. |
||
91 | * |
||
92 | * @param string $locale Locale code |
||
93 | * |
||
94 | * @return array list of locales to try for any POSIX-style locale specification. |
||
95 | */ |
||
96 | 18 | public static function list_locales($locale) { |
|
97 | 18 | $locale_names = array(); |
|
98 | |||
99 | 18 | $lang = NULL; |
|
100 | 18 | $country = NULL; |
|
101 | 18 | $charset = NULL; |
|
102 | 18 | $modifier = NULL; |
|
103 | |||
104 | 18 | if ($locale) { |
|
105 | 17 | if (preg_match("/^(?P<lang>[a-z]{2,3})" // language code |
|
106 | ."(?:_(?P<country>[A-Z]{2}))?" // country code |
||
107 | 17 | ."(?:\.(?P<charset>[-A-Za-z0-9_]+))?" // charset |
|
108 | 17 | ."(?:@(?P<modifier>[-A-Za-z0-9_]+))?$/", // @ modifier |
|
109 | 17 | $locale, $matches)) { |
|
110 | |||
111 | 16 | extract($matches); |
|
112 | |||
113 | 16 | if ($modifier) { |
|
114 | 5 | View Code Duplication | if ($country) { |
1 ignored issue
–
show
|
|||
115 | 2 | if ($charset) { |
|
116 | 2 | array_push($locale_names, "${lang}_$country.$charset@$modifier"); |
|
117 | 2 | } |
|
118 | 2 | array_push($locale_names, "${lang}_$country@$modifier"); |
|
119 | 5 | } elseif ($charset) { |
|
120 | 1 | array_push($locale_names, "${lang}.$charset@$modifier"); |
|
121 | 1 | } |
|
122 | 5 | array_push($locale_names, "$lang@$modifier"); |
|
123 | 5 | } |
|
124 | View Code Duplication | if ($country) { |
|
1 ignored issue
–
show
|
|||
125 | if ($charset) { |
||
126 | array_push($locale_names, "${lang}_$country.$charset"); |
||
127 | } |
||
128 | array_push($locale_names, "${lang}_$country"); |
||
129 | 16 | } elseif ($charset) { |
|
130 | 2 | array_push($locale_names, "${lang}.$charset"); |
|
131 | 2 | } |
|
132 | 16 | array_push($locale_names, $lang); |
|
133 | 16 | } |
|
134 | |||
135 | // If the locale name doesn't match POSIX style, just include it as-is. |
||
136 | 17 | if (!in_array($locale, $locale_names)) { |
|
137 | 1 | array_push($locale_names, $locale); |
|
138 | 1 | } |
|
139 | 17 | } |
|
140 | 18 | return $locale_names; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Returns Translator object for domain or for default domain |
||
145 | * |
||
146 | * @param string $domain Translation domain |
||
147 | * |
||
148 | * @return Translator |
||
149 | */ |
||
150 | public function get_translator($domain = '') |
||
181 | |||
182 | /** |
||
183 | * Sets the path for a domain. |
||
184 | * |
||
185 | * @param string $domain Domain name |
||
186 | * @param string $path Path where to find locales |
||
187 | * |
||
188 | * @return void |
||
189 | */ |
||
190 | public function bindtextdomain($domain, $path) |
||
194 | |||
195 | /** |
||
196 | * Sets the default domain. |
||
197 | * |
||
198 | * @param string $domain Domain name |
||
199 | * |
||
200 | * @return void |
||
201 | */ |
||
202 | public function textdomain($domain) |
||
206 | |||
207 | /** |
||
208 | * Sets a requested locale |
||
209 | * |
||
210 | * @param string $locale Locale name |
||
211 | * |
||
212 | * @return string Set or current locale |
||
213 | */ |
||
214 | public function setlocale($locale) |
||
225 | |||
226 | /** |
||
227 | * Detects currently configured locale |
||
228 | * |
||
229 | * It checks: |
||
230 | * |
||
231 | * - global lang variable |
||
232 | * - environment for LC_ALL, LC_MESSAGES and LANG |
||
233 | * |
||
234 | * @return string with locale name |
||
235 | */ |
||
236 | public function detectlocale() |
||
249 | } |
||
250 |
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.