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 admin_controller 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 admin_controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class admin_controller |
||
| 19 | { |
||
| 20 | /** @var \phpbb\template\template */ |
||
| 21 | protected $template; |
||
| 22 | |||
| 23 | /** @var \phpbb\language\language */ |
||
| 24 | protected $language; |
||
| 25 | |||
| 26 | /** @var \phpbb\request\request */ |
||
| 27 | protected $request; |
||
| 28 | |||
| 29 | /** @var \phpbb\ads\ad\manager */ |
||
| 30 | protected $manager; |
||
| 31 | |||
| 32 | /** @var \phpbb\config\db_text */ |
||
| 33 | protected $config_text; |
||
| 34 | |||
| 35 | /** @var \phpbb\config\config */ |
||
| 36 | protected $config; |
||
| 37 | |||
| 38 | /** @var \phpbb\group\helper */ |
||
| 39 | protected $group_helper; |
||
| 40 | |||
| 41 | /** @var \phpbb\ads\controller\admin_input */ |
||
| 42 | protected $input; |
||
| 43 | |||
| 44 | /** @var \phpbb\ads\controller\admin_helper */ |
||
| 45 | protected $helper; |
||
| 46 | |||
| 47 | /** @var \phpbb\ads\analyser\manager */ |
||
| 48 | protected $analyser; |
||
| 49 | |||
| 50 | /** @var string root_path */ |
||
| 51 | protected $root_path; |
||
| 52 | |||
| 53 | /** @var string php_ext */ |
||
| 54 | protected $php_ext; |
||
| 55 | |||
| 56 | /** @var string Custom form action */ |
||
| 57 | protected $u_action; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Constructor |
||
| 61 | * |
||
| 62 | * @param \phpbb\template\template $template Template object |
||
| 63 | * @param \phpbb\language\language $language Language object |
||
| 64 | * @param \phpbb\request\request $request Request object |
||
| 65 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
| 66 | * @param \phpbb\config\db_text $config_text Config text object |
||
| 67 | * @param \phpbb\config\config $config Config object |
||
| 68 | * @param \phpbb\group\helper $group_helper Group helper object |
||
| 69 | * @param \phpbb\ads\controller\admin_input $input Admin input object |
||
| 70 | * @param \phpbb\ads\controller\admin_helper $helper Admin helper object |
||
| 71 | * @param \phpbb\ads\analyser\manager $analyser Ad code analyser object |
||
| 72 | * @param string $root_path phpBB root path |
||
| 73 | * @param string $php_ext PHP extension |
||
| 74 | */ |
||
| 75 | 33 | public function __construct(\phpbb\template\template $template, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\group\helper $group_helper, \phpbb\ads\controller\admin_input $input, \phpbb\ads\controller\admin_helper $helper, \phpbb\ads\analyser\manager $analyser, $root_path, $php_ext) |
|
| 76 | { |
||
| 77 | 33 | $this->template = $template; |
|
| 78 | 33 | $this->language = $language; |
|
| 79 | 33 | $this->request = $request; |
|
| 80 | 33 | $this->manager = $manager; |
|
| 81 | 33 | $this->config_text = $config_text; |
|
| 82 | 33 | $this->config = $config; |
|
| 83 | 33 | $this->group_helper = $group_helper; |
|
| 84 | 33 | $this->input = $input; |
|
| 85 | 33 | $this->helper = $helper; |
|
| 86 | 33 | $this->analyser = $analyser; |
|
| 87 | 33 | $this->root_path = $root_path; |
|
| 88 | 33 | $this->php_ext = $php_ext; |
|
| 89 | 33 | } |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Process user request for manage mode |
||
| 93 | * |
||
| 94 | * @return void |
||
| 95 | */ |
||
| 96 | 6 | public function mode_manage() |
|
| 97 | { |
||
| 98 | 6 | $this->setup(); |
|
| 99 | |||
| 100 | // Trigger specific action |
||
| 101 | 6 | $action = $this->request->variable('action', ''); |
|
| 102 | 6 | if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete'))) |
|
| 103 | 6 | { |
|
| 104 | 5 | $this->{'action_' . $action}(); |
|
| 105 | 5 | } |
|
| 106 | |||
| 107 | // Otherwise default to this |
||
| 108 | 6 | $this->list_ads(); |
|
| 109 | 6 | } |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Process user request for settings mode |
||
| 113 | * |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | 3 | public function mode_settings() |
|
| 117 | { |
||
| 118 | 3 | $this->setup(); |
|
| 119 | |||
| 120 | 3 | add_form_key('phpbb/ads/settings'); |
|
| 121 | 3 | if ($this->request->is_set_post('submit')) |
|
| 122 | 3 | { |
|
| 123 | // Validate form key |
||
| 124 | 2 | if (check_form_key('phpbb/ads/settings')) |
|
| 125 | 2 | { |
|
| 126 | 1 | $this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0)); |
|
| 127 | 1 | $this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0)); |
|
| 128 | 1 | $this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0)); |
|
| 129 | 1 | $this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0)))); |
|
| 130 | |||
| 131 | 1 | $this->success('ACP_AD_SETTINGS_SAVED'); |
|
| 132 | } |
||
| 133 | |||
| 134 | 1 | $this->helper->assign_errors(array($this->language->lang('FORM_INVALID'))); |
|
| 135 | 1 | } |
|
| 136 | |||
| 137 | 2 | $hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true); |
|
| 138 | 2 | $groups = $this->manager->load_groups(); |
|
| 139 | 2 | View Code Duplication | foreach ($groups as $group) |
| 140 | { |
||
| 141 | 2 | $this->template->assign_block_vars('groups', array( |
|
| 142 | 2 | 'ID' => $group['group_id'], |
|
| 143 | 2 | 'NAME' => $this->group_helper->get_name($group['group_name']), |
|
| 144 | 2 | 'S_SELECTED' => in_array($group['group_id'], $hide_groups), |
|
| 145 | 2 | )); |
|
| 146 | 2 | } |
|
| 147 | |||
| 148 | 2 | $this->template->assign_vars(array( |
|
| 149 | 2 | 'U_ACTION' => $this->u_action, |
|
| 150 | 2 | 'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'], |
|
| 151 | 2 | 'ENABLE_VIEWS' => $this->config['phpbb_ads_enable_views'], |
|
| 152 | 2 | 'ENABLE_CLICKS' => $this->config['phpbb_ads_enable_clicks'], |
|
| 153 | 2 | )); |
|
| 154 | 2 | } |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Set page url |
||
| 158 | * |
||
| 159 | * @param string $u_action Custom form action |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | 27 | public function set_page_url($u_action) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get ACP page title for Ads module |
||
| 169 | * |
||
| 170 | * @return string Language string for Ads ACP module |
||
| 171 | */ |
||
| 172 | 1 | public function get_page_title() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Add an advertisement |
||
| 179 | * |
||
| 180 | * @return void |
||
| 181 | */ |
||
| 182 | 12 | public function action_add() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Edit an advertisement |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | 7 | public function action_edit() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Enable an advertisement |
||
| 311 | * |
||
| 312 | * @return void |
||
| 313 | */ |
||
| 314 | 4 | public function action_enable() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Disable an advertisement |
||
| 321 | * |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | 4 | public function action_disable() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Delete an advertisement |
||
| 331 | * |
||
| 332 | * @return void |
||
| 333 | */ |
||
| 334 | 3 | public function action_delete() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Display the ads |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | 1 | public function list_ads() |
|
| 381 | { |
||
| 382 | 1 | $ads_locations = $this->helper->prepare_ads_locations($this->manager->get_all_ad_locations()); |
|
| 383 | 1 | foreach ($this->manager->get_all_ads() as $row) |
|
| 384 | { |
||
| 385 | 1 | $ad_enabled = (int) $row['ad_enabled']; |
|
| 386 | 1 | $ad_end_date = (int) $row['ad_end_date']; |
|
| 387 | 1 | $ad_expired = $ad_end_date > 0 && $ad_end_date < time(); |
|
| 388 | 1 | if ($ad_expired && $ad_enabled) |
|
| 389 | 1 | { |
|
| 390 | 1 | $ad_enabled = 0; |
|
| 391 | 1 | $this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0)); |
|
| 392 | 1 | } |
|
| 393 | |||
| 394 | 1 | $this->template->assign_block_vars('ads', array( |
|
| 395 | 1 | 'NAME' => $row['ad_name'], |
|
| 396 | 1 | 'PRIORITY' => $row['ad_priority'], |
|
| 397 | 1 | 'END_DATE' => $this->helper->prepare_end_date($ad_end_date), |
|
| 398 | 1 | 'VIEWS' => $row['ad_views'], |
|
| 399 | 1 | 'CLICKS' => $row['ad_clicks'], |
|
| 400 | 1 | 'VIEWS_LIMIT' => $row['ad_views_limit'], |
|
| 401 | 1 | 'CLICKS_LIMIT' => $row['ad_clicks_limit'], |
|
| 402 | 1 | 'S_END_DATE_EXPIRED' => $ad_expired, |
|
| 403 | 1 | 'S_ENABLED' => $ad_enabled, |
|
| 404 | 1 | 'U_ENABLE' => $this->u_action . '&action=' . ($ad_enabled ? 'disable' : 'enable') . '&id=' . $row['ad_id'], |
|
| 405 | 1 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['ad_id'], |
|
| 406 | 1 | 'U_DELETE' => $this->u_action . '&action=delete&id=' . $row['ad_id'], |
|
| 407 | 1 | )); |
|
| 408 | |||
| 409 | 1 | if (array_key_exists($row['ad_id'], $ads_locations)) |
|
| 410 | 1 | { |
|
| 411 | 1 | foreach ($ads_locations[$row['ad_id']] as $name) |
|
| 412 | { |
||
| 413 | 1 | $this->template->assign_block_vars('ads.locations', array( |
|
| 414 | 1 | 'NAME' => $name, |
|
| 415 | 1 | )); |
|
| 416 | 1 | } |
|
| 417 | 1 | } |
|
| 418 | 1 | } |
|
| 419 | |||
| 420 | // Set output vars for display in the template |
||
| 421 | 1 | $this->template->assign_vars(array( |
|
| 422 | 1 | 'U_ACTION_ADD' => $this->u_action . '&action=add', |
|
| 423 | 1 | 'S_VIEWS_ENABLED' => $this->config['phpbb_ads_enable_views'], |
|
| 424 | 1 | 'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'], |
|
| 425 | 1 | )); |
|
| 426 | 1 | } |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Perform general tasks |
||
| 430 | * |
||
| 431 | * @return void |
||
| 432 | */ |
||
| 433 | 9 | protected function setup() |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Enable/disable an advertisement |
||
| 448 | * |
||
| 449 | * @param bool $enable Enable or disable the advertisement? |
||
| 450 | * @return void |
||
| 451 | */ |
||
| 452 | 6 | protected function ad_enable($enable) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Prepare advertisement preview |
||
| 483 | * |
||
| 484 | * @param string $code Ad code to preview |
||
| 485 | * @return void |
||
| 486 | */ |
||
| 487 | 2 | protected function ad_preview($code) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Print success message. |
||
| 494 | * |
||
| 495 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 496 | */ |
||
| 497 | 6 | protected function success() |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Print error message. |
||
| 504 | * |
||
| 505 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 506 | */ |
||
| 507 | 5 | protected function error() |
|
| 511 | } |
||
| 512 |