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