Total Complexity | 67 |
Total Lines | 361 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like LanguageHandler 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.
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 LanguageHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class LanguageHandler extends \XoopsObjectHandler |
||
32 | { |
||
33 | public $cachedConfig; |
||
34 | |||
35 | public function loadConfig() |
||
36 | { |
||
37 | $this->cachedConfig = $this->loadFileConfig(); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param int $id |
||
42 | * @param bool $isBase |
||
43 | * |
||
44 | * @return Blanguage|Language|null |
||
45 | */ |
||
46 | public function get($id, $isBase = true) |
||
47 | { |
||
48 | $array = []; |
||
49 | $lang = null; |
||
50 | $id = (int)$id; |
||
51 | if (!$id) { |
||
52 | return $lang; |
||
53 | } |
||
54 | $prefix = $isBase ? 'xlanguage_base' : 'xlanguage_ext'; |
||
55 | if (isset($this->cachedConfig[$prefix][$id])) { |
||
56 | $array = $this->cachedConfig[$prefix][$id]; |
||
57 | } else { |
||
58 | $sql = 'SELECT * FROM ' . $this->db->prefix($prefix) . ' WHERE lang_id=' . $id; |
||
59 | /** @var \mysqli_result|false $result */ |
||
60 | $result = $this->db->query($sql); |
||
61 | if ($result) { |
||
62 | $array = $this->db->fetchArray($result); |
||
63 | } |
||
64 | } |
||
65 | if (!is_array($array) || 0 == count($array)) { |
||
66 | return $lang; |
||
67 | } |
||
68 | $lang = $this->create(false, $isBase); |
||
69 | $lang->assignVars($array); |
||
70 | if ($isBase) { |
||
71 | $lang->isBase = true; |
||
72 | } |
||
73 | |||
74 | return $lang; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param string $name |
||
79 | * @param bool $isBase |
||
80 | * |
||
81 | * @return Xlanguage\Blanguage|Xlanguage\Language|null |
||
82 | */ |
||
83 | public function getByName($name, $isBase=false) |
||
84 | { |
||
85 | $lang = null; |
||
86 | if (empty($name) || preg_match("/[^a-zA-Z0-9\_\-]/", $name)) { |
||
87 | return $lang; |
||
88 | } |
||
89 | |||
90 | if (isset($this->cachedConfig['xlanguage_base'][$name])) { |
||
91 | $array = $this->cachedConfig['xlanguage_base'][$name]; |
||
92 | $isBase = true; |
||
93 | } elseif (isset($this->cachedConfig['xlanguage_ext'][$name])) { |
||
94 | $array = $this->cachedConfig['xlanguage_ext'][$name]; |
||
95 | } elseif (!isset($this->cachedConfig)) { |
||
96 | $sql = 'SELECT * FROM ' . $this->db->prefix('xlanguage_base') . ' WHERE lang_name=\'' . $name . '\''; |
||
97 | $result = $this->db->query($sql); |
||
98 | $array = $this->db->fetchArray($result); |
||
99 | if (!is_array($array) || 0 == count($array)) { |
||
100 | $sql = 'SELECT * FROM ' . $this->db->prefix('xlanguage_ext') . ' WHERE lang_name=\'' . $name . '\''; |
||
101 | $result = $this->db->query($sql); |
||
102 | $array = $this->db->fetchArray($result); |
||
103 | if (!is_array($array) || 0 == count($array)) { |
||
104 | return $lang; |
||
105 | } |
||
106 | } else { |
||
107 | $isBase = true; |
||
108 | } |
||
109 | } |
||
110 | if (empty($array)) { |
||
111 | return $lang; |
||
112 | } |
||
113 | $lang = $this->create(false, $isBase); |
||
114 | $lang->assignVars($array); |
||
|
|||
115 | if (!isset($array['lang_base'])) { |
||
116 | $lang->isBase = true; |
||
117 | } |
||
118 | |||
119 | return $lang; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param bool $isBase |
||
124 | * |
||
125 | * @return array|false |
||
126 | */ |
||
127 | public function getAll($isBase = true) |
||
128 | { |
||
129 | $prefix = $isBase ? 'xlanguage_base' : 'xlanguage_ext'; |
||
130 | $ret = []; |
||
131 | if (isset($this->cachedConfig[$prefix])) { |
||
132 | $array = $this->cachedConfig[$prefix]; |
||
133 | foreach ($array as $lang_name => $myrow) { |
||
134 | $lang = $this->create(false, $isBase); |
||
135 | $lang->assignVars($myrow); |
||
136 | $ret[$myrow['lang_name']] = &$lang; |
||
137 | unset($lang); |
||
138 | } |
||
139 | } elseif (!isset($this->cachedConfig)) { |
||
140 | // } elseif (false === $this->cachedConfig) { |
||
141 | $sql = 'SELECT * FROM ' . $this->db->prefix($prefix); |
||
142 | /** @var \mysqli_result|false $result */ |
||
143 | $result = $this->db->query($sql); |
||
144 | if (!$result ) { |
||
145 | return false; |
||
146 | } |
||
147 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
148 | $lang = $this->create(false, $isBase); |
||
149 | $lang->assignVars($myrow); |
||
150 | $ret[$myrow['lang_name']] = &$lang; |
||
151 | unset($lang); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | return $ret; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return array |
||
160 | */ |
||
161 | public function getAllList() |
||
162 | { |
||
163 | $baseArray = $this->getAll(); |
||
164 | |||
165 | $extArray = $this->getAll(false); |
||
166 | $ret = []; |
||
167 | if ($baseArray && is_array($baseArray)) { |
||
168 | foreach ($baseArray as $base) { |
||
169 | $ret[$base->getVar('lang_name')]['base'] = $base; |
||
170 | unset($base); |
||
171 | } |
||
172 | } |
||
173 | if ($extArray && is_array($extArray)) { |
||
174 | foreach ($extArray as $ext) { |
||
175 | $ret[$ext->getVar('lang_base')]['ext'][] = $ext; |
||
176 | unset($ext); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | return $ret; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param bool $isNew |
||
185 | * @param bool $isBase |
||
186 | * |
||
187 | * @return Blanguage|Language |
||
188 | */ |
||
189 | public function create($isNew = true, $isBase = true) |
||
190 | { |
||
191 | if ($isBase) { |
||
192 | $lang = new Xlanguage\Blanguage($isBase); |
||
193 | } else { |
||
194 | $lang = new Xlanguage\Language(); |
||
195 | } |
||
196 | if ($isNew) { |
||
197 | $lang->setNew(); |
||
198 | } |
||
199 | |||
200 | return $lang; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param Blanguage|Language $lang |
||
205 | * @return bool|string|array |
||
206 | * @internal param object $lang |
||
207 | */ |
||
208 | public function insert(Blanguage $lang) |
||
209 | { |
||
210 | $val_array = []; |
||
211 | if (!$lang->isDirty()) { |
||
212 | return true; |
||
213 | } |
||
214 | if (!$lang->cleanVars()) { |
||
215 | return false; |
||
216 | } |
||
217 | $lang->prepareVars(); |
||
218 | foreach ($lang->cleanVars as $k => $v) { |
||
219 | ${$k} = $v; |
||
220 | } |
||
221 | |||
222 | if ($lang->isNew()) { |
||
223 | $var_array = [ |
||
224 | 'lang_id', |
||
225 | 'weight', |
||
226 | 'lang_name', |
||
227 | 'lang_desc', |
||
228 | 'lang_code', |
||
229 | 'lang_charset', |
||
230 | 'lang_image', |
||
231 | 'lang_base', |
||
232 | ]; |
||
233 | if ($lang->isBase) { |
||
234 | $var_array = [ |
||
235 | 'lang_id', |
||
236 | 'weight', |
||
237 | 'lang_name', |
||
238 | 'lang_desc', |
||
239 | 'lang_code', |
||
240 | 'lang_charset', |
||
241 | 'lang_image', |
||
242 | ]; |
||
243 | } |
||
244 | $lang_id = $this->db->genId($lang->table . '_lang_id_seq'); |
||
245 | foreach ($var_array as $var) { |
||
246 | $val_array[] = ${$var}; |
||
247 | } |
||
248 | $sql = 'INSERT INTO ' . $lang->table . ' (' . implode(',', $var_array) . ') VALUES (' . implode(',', $val_array) . ')'; |
||
249 | if (!$result = $this->db->queryF($sql)) { |
||
250 | xoops_error('Insert language error:' . $sql); |
||
251 | |||
252 | return false; |
||
253 | } |
||
254 | if (0 == $lang_id) { |
||
255 | $lang_id = $this->db->getInsertId(); |
||
256 | } |
||
257 | $lang->setVar('lang_id', $lang_id); |
||
258 | } else { |
||
259 | $var_array = [ |
||
260 | 'weight', |
||
261 | 'lang_name', |
||
262 | 'lang_desc', |
||
263 | 'lang_code', |
||
264 | 'lang_charset', |
||
265 | 'lang_image', |
||
266 | 'lang_base', |
||
267 | ]; |
||
268 | if ($lang->isBase) { |
||
269 | $var_array = ['weight', 'lang_name', 'lang_desc', 'lang_code', 'lang_charset', 'lang_image']; |
||
270 | } |
||
271 | $set_array = []; |
||
272 | foreach ($var_array as $var) { |
||
273 | $set_array[] = "$var = " . ${$var}; |
||
274 | } |
||
275 | $set_string = implode(',', $set_array); |
||
276 | $sql = 'UPDATE ' . $lang->table . ' SET ' . $set_string . ' WHERE lang_id = ' . $lang->getVar('lang_id'); |
||
277 | if (!$result = $this->db->queryF($sql)) { |
||
278 | xoops_error('update language error:' . $sql); |
||
279 | |||
280 | return false; |
||
281 | } |
||
282 | } |
||
283 | $this->createConfig(); |
||
284 | |||
285 | return $lang->getVar('lang_id'); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @param \XoopsObject $lang |
||
290 | * @return bool |
||
291 | * @internal param object $lang |
||
292 | */ |
||
293 | public function delete(\XoopsObject $lang)//delete(&$lang) |
||
294 | { |
||
295 | if (!is_object($lang) || !$lang->getVar('lang_id')) { |
||
296 | return true; |
||
297 | } |
||
298 | $sql = 'DELETE FROM ' . $lang->table . ' WHERE lang_id= ' . $lang->getVar('lang_id'); |
||
299 | if (!$result = $this->db->query($sql)) { |
||
300 | return false; |
||
301 | } |
||
302 | $this->createConfig(); |
||
303 | |||
304 | return true; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @return array |
||
309 | */ |
||
310 | public function getXoopsLangList() |
||
311 | { |
||
312 | return \XoopsLists::getLangList(); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @return bool |
||
317 | */ |
||
318 | public function createConfig() |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * @return bool|null |
||
374 | */ |
||
375 | public function loadFileConfig() |
||
392 | } |
||
393 | } |
||
394 |