Total Complexity | 55 |
Total Lines | 437 |
Duplicated Lines | 0 % |
Changes | 23 | ||
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) |
||
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 | $select .= '<option value="-1"' . (($cat == -1) ? ' selected="selected"' : '') . '>' . $this->language->lang('SC_CATEGORY_ANY') . '</option>'; |
||
117 | } |
||
118 | |||
119 | $sql = 'SELECT * |
||
120 | FROM ' . $this->smilies_category_table . " |
||
121 | WHERE cat_lang = '$lang' |
||
122 | ORDER BY cat_order ASC"; |
||
123 | $result = $this->db->sql_query($sql); |
||
124 | while ($row = $this->db->sql_fetchrow($result)) |
||
125 | { |
||
126 | if (!$row['cat_nb'] && $empty) |
||
127 | { |
||
128 | continue; |
||
129 | } |
||
130 | $selected = ($cat === (int) $row['cat_id']) ? ' selected="selected"' : ''; |
||
131 | $select .= '<option title="' . $row['cat_name'] . '" value="' . $row['cat_id'] . '"' . $selected . '> ' . $row['cat_name'] . '</option>'; |
||
132 | } |
||
133 | $this->db->sql_freeresult($result); |
||
134 | |||
135 | $selected_default = (!$cat) ? ' selected="selected"' : ''; |
||
136 | $select .= '<option title="' . $this->language->lang('SC_CATEGORY_DEFAUT') . '" value="0"' . $selected_default . '> ' . $this->language->lang('SC_CATEGORY_DEFAUT') . '</option>'; |
||
137 | |||
138 | return $select; |
||
139 | } |
||
140 | |||
141 | public function capitalize($var) |
||
142 | { |
||
143 | return $this->db->sql_escape(ucfirst(strtolower(trim($var)))); |
||
144 | } |
||
145 | |||
146 | public function get_max_order() |
||
147 | { |
||
148 | // Get max order id... |
||
149 | $sql = 'SELECT MAX(cat_order) AS maxi |
||
150 | FROM ' . $this->smilies_category_table; |
||
151 | $result = $this->db->sql_query($sql); |
||
152 | $max = (int) $this->db->sql_fetchfield('maxi', $result); |
||
153 | $this->db->sql_freeresult($result); |
||
154 | |||
155 | return $max; |
||
156 | } |
||
157 | |||
158 | public function get_max_id() |
||
159 | { |
||
160 | // Get max id... |
||
161 | $sql = 'SELECT MAX(cat_id) AS id_max |
||
162 | FROM ' . $this->smilies_category_table; |
||
163 | $result = $this->db->sql_query($sql); |
||
164 | $id_max = (int) $this->db->sql_fetchfield('id_max', $result); |
||
165 | $this->db->sql_freeresult($result); |
||
166 | |||
167 | return $id_max; |
||
168 | } |
||
169 | |||
170 | public function get_first_order() |
||
171 | { |
||
172 | // Get first order id... |
||
173 | $sql = 'SELECT cat_order, cat_id |
||
174 | FROM ' . $this->smilies_category_table . ' |
||
175 | ORDER BY cat_order ASC'; |
||
176 | $result = $this->db->sql_query_limit($sql, 1); |
||
177 | $mini = (int) $this->db->sql_fetchfield('cat_id'); |
||
178 | $this->db->sql_freeresult($result); |
||
179 | |||
180 | return $mini; |
||
181 | } |
||
182 | |||
183 | public function extract_list_categories($cat) |
||
184 | { |
||
185 | $title = ''; |
||
186 | $cat_order = $i = 0; |
||
187 | $lang = $this->user->lang_name; |
||
188 | $sql = $this->db->sql_build_query('SELECT', [ |
||
189 | 'SELECT' => '*', |
||
190 | 'FROM' => [$this->smilies_category_table => ''], |
||
191 | 'WHERE' => "cat_lang = '$lang'", |
||
192 | 'ORDER_BY' => 'cat_order ASC', |
||
193 | ]); |
||
194 | $result = $this->db->sql_query($sql); |
||
195 | while ($row = $this->db->sql_fetchrow($result)) |
||
196 | { |
||
197 | // Choose only non-empty categories |
||
198 | if ($row['cat_nb']) |
||
199 | { |
||
200 | $actual_cat = (int) $row['cat_id'] === $cat; |
||
201 | $this->template->assign_block_vars('categories', [ |
||
202 | 'CLASS' => $actual_cat ? 'cat-active' : 'cat-inactive', |
||
203 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
204 | 'CAT_ID' => $row['cat_id'], |
||
205 | 'CAT_ORDER' => $row['cat_order'], |
||
206 | 'CAT_NAME' => $row['cat_name'] ? $row['cat_name'] : $row['cat_title'], |
||
207 | 'CAT_NB' => $row['cat_nb'], |
||
208 | ]); |
||
209 | $i++; |
||
210 | |||
211 | // Keep these values in memory |
||
212 | $title = $actual_cat ? $row['cat_name'] : $title; |
||
213 | $cat_order = $row['cat_order']; |
||
214 | } |
||
215 | } |
||
216 | $this->db->sql_freeresult($result); |
||
217 | |||
218 | // Add the Unclassified category if not empty |
||
219 | $nb = $this->smilies_count(0); |
||
220 | if ($nb) |
||
221 | { |
||
222 | $this->template->assign_block_vars('categories', [ |
||
223 | 'CLASS' => ($cat === 0) ? 'cat-active' : 'cat-inactive', |
||
224 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
225 | 'CAT_ID' => 0, |
||
226 | 'CAT_ORDER' => $cat_order + 1, |
||
227 | 'CAT_NAME' => $this->language->lang('SC_CATEGORY_DEFAUT'), |
||
228 | 'CAT_NB' => $nb, |
||
229 | ]); |
||
230 | } |
||
231 | |||
232 | return (!$cat) ? $this->language->lang('SC_CATEGORY_DEFAUT') : $title; |
||
233 | } |
||
234 | |||
235 | public function shoutbox_smilies($event) |
||
279 | ]); |
||
280 | } |
||
281 | |||
282 | public function shoutbox_smilies_popup($event) |
||
283 | { |
||
284 | $cat = (int) $event['cat']; |
||
285 | if ($cat > -1) |
||
286 | { |
||
287 | $i = 0; |
||
288 | $smilies = []; |
||
289 | $lang = $this->user->lang_name; |
||
290 | $cat_name = $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
291 | |||
292 | if ($cat > 0) |
||
293 | { |
||
294 | $sql = 'SELECT cat_name |
||
295 | FROM ' . $this->smilies_category_table . " |
||
296 | WHERE cat_lang = '$lang' |
||
297 | AND cat_id = $cat"; |
||
298 | $result = $this->db->sql_query_limit($sql, 1); |
||
299 | $row = $this->db->sql_fetchrow($result); |
||
300 | $cat_name = $row['cat_name']; |
||
301 | $this->db->sql_freeresult($result); |
||
302 | } |
||
303 | |||
304 | $sql = $this->db->sql_build_query('SELECT', [ |
||
305 | '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', |
||
306 | 'FROM' => [SMILIES_TABLE => ''], |
||
307 | 'WHERE' => 'category = ' . $cat, |
||
308 | 'GROUP_BY' => 'smiley_url', |
||
309 | 'ORDER_BY' => 'min_smiley_order ASC', |
||
310 | ]); |
||
311 | $result = $this->db->sql_query($sql); |
||
312 | while ($row = $this->db->sql_fetchrow($result)) |
||
313 | { |
||
314 | $smilies[$i] = [ |
||
315 | 'nb' => (int) $i, |
||
316 | 'code' => (string) $row['code'], |
||
317 | 'emotion' => (string) $row['emotion'], |
||
318 | 'image' => (string) $row['smiley_url'], |
||
319 | 'width' => (int) $row['smiley_width'], |
||
320 | 'height' => (int) $row['smiley_height'], |
||
321 | ]; |
||
322 | $i++; |
||
323 | } |
||
324 | |||
325 | $event['content'] = array_merge($event['content'], [ |
||
326 | 'in_cat' => true, |
||
327 | 'cat' => $cat, |
||
328 | 'total' => $i, |
||
329 | 'smilies' => $smilies, |
||
330 | 'emptyRow' => (!$i) ? $this->language->lang('SC_SMILIES_EMPTY_CATEGORY') : '', |
||
331 | 'title' => $this->language->lang('SC_CATEGORY_IN', $cat_name), |
||
332 | ]); |
||
333 | } |
||
334 | } |
||
335 | |||
336 | public function set_order($action, $current_order) |
||
355 | } |
||
356 | |||
357 | public function adm_list_cat($u_action) |
||
358 | { |
||
359 | $i = $cat = 0; |
||
360 | $max = $this->get_max_order(); |
||
361 | $sql = $this->db->sql_build_query('SELECT', [ |
||
362 | 'SELECT' => 'l.lang_iso, l.lang_local_name, c.*', |
||
363 | 'FROM' => [LANG_TABLE => 'l'], |
||
364 | 'LEFT_JOIN' => [ |
||
365 | [ |
||
366 | 'FROM' => [$this->smilies_category_table => 'c'], |
||
367 | 'ON' => 'cat_lang = lang_iso', |
||
368 | ], |
||
369 | ], |
||
370 | 'ORDER_BY' => 'cat_order ASC, cat_lang_id ASC', |
||
371 | ]); |
||
372 | $result = $this->db->sql_query($sql); |
||
373 | while ($row = $this->db->sql_fetchrow($result)) |
||
374 | { |
||
375 | if (!$row) |
||
376 | { |
||
377 | $this->template->assign_vars([ |
||
378 | 'EMPTY_ROW' => true, |
||
379 | ]); |
||
380 | } |
||
381 | else |
||
382 | { |
||
383 | $this->template->assign_block_vars('categories', [ |
||
384 | 'CAT_NR' => $i + 1, |
||
385 | 'LANG_EMPTY' => !$row['cat_id'] && !$row['cat_order'] && !$row['cat_name'], |
||
386 | 'SPACER_CAT' => $this->language->lang('SC_CATEGORY_IN', $row['cat_title']), |
||
387 | 'CAT_LANG' => $row['lang_local_name'], |
||
388 | 'CAT_ISO' => $row['lang_iso'], |
||
389 | 'CAT_ID' => $row['cat_id'], |
||
390 | 'CAT_ORDER' => $row['cat_order'], |
||
391 | 'CAT_TRANSLATE' => $row['cat_name'], |
||
392 | 'CAT_NB' => $row['cat_nb'], |
||
393 | 'ROW' => (int) $row['cat_id'] !== $cat, |
||
394 | 'ROW_MAX' => (int) $row['cat_order'] === $max, |
||
395 | 'U_EDIT' => $u_action . '&action=edit&id=' . $row['cat_id'], |
||
396 | 'U_DELETE' => $u_action . '&action=delete&id=' . $row['cat_id'], |
||
397 | ]); |
||
398 | $i++; |
||
399 | // Keep this value in memory |
||
400 | $cat = (int) $row['cat_id']; |
||
401 | } |
||
402 | } |
||
403 | $this->db->sql_freeresult($result); |
||
404 | |||
405 | $this->template->assign_vars([ |
||
406 | 'IN_LIST_CAT' => true, |
||
407 | 'U_ACTION' => $u_action, |
||
408 | 'U_MOVE_CATS' => $this->helper->route('sylver35_smiliescat_ajax_list_cat'), |
||
409 | ]); |
||
410 | } |
||
411 | |||
412 | public function reset_first_cat($current_order, $switch_order_id) |
||
413 | { |
||
414 | $first = $this->get_first_order(); |
||
415 | if ($current_order === 1 || $switch_order_id === 1) |
||
416 | { |
||
417 | $this->config->set('smilies_category_nb', $first); |
||
418 | } |
||
419 | } |
||
420 | |||
421 | public function move_cat($action, $id) |
||
459 | } |
||
460 | } |
||
461 |