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:
Complex classes like cat 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 cat, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class cat extends helper |
||
| 16 | { |
||
| 17 | /** @var \phpbb\cache\service */ |
||
| 18 | protected $cache; |
||
| 19 | |||
| 20 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 21 | protected $db; |
||
| 22 | |||
| 23 | /** @var \phpbb\controller\helper */ |
||
| 24 | protected $helper; |
||
| 25 | |||
| 26 | /** @var \phpbb\language\language */ |
||
| 27 | protected $language; |
||
| 28 | |||
| 29 | /** @var \phpbb\log\log */ |
||
| 30 | protected $phpbb_log; |
||
| 31 | |||
| 32 | /** @var \phpbb\request\request */ |
||
| 33 | protected $request; |
||
| 34 | |||
| 35 | /** @var \phpbb\template\template */ |
||
| 36 | protected $template; |
||
| 37 | |||
| 38 | /** @var \phpbb\user */ |
||
| 39 | protected $user; |
||
| 40 | |||
| 41 | /** @var \ernadoo\phpbbdirectory\core\categorie */ |
||
| 42 | protected $categorie; |
||
| 43 | |||
| 44 | /** @var \ernadoo\phpbbdirectory\core\nestedset_category */ |
||
| 45 | protected $nestedset_category; |
||
| 46 | |||
| 47 | /** @var string Custom form action */ |
||
| 48 | protected $u_action; |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $action; |
||
| 52 | |||
| 53 | /** @var array */ |
||
| 54 | private $cat_data = array(); |
||
| 55 | |||
| 56 | /** @var int */ |
||
| 57 | private $cat_id; |
||
| 58 | |||
| 59 | /** @var array */ |
||
| 60 | private $errors; |
||
| 61 | |||
| 62 | /** @var string */ |
||
| 63 | private $form_key; |
||
| 64 | |||
| 65 | /** @var int */ |
||
| 66 | private $parent_id; |
||
| 67 | |||
| 68 | /** @var bool */ |
||
| 69 | private $update; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Constructor |
||
| 73 | * |
||
| 74 | * @param \phpbb\cache\service $cache Cache object |
||
| 75 | * @param \phpbb\db\driver\driver_interface $db Database object |
||
| 76 | * @param \phpbb\controller\helper $helper Helper object |
||
| 77 | * @param \phpbb\language\language $language Language object |
||
| 78 | * @param \phpbb\log\log $log Log object |
||
| 79 | * @param \phpbb\request\request $request Request object |
||
| 80 | * @param \phpbb\template\template $template Template object |
||
| 81 | * @param \phpbb\user $user User object |
||
| 82 | * @param \ernadoo\phpbbdirectory\core\categorie $categorie PhpBB Directory extension categorie object |
||
| 83 | * @param \ernadoo\phpbbdirectory\core\nestedset_category $nestedset_category PhpBB Directory extension nestedset object |
||
| 84 | */ |
||
| 85 | public function __construct(\phpbb\cache\service $cache, \phpbb\db\driver\driver_interface $db, \phpbb\controller\helper $helper, \phpbb\language\language $language, \phpbb\log\log $log, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, \ernadoo\phpbbdirectory\core\categorie $categorie, \ernadoo\phpbbdirectory\core\nestedset_category $nestedset_category) |
||
| 86 | { |
||
| 87 | $this->cache = $cache; |
||
| 88 | $this->db = $db; |
||
| 89 | $this->helper = $helper; |
||
| 90 | $this->language = $language; |
||
| 91 | $this->phpbb_log = $log; |
||
| 92 | $this->request = $request; |
||
| 93 | $this->template = $template; |
||
| 94 | $this->user = $user; |
||
| 95 | $this->categorie = $categorie; |
||
| 96 | $this->nestedset_category = $nestedset_category; |
||
| 97 | |||
| 98 | $this->form_key = 'acp_dir_cat'; |
||
| 99 | add_form_key($this->form_key); |
||
| 100 | |||
| 101 | $this->action = $this->request->variable('action', ''); |
||
| 102 | $this->cat_id = $request->variable('c', 0); |
||
| 103 | $this->parent_id = $request->variable('parent_id', 0); |
||
| 104 | $this->update = ($this->request->is_set_post('update')) ? true : false; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Initialize defaults data for add page |
||
| 109 | * |
||
| 110 | * @return null |
||
| 111 | */ |
||
| 112 | public function action_add() |
||
| 113 | { |
||
| 114 | $this->cat_id = $this->parent_id; |
||
| 115 | $parents_list = $this->categorie->make_cat_select($this->parent_id); |
||
| 116 | |||
| 117 | // Fill categorie data with default values |
||
| 118 | if (!$this->update) |
||
| 119 | { |
||
| 120 | $this->cat_data = array( |
||
| 121 | 'parent_id' => $this->parent_id, |
||
| 122 | 'cat_name' => $this->request->variable('cat_name', '', true), |
||
| 123 | 'cat_desc' => '', |
||
| 124 | 'cat_icon' => '', |
||
| 125 | 'cat_allow_comments' => true, |
||
| 126 | 'cat_allow_votes' => true, |
||
| 127 | 'cat_must_describe' => true, |
||
| 128 | 'cat_count_all' => false, |
||
| 129 | 'cat_validate' => false, |
||
| 130 | 'enable_icons' => false, |
||
| 131 | |||
| 132 | 'display_subcat_list' => true, |
||
| 133 | |||
| 134 | 'cat_link_back' => false, |
||
| 135 | 'cat_cron_enable' => false, |
||
| 136 | 'cat_cron_freq' => 7, |
||
| 137 | 'cat_cron_nb_check' => 1, |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | $this->_display_cat_form($parents_list); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Display deleting page |
||
| 146 | * |
||
| 147 | * @return null |
||
| 148 | */ |
||
| 149 | public function action_delete() |
||
| 150 | { |
||
| 151 | View Code Duplication | if (!$this->cat_id) |
|
| 152 | { |
||
| 153 | trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING); |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->cat_data = $this->_get_cat_info($this->cat_id); |
||
| 157 | |||
| 158 | $subcats_id = array(); |
||
| 159 | $subcats = $this->nestedset_category->get_subtree_data($this->cat_id); |
||
| 160 | |||
| 161 | foreach ($subcats as $row) |
||
| 162 | { |
||
| 163 | $subcats_id[] = $row['cat_id']; |
||
| 164 | } |
||
| 165 | |||
| 166 | $cat_list = $this->categorie->make_cat_select((int) $this->cat_data['parent_id'], $subcats_id); |
||
| 167 | |||
| 168 | $sql = 'SELECT cat_id |
||
| 169 | FROM ' . $this->categories_table . ' |
||
| 170 | WHERE cat_id <> ' . (int) $this->cat_id; |
||
| 171 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 172 | |||
| 173 | if ($this->db->sql_fetchrow($result)) |
||
| 174 | { |
||
| 175 | $this->template->assign_vars(array( |
||
| 176 | 'S_MOVE_DIR_CAT_OPTIONS' => $this->categorie->make_cat_select((int) $this->cat_data['parent_id'], $subcats_id)) |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | $this->db->sql_freeresult($result); |
||
| 180 | |||
| 181 | $parent_id = ($this->parent_id == $this->cat_id) ? 0 : $this->parent_id; |
||
| 182 | |||
| 183 | $this->template->assign_vars(array( |
||
| 184 | 'S_DELETE_DIR_CAT' => true, |
||
| 185 | 'U_ACTION' => $this->u_action . "&parent_id={$parent_id}&action=delete&c=$this->cat_id", |
||
| 186 | 'U_BACK' => $this->u_action . '&parent_id=' . $this->parent_id, |
||
| 187 | |||
| 188 | 'DIR_CAT_NAME' => $this->cat_data['cat_name'], |
||
| 189 | 'S_HAS_SUBCATS' => ($this->cat_data['right_id'] - $this->cat_data['left_id'] > 1) ? true : false, |
||
| 190 | 'S_CATS_LIST' => $cat_list, |
||
| 191 | 'S_ERROR' => (sizeof($this->errors)) ? true : false, |
||
| 192 | 'ERROR_MSG' => (sizeof($this->errors)) ? implode('<br />', $this->errors) : '') |
||
| 193 | ); |
||
| 194 | |||
| 195 | return; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Initialize data for edit page |
||
| 200 | * |
||
| 201 | * @return null |
||
| 202 | */ |
||
| 203 | public function action_edit() |
||
| 204 | { |
||
| 205 | $row = $this->_get_cat_info($this->cat_id); |
||
| 206 | |||
| 207 | if (!$this->update) |
||
| 208 | { |
||
| 209 | $this->cat_data = $row; |
||
| 210 | } |
||
| 211 | else |
||
| 212 | { |
||
| 213 | $this->cat_data['left_id'] = $row['left_id']; |
||
| 214 | $this->cat_data['right_id'] = $row['right_id']; |
||
| 215 | } |
||
| 216 | |||
| 217 | // Make sure no direct child categories are able to be selected as parents. |
||
| 218 | $exclude_cats = array(); |
||
| 219 | foreach ($this->nestedset_category->get_subtree_data($this->cat_id) as $row2) |
||
| 220 | { |
||
| 221 | $exclude_cats[] = $row2['cat_id']; |
||
| 222 | } |
||
| 223 | $parents_list = $this->categorie->make_cat_select((int) $this->cat_data['parent_id'], $exclude_cats); |
||
| 224 | |||
| 225 | $this->_display_cat_form($parents_list); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Move order categories |
||
| 230 | * |
||
| 231 | * @return null |
||
| 232 | */ |
||
| 233 | public function action_move() |
||
| 234 | { |
||
| 235 | View Code Duplication | if (!$this->cat_id) |
|
| 236 | { |
||
| 237 | trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING); |
||
| 238 | } |
||
| 239 | |||
| 240 | $sql = 'SELECT cat_id, cat_name, parent_id, left_id, right_id |
||
| 241 | FROM ' . $this->categories_table . ' |
||
| 242 | WHERE cat_id = ' . (int) $this->cat_id; |
||
| 243 | $result = $this->db->sql_query($sql); |
||
| 244 | $row = $this->db->sql_fetchrow($result); |
||
| 245 | $this->db->sql_freeresult($result); |
||
| 246 | |||
| 247 | View Code Duplication | if (!$row) |
|
| 248 | { |
||
| 249 | trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING); |
||
| 250 | } |
||
| 251 | |||
| 252 | try |
||
| 253 | { |
||
| 254 | $move_cat_name = $this->nestedset_category->{$this->action}($this->cat_id); |
||
| 255 | } |
||
| 256 | catch (\Exception $e) |
||
| 257 | { |
||
| 258 | trigger_error($e->getMessage(), E_USER_WARNING); |
||
| 259 | } |
||
| 260 | |||
| 261 | if ($move_cat_name !== false) |
||
| 262 | { |
||
| 263 | $this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_DIR_CAT_' . strtoupper($this->action), time(), array($row['cat_name'], $move_cat_name)); |
||
| 264 | $this->cache->destroy('sql', $this->categories_table); |
||
| 265 | } |
||
| 266 | |||
| 267 | if ($this->request->is_ajax()) |
||
| 268 | { |
||
| 269 | $json_response = new \phpbb\json_response; |
||
| 270 | $json_response->send(array('success' => ($move_cat_name !== false))); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Display progress bar for syncinc categories |
||
| 276 | * |
||
| 277 | * @return null |
||
| 278 | */ |
||
| 279 | public function action_progress_bar() |
||
| 280 | { |
||
| 281 | $start = $this->request->variable('start', 0); |
||
| 282 | $total = $this->request->variable('total', 0); |
||
| 283 | |||
| 284 | adm_page_header($this->language->lang('SYNC_IN_PROGRESS')); |
||
| 285 | |||
| 286 | $this->template->set_filenames(array( |
||
| 287 | 'body' => 'progress_bar.html') |
||
| 288 | ); |
||
| 289 | |||
| 290 | $this->template->assign_vars(array( |
||
| 291 | 'L_PROGRESS' => $this->language->lang('SYNC_IN_PROGRESS'), |
||
| 292 | 'L_PROGRESS_EXPLAIN' => ($start && $total) ? $this->language->lang('SYNC_IN_PROGRESS_EXPLAIN', $start, $total) : $this->language->lang('SYNC_IN_PROGRESS')) |
||
| 293 | ); |
||
| 294 | |||
| 295 | adm_page_footer(); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get link's ID interval for _sync_dir_links() |
||
| 300 | * |
||
| 301 | * @return null |
||
| 302 | */ |
||
| 303 | public function action_sync() |
||
| 304 | { |
||
| 305 | View Code Duplication | if (!$this->cat_id) |
|
| 306 | { |
||
| 307 | trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING); |
||
| 308 | } |
||
| 309 | |||
| 310 | @set_time_limit(0); |
||
| 311 | |||
| 312 | $sql = 'SELECT cat_name, cat_links |
||
| 313 | FROM ' . $this->categories_table . ' |
||
| 314 | WHERE cat_id = ' . (int) $this->cat_id; |
||
| 315 | $result = $this->db->sql_query($sql); |
||
| 316 | $row = $this->db->sql_fetchrow($result); |
||
| 317 | $this->db->sql_freeresult($result); |
||
| 318 | |||
| 319 | View Code Duplication | if (!$row) |
|
| 320 | { |
||
| 321 | trigger_error($this->language->lang('DIR_NO_CAT') . adm_back_link($this->u_action . '&parent_id=' . $this->parent_id), E_USER_WARNING); |
||
| 322 | } |
||
| 323 | |||
| 324 | $sql = 'SELECT MIN(link_id) as min_link_id, MAX(link_id) as max_link_id |
||
| 325 | FROM ' . $this->links_table . ' |
||
| 326 | WHERE link_cat = ' . (int) $this->cat_id . ' |
||
| 327 | AND link_active = 1'; |
||
| 328 | $result = $this->db->sql_query($sql); |
||
| 329 | $row2 = $this->db->sql_fetchrow($result); |
||
| 330 | $this->db->sql_freeresult($result); |
||
| 331 | |||
| 332 | // Typecast to int if there is no data available |
||
| 333 | $row2['min_link_id'] = (int) $row2['min_link_id']; |
||
| 334 | $row2['max_link_id'] = (int) $row2['max_link_id']; |
||
| 335 | |||
| 336 | $start = $this->request->variable('start', $row2['min_link_id']); |
||
| 337 | |||
| 338 | $batch_size = 200; |
||
| 339 | $end = $start + $batch_size; |
||
| 340 | |||
| 341 | // Sync all links in batch mode... |
||
| 342 | $this->_sync_dir_links($start, $end); |
||
| 343 | |||
| 344 | if ($end < $row2['max_link_id']) |
||
| 345 | { |
||
| 346 | // We really need to find a way of showing statistics... no progress here |
||
| 347 | $sql = 'SELECT COUNT(link_id) as num_links |
||
| 348 | FROM ' . $this->links_table . ' |
||
| 349 | WHERE link_cat = ' . (int) $this->cat_id . ' |
||
| 350 | AND link_active = 1 |
||
| 351 | AND link_id BETWEEN ' . (int) $start . ' AND ' . (int) $end; |
||
| 352 | $result = $this->db->sql_query($sql); |
||
| 353 | $links_done = $this->request->variable('links_done', 0) + (int) $this->db->sql_fetchfield('num_links'); |
||
| 354 | $this->db->sql_freeresult($result); |
||
| 355 | |||
| 356 | $start += $batch_size; |
||
| 357 | |||
| 358 | $url = $this->u_action . "&parent_id={$this->parent_id}&c=$this->cat_id&action=sync&start=$start&links_done=$links_done&total={$row['cat_links']}"; |
||
| 359 | |||
| 360 | meta_refresh(0, $url); |
||
| 361 | |||
| 362 | $this->template->assign_vars(array( |
||
| 363 | 'UA_PROGRESS_BAR' => $this->u_action . "&action=progress_bar&start=$links_done&total={$row['cat_links']}", |
||
| 364 | 'S_CONTINUE_SYNC' => true, |
||
| 365 | 'L_PROGRESS_EXPLAIN' => $this->language->lang('SYNC_IN_PROGRESS_EXPLAIN', $links_done, $row['cat_links'])) |
||
| 366 | ); |
||
| 367 | |||
| 368 | return; |
||
| 369 | } |
||
| 370 | |||
| 371 | $url = $this->u_action . "&parent_id={$this->parent_id}&c=$this->cat_id&action=sync_cat"; |
||
| 372 | meta_refresh(0, $url); |
||
| 373 | |||
| 374 | $this->template->assign_vars(array( |
||
| 375 | 'UA_PROGRESS_BAR' => $this->u_action . '&action=progress_bar', |
||
| 376 | 'S_CONTINUE_SYNC' => true, |
||
| 377 | 'L_PROGRESS_EXPLAIN' => $this->language->lang('SYNC_IN_PROGRESS_EXPLAIN', 0, $row['cat_links'])) |
||
| 378 | ); |
||
| 379 | |||
| 380 | return; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Sync category data |
||
| 385 | * |
||
| 386 | * @return null |
||
| 387 | */ |
||
| 388 | public function action_sync_cat() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Display categories page |
||
| 412 | * |
||
| 413 | * @return null |
||
| 414 | */ |
||
| 415 | public function display_cats() |
||
| 416 | { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Set page url |
||
| 509 | * |
||
| 510 | * @param string $u_action Custom form action |
||
| 511 | * @return null |
||
| 512 | * @access public |
||
| 513 | */ |
||
| 514 | public function set_page_url($u_action) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Update cat table |
||
| 521 | * |
||
| 522 | * @return null |
||
| 523 | */ |
||
| 524 | public function update() |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Display form |
||
| 618 | * |
||
| 619 | * @param string $parents_list Drop-down list |
||
| 620 | * @return null |
||
| 621 | */ |
||
| 622 | private function _display_cat_form($parents_list) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Get category details |
||
| 692 | * |
||
| 693 | * @param int $cat_id The category ID |
||
| 694 | * @return array |
||
| 695 | */ |
||
| 696 | View Code Duplication | private function _get_cat_info($cat_id) |
|
| 712 | |||
| 713 | /** |
||
| 714 | * Update category data |
||
| 715 | * |
||
| 716 | * @return array |
||
| 717 | */ |
||
| 718 | private function _update_cat_data() |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Remove complete category |
||
| 812 | * |
||
| 813 | * @param string $action_links Action for categories links |
||
| 814 | * @param string $action_subcats Action for sub-categories |
||
| 815 | * @param int $links_to_id New category ID for links |
||
| 816 | * @param int $subcats_to_id New category ID for sub-categories |
||
| 817 | * @return array |
||
| 818 | */ |
||
| 819 | private function _delete_cat($action_links = 'delete', $action_subcats = 'delete', $links_to_id = 0, $subcats_to_id = 0) |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Move category content from one to another forum |
||
| 936 | * |
||
| 937 | * @param int $from_id |
||
| 938 | * @param int $to_id |
||
| 939 | * @return null |
||
| 940 | */ |
||
| 941 | private function _move_cat_content($from_id, $to_id) |
||
| 954 | |||
| 955 | /** |
||
| 956 | * Delete category content |
||
| 957 | * |
||
| 958 | * @return array |
||
| 959 | */ |
||
| 960 | private function _delete_cat_content() |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Update links counter |
||
| 1019 | * |
||
| 1020 | * @param int $cat_id The category ID |
||
| 1021 | * @return null |
||
| 1022 | */ |
||
| 1023 | private function _sync_dir_cat($cat_id) |
||
| 1038 | |||
| 1039 | /** |
||
| 1040 | * Update link data (note, vote, comment) |
||
| 1041 | * |
||
| 1042 | * @param int $start |
||
| 1043 | * @param int $stop |
||
| 1044 | * @return null |
||
| 1045 | */ |
||
| 1046 | private function _sync_dir_links($start, $stop) |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Display icons drop-down list |
||
| 1089 | * |
||
| 1090 | * @param string $icons_path |
||
| 1091 | * @param string $img_selected |
||
| 1092 | * @return string |
||
| 1093 | */ |
||
| 1094 | private function _get_dir_icon_list($icons_path, $img_selected) |
||
| 1124 | } |
||
| 1125 |