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 |
||
16 | class Gettext |
||
17 | { |
||
18 | /** @var string domain name match with file contains translation */ |
||
19 | private $_textDomain; |
||
20 | |||
21 | /** @var string character encoding in which the messages are */ |
||
22 | private $_catalogCodeSet; |
||
23 | |||
24 | /** @var string the path for a domain */ |
||
25 | private $_localeDir; |
||
26 | |||
27 | /** @var string|array locale or array of locales */ |
||
28 | private $_locale; |
||
29 | |||
30 | /** @var int constant specifying the category of the functions affected by the locale setting */ |
||
31 | private $_category; |
||
32 | |||
33 | /** |
||
34 | * Initialize Codeigniter PHP framework and get configuration |
||
35 | * |
||
36 | * @codeCoverageIgnore |
||
37 | * @param array $config Override default configuration |
||
38 | */ |
||
39 | public function __construct(array $config = array()) |
||
40 | { |
||
41 | log_message('info', 'Gettext Library Class Initialized'); |
||
42 | |||
43 | $this->_setConfig($config); |
||
44 | |||
45 | $this |
||
46 | ->_bindTextDomainCodeSet() |
||
47 | ->_bindTextDomain() |
||
48 | ->_textDomain() |
||
49 | ->_setLocale() |
||
50 | ->_putEnv() |
||
51 | ->_checkLocaleFile() |
||
52 | ; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Load a domain |
||
57 | * @param string $domain |
||
58 | */ |
||
59 | 3 | public function changeDomain($domain) |
|
72 | |||
73 | /** |
||
74 | * Merge config as parameter and default config (config/gettext.php file) |
||
75 | * @param array $config |
||
76 | */ |
||
77 | 19 | private function _setConfig(array $config = array()) |
|
78 | { |
||
79 | 19 | $this->_localeDir = isset($config['gettext_locale_dir']) |
|
80 | 19 | ? $config['gettext_locale_dir'] : config_item('gettext_locale_dir'); |
|
81 | |||
82 | 19 | $this->_textDomain = isset($config['gettext_text_domain']) |
|
83 | 19 | ? $config['gettext_text_domain'] : config_item('gettext_text_domain'); |
|
84 | |||
85 | 19 | $this->_catalogCodeSet = isset($config['gettext_catalog_codeset']) |
|
86 | 19 | ? $config['gettext_catalog_codeset'] : config_item('gettext_catalog_codeset'); |
|
87 | |||
88 | 19 | $this->_locale = isset($config['gettext_locale']) |
|
89 | 19 | ? $config['gettext_locale'] : config_item('gettext_locale'); |
|
90 | |||
91 | 19 | $this->_category = LC_ALL; |
|
92 | 19 | } |
|
93 | |||
94 | /** |
||
95 | * Gettext catalog codeset |
||
96 | * @return $this |
||
97 | */ |
||
98 | 19 | private function _bindTextDomainCodeSet() |
|
111 | |||
112 | /** |
||
113 | * Path to gettext locales directory relative to APPPATH |
||
114 | * @return $this |
||
115 | */ |
||
116 | 19 | View Code Duplication | private function _bindTextDomain() |
129 | |||
130 | /** |
||
131 | * Gettext domain |
||
132 | * @return $this |
||
133 | */ |
||
134 | 19 | private function _textDomain() |
|
145 | |||
146 | /** |
||
147 | * Gettext locale |
||
148 | * @return $this |
||
149 | */ |
||
150 | 19 | private function _setLocale() |
|
166 | |||
167 | /** |
||
168 | * Change environment language for CLI |
||
169 | * @return $this |
||
170 | */ |
||
171 | 19 | private function _putEnv() |
|
182 | |||
183 | /** |
||
184 | * MO file exists for locale |
||
185 | * @return $this |
||
186 | */ |
||
187 | 19 | View Code Duplication | private function _checkLocaleFile() |
198 | } |
||
199 | |||
201 | /* Location: ./libraries/gettext.php */ |
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.