Total Complexity | 61 |
Total Lines | 522 |
Duplicated Lines | 0 % |
Changes | 26 | ||
Bugs | 0 | Features | 0 |
Complex classes like category 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 category, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class category |
||
23 | { |
||
24 | /** @var \phpbb\cache\driver\driver_interface */ |
||
25 | protected $cache; |
||
26 | |||
27 | /** @var \phpbb\db\driver\driver_interface */ |
||
28 | protected $db; |
||
29 | |||
30 | /** @var \phpbb\config\config */ |
||
31 | protected $config; |
||
32 | |||
33 | /* @var \phpbb\controller\helper */ |
||
34 | protected $helper; |
||
35 | |||
36 | /** @var \phpbb\user */ |
||
37 | protected $user; |
||
38 | |||
39 | /** @var \phpbb\language\language */ |
||
40 | protected $language; |
||
41 | |||
42 | /** @var \phpbb\template\template */ |
||
43 | protected $template; |
||
44 | |||
45 | /** @var \phpbb\extension\manager "Extension Manager" */ |
||
46 | protected $ext_manager; |
||
47 | |||
48 | /** @var \phpbb\log\log */ |
||
49 | protected $log; |
||
50 | |||
51 | /** |
||
52 | * The database tables |
||
53 | * |
||
54 | * @var string */ |
||
55 | protected $smilies_category_table; |
||
56 | |||
57 | /** @var string phpBB root path */ |
||
58 | protected $root_path; |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | */ |
||
63 | public function __construct(cache $cache, db $db, config $config, helper $helper, user $user, language $language, template $template, manager $ext_manager, log $log, $smilies_category_table, $root_path) |
||
64 | { |
||
65 | $this->cache = $cache; |
||
66 | $this->db = $db; |
||
67 | $this->config = $config; |
||
68 | $this->helper = $helper; |
||
69 | $this->user = $user; |
||
70 | $this->language = $language; |
||
71 | $this->template = $template; |
||
72 | $this->ext_manager = $ext_manager; |
||
73 | $this->log = $log; |
||
74 | $this->smilies_category_table = $smilies_category_table; |
||
75 | $this->root_path = $root_path; |
||
76 | } |
||
77 | |||
78 | public function get_version() |
||
94 | } |
||
95 | |||
96 | public function smilies_count($cat, $compact = false) |
||
97 | { |
||
98 | $sql = $this->db->sql_build_query('SELECT', [ |
||
99 | 'SELECT' => (!$compact) ? 'COUNT(DISTINCT smiley_id) AS smilies_count' : 'COUNT(DISTINCT smiley_url) AS smilies_count', |
||
100 | 'FROM' => [SMILIES_TABLE => ''], |
||
101 | 'WHERE' => ($cat > -1) ? 'category = ' . (int) $cat : "code <> ''", |
||
102 | ]); |
||
103 | $result = $this->db->sql_query($sql); |
||
104 | $nb = (int) $this->db->sql_fetchfield('smilies_count'); |
||
105 | $this->db->sql_freeresult($result); |
||
106 | |||
107 | return $nb; |
||
108 | } |
||
109 | |||
110 | public function select_categories($cat, $modify = false, $empty = false) |
||
111 | { |
||
112 | $lang = $this->user->lang_name; |
||
113 | $select = '<option disabled="disabled">' . $this->language->lang('SC_CATEGORY_SELECT') . '</option>'; |
||
114 | if ($modify) |
||
115 | { |
||
116 | $selected = ((int) $cat === -1) ? ' selected="selected"' : ''; |
||
117 | $select .= '<option value="-1"' . $selected . '>' . $this->language->lang('SC_CATEGORY_ANY') . '</option>'; |
||
118 | } |
||
119 | |||
120 | $sql = 'SELECT * |
||
121 | FROM ' . $this->smilies_category_table . " |
||
122 | WHERE cat_lang = '$lang' |
||
123 | ORDER BY cat_order ASC"; |
||
124 | $result = $this->db->sql_query($sql); |
||
125 | while ($row = $this->db->sql_fetchrow($result)) |
||
126 | { |
||
127 | if (!$row['cat_nb'] && $empty) |
||
128 | { |
||
129 | continue; |
||
130 | } |
||
131 | $selected = ((int) $cat === (int) $row['cat_id']) ? ' selected="selected"' : ''; |
||
132 | $select .= '<option title="' . $row['cat_name'] . '" value="' . $row['cat_id'] . '"' . $selected . '> ' . $row['cat_name'] . '</option>'; |
||
133 | } |
||
134 | $this->db->sql_freeresult($result); |
||
135 | |||
136 | $selected_default = (!$cat) ? ' selected="selected"' : ''; |
||
137 | $select .= '<option title="' . $this->language->lang('SC_CATEGORY_DEFAUT') . '" value="0"' . $selected_default . '> ' . $this->language->lang('SC_CATEGORY_DEFAUT') . '</option>'; |
||
138 | |||
139 | return $select; |
||
140 | } |
||
141 | |||
142 | public function capitalize($var) |
||
143 | { |
||
144 | return ucfirst(strtolower(trim($var))); |
||
145 | } |
||
146 | |||
147 | public function get_max_order() |
||
148 | { |
||
149 | // Get max order id... |
||
150 | $sql = 'SELECT MAX(cat_order) AS maxi |
||
151 | FROM ' . $this->smilies_category_table; |
||
152 | $result = $this->db->sql_query($sql); |
||
153 | $max = (int) $this->db->sql_fetchfield('maxi'); |
||
154 | $this->db->sql_freeresult($result); |
||
155 | |||
156 | return $max; |
||
157 | } |
||
158 | |||
159 | public function get_max_id() |
||
169 | } |
||
170 | |||
171 | public function get_first_order() |
||
182 | } |
||
183 | |||
184 | public function get_cat_nb($id) |
||
185 | { |
||
186 | $sql = 'SELECT cat_nb |
||
187 | FROM ' . $this->smilies_category_table . ' |
||
188 | WHERE cat_id = ' . (int) $id; |
||
189 | $result = $this->db->sql_query($sql); |
||
190 | $cat_nb = (int) $this->db->sql_fetchfield('cat_nb'); |
||
191 | $this->db->sql_freeresult($result); |
||
192 | |||
193 | return $cat_nb; |
||
194 | } |
||
195 | |||
196 | public function extract_list_categories($cat) |
||
197 | { |
||
198 | $title = ''; |
||
199 | $cat_order = $i = 0; |
||
200 | $lang = $this->user->lang_name; |
||
201 | $sql = $this->db->sql_build_query('SELECT', [ |
||
202 | 'SELECT' => '*', |
||
203 | 'FROM' => [$this->smilies_category_table => ''], |
||
204 | 'WHERE' => "cat_nb <> 0 AND cat_lang = '$lang'", |
||
205 | 'ORDER_BY' => 'cat_order ASC', |
||
206 | ]); |
||
207 | $result = $this->db->sql_query($sql); |
||
208 | while ($row = $this->db->sql_fetchrow($result)) |
||
209 | { |
||
210 | $actual_cat = (int) $row['cat_id'] === $cat; |
||
211 | $this->template->assign_block_vars('categories', [ |
||
212 | 'CLASS' => $actual_cat ? 'cat-active' : 'cat-inactive', |
||
213 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
214 | 'CAT_NAME' => $row['cat_name'] ? $row['cat_name'] : $row['cat_title'], |
||
215 | 'CAT_ORDER' => $row['cat_order'], |
||
216 | 'CAT_ID' => $row['cat_id'], |
||
217 | 'CAT_NB' => $row['cat_nb'], |
||
218 | 'U_CAT' => $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $row['cat_id']]), |
||
219 | ]); |
||
220 | $i++; |
||
221 | |||
222 | // Keep these values in memory |
||
223 | $title = $actual_cat ? $row['cat_name'] : $title; |
||
224 | $cat_order = $row['cat_order']; |
||
225 | } |
||
226 | $this->db->sql_freeresult($result); |
||
227 | |||
228 | // Add the Unclassified category if not empty |
||
229 | if ($nb = $this->smilies_count(0)) |
||
230 | { |
||
231 | $this->template->assign_block_vars('categories', [ |
||
232 | 'CLASS' => ($cat === 0) ? 'cat-active' : 'cat-inactive', |
||
233 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
234 | 'CAT_NAME' => $this->language->lang('SC_CATEGORY_DEFAUT'), |
||
235 | 'CAT_ORDER' => $cat_order + 1, |
||
236 | 'CAT_ID' => 0, |
||
237 | 'CAT_NB' => $nb, |
||
238 | 'U_CAT' => $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => 0]), |
||
239 | ]); |
||
240 | } |
||
241 | |||
242 | return (!$cat) ? $this->language->lang('SC_CATEGORY_DEFAUT') : $title; |
||
243 | } |
||
244 | |||
245 | public function shoutbox_smilies($event) |
||
288 | ]); |
||
289 | } |
||
290 | |||
291 | public function shoutbox_smilies_popup($event) |
||
292 | { |
||
293 | $cat = (int) $event['cat']; |
||
294 | if ($cat !== -1) |
||
295 | { |
||
296 | $i = 0; |
||
297 | $smilies = []; |
||
298 | $cat_name = $this->get_cat_name($cat); |
||
299 | |||
300 | $sql = [ |
||
301 | 'SELECT' => 'smiley_url, MIN(smiley_id) AS smiley_id, MIN(code) AS code, MIN(smiley_order) AS min_smiley_order, MIN(smiley_width) AS smiley_width, MIN(smiley_height) AS smiley_height, MIN(emotion) AS emotion', |
||
302 | 'FROM' => [SMILIES_TABLE => ''], |
||
303 | 'WHERE' => 'category = ' . $cat, |
||
304 | 'GROUP_BY' => 'smiley_url', |
||
305 | 'ORDER_BY' => 'min_smiley_order ASC', |
||
306 | ]; |
||
307 | $result = $this->db->sql_query($this->db->sql_build_query('SELECT', $sql)); |
||
308 | while ($row = $this->db->sql_fetchrow($result)) |
||
309 | { |
||
310 | $smilies[$i] = [ |
||
311 | 'nb' => (int) $i, |
||
312 | 'code' => (string) $row['code'], |
||
313 | 'emotion' => (string) $row['emotion'], |
||
314 | 'image' => (string) $row['smiley_url'], |
||
315 | 'width' => (int) $row['smiley_width'], |
||
316 | 'height' => (int) $row['smiley_height'], |
||
317 | ]; |
||
318 | $i++; |
||
319 | } |
||
320 | $this->db->sql_freeresult($result); |
||
321 | |||
322 | $event['content'] = array_merge($event['content'], [ |
||
323 | 'in_cat' => true, |
||
324 | 'cat' => $cat, |
||
325 | 'total' => $i, |
||
326 | 'smilies' => $smilies, |
||
327 | 'emptyRow' => ($i === 0) ? $this->language->lang('SC_SMILIES_EMPTY_CATEGORY') : '', |
||
328 | 'title' => $this->language->lang('SC_CATEGORY_IN', $cat_name), |
||
329 | ]); |
||
330 | } |
||
331 | } |
||
332 | |||
333 | private function get_cat_name($cat) |
||
334 | { |
||
335 | if ($cat > 0) |
||
336 | { |
||
337 | $lang = $this->user->lang_name; |
||
338 | $sql = 'SELECT cat_name |
||
339 | FROM ' . $this->smilies_category_table . " |
||
340 | WHERE cat_lang = '$lang' |
||
341 | AND cat_id = $cat"; |
||
342 | $result = $this->db->sql_query_limit($sql, 1); |
||
343 | $row = $this->db->sql_fetchrow($result); |
||
344 | $cat_name = $row['cat_name']; |
||
345 | $this->db->sql_freeresult($result); |
||
346 | } |
||
347 | else |
||
348 | { |
||
349 | $cat_name = $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
350 | } |
||
351 | |||
352 | return $cat_name; |
||
353 | } |
||
354 | |||
355 | public function set_order($action, $current_order) |
||
374 | } |
||
375 | |||
376 | public function adm_list_cat($u_action) |
||
377 | { |
||
378 | $i = $cat = 0; |
||
379 | $max = $this->get_max_order(); |
||
380 | $sql = $this->db->sql_build_query('SELECT', [ |
||
381 | 'SELECT' => 'l.lang_iso, l.lang_local_name, c.*', |
||
382 | 'FROM' => [LANG_TABLE => 'l'], |
||
383 | 'LEFT_JOIN' => [ |
||
384 | [ |
||
385 | 'FROM' => [$this->smilies_category_table => 'c'], |
||
386 | 'ON' => 'cat_lang = lang_iso', |
||
387 | ], |
||
388 | ], |
||
389 | 'ORDER_BY' => 'cat_order ASC, cat_lang_id ASC', |
||
390 | ]); |
||
391 | $result = $this->db->sql_query($sql); |
||
392 | while ($row = $this->db->sql_fetchrow($result)) |
||
393 | { |
||
394 | if (!$row) |
||
395 | { |
||
396 | $this->template->assign_vars([ |
||
397 | 'EMPTY_ROW' => true, |
||
398 | ]); |
||
399 | } |
||
400 | else |
||
401 | { |
||
402 | $this->template->assign_block_vars('categories', [ |
||
403 | 'CAT_NR' => $i + 1, |
||
404 | 'LANG_EMPTY' => !$row['cat_id'] && !$row['cat_order'] && !$row['cat_name'], |
||
405 | 'SPACER_CAT' => $this->language->lang('SC_CATEGORY_IN', $row['cat_title']), |
||
406 | 'CAT_LANG' => $row['lang_local_name'], |
||
407 | 'CAT_ISO' => $row['lang_iso'], |
||
408 | 'CAT_ID' => $row['cat_id'], |
||
409 | 'CAT_ORDER' => $row['cat_order'], |
||
410 | 'CAT_TRANSLATE' => $row['cat_name'], |
||
411 | 'CAT_NB' => $row['cat_nb'], |
||
412 | 'ROW' => (int) $row['cat_id'] !== $cat, |
||
413 | 'ROW_MAX' => (int) $row['cat_order'] === $max, |
||
414 | 'U_EDIT' => $u_action . '&action=edit&id=' . $row['cat_id'], |
||
415 | 'U_DELETE' => $u_action . '&action=delete&id=' . $row['cat_id'], |
||
416 | ]); |
||
417 | $i++; |
||
418 | // Keep this value in memory |
||
419 | $cat = (int) $row['cat_id']; |
||
420 | } |
||
421 | } |
||
422 | $this->db->sql_freeresult($result); |
||
423 | |||
424 | $this->template->assign_vars([ |
||
425 | 'IN_LIST_CAT' => true, |
||
426 | 'U_ACTION' => $u_action, |
||
427 | 'U_MOVE_CATS' => $this->helper->route('sylver35_smiliescat_ajax_list_cat'), |
||
428 | ]); |
||
429 | } |
||
430 | |||
431 | public function reset_first_cat($current_order, $switch_order_id) |
||
432 | { |
||
433 | $first = $this->get_first_order(); |
||
434 | if ($current_order === 1 || $switch_order_id === 1) |
||
435 | { |
||
436 | if ($this->get_cat_nb($first) > 0) |
||
437 | { |
||
438 | $this->config->set('smilies_category_nb', $first); |
||
439 | } |
||
440 | } |
||
441 | } |
||
442 | |||
443 | public function move_cat($id, $action) |
||
472 | } |
||
473 | |||
474 | public function edit_smiley($id, $start, $u_action) |
||
475 | { |
||
476 | $lang = $this->user->lang_name; |
||
477 | $sql = $this->db->sql_build_query('SELECT', [ |
||
505 | } |
||
506 | |||
507 | public function edit_multi_smiley($list, $start, $u_action) |
||
547 |