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 |
||
| 16 | class admin_controller |
||
| 17 | { |
||
| 18 | const MAX_NAME_LENGTH = 255; |
||
| 19 | const DATE_FORMAT = 'Y-m-d'; |
||
| 20 | const DEFAULT_PRIORITY = 5; |
||
| 21 | |||
| 22 | /** @var \phpbb\template\template */ |
||
| 23 | protected $template; |
||
| 24 | |||
| 25 | /** @var \phpbb\user */ |
||
| 26 | protected $user; |
||
| 27 | |||
| 28 | /** @var \phpbb\request\request */ |
||
| 29 | protected $request; |
||
| 30 | |||
| 31 | /** @var \phpbb\ads\ad\manager */ |
||
| 32 | protected $manager; |
||
| 33 | |||
| 34 | /** @var \phpbb\ads\location\manager */ |
||
| 35 | protected $location_manager; |
||
| 36 | |||
| 37 | /** @var \phpbb\log\log */ |
||
| 38 | protected $log; |
||
| 39 | |||
| 40 | /** @var \phpbb\config\db_text */ |
||
| 41 | protected $config_text; |
||
| 42 | |||
| 43 | /** @var \phpbb\config\config */ |
||
| 44 | protected $config; |
||
| 45 | |||
| 46 | /** @var string php_ext */ |
||
| 47 | protected $php_ext; |
||
| 48 | |||
| 49 | /** @var string ext_path */ |
||
| 50 | protected $ext_path; |
||
| 51 | |||
| 52 | /** @var string Custom form action */ |
||
| 53 | protected $u_action; |
||
| 54 | |||
| 55 | /** @var array Form validation errors */ |
||
| 56 | protected $errors = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Constructor |
||
| 60 | * |
||
| 61 | * @param \phpbb\template\template $template Template object |
||
| 62 | * @param \phpbb\user $user User object |
||
| 63 | * @param \phpbb\request\request $request Request object |
||
| 64 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
| 65 | * @param \phpbb\ads\location\manager $location_manager Template location manager object |
||
| 66 | * @param \phpbb\log\log $log The phpBB log system |
||
| 67 | * @param \phpbb\config\db_text $config_text Config text object |
||
| 68 | * @param \phpbb\config\config $config Config object |
||
| 69 | * @param string $php_ext PHP extension |
||
| 70 | * @param string $ext_path Path to this extension |
||
| 71 | */ |
||
| 72 | 44 | public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\log\log $log, \phpbb\config\db_text $config_text, \phpbb\config\config $config, $php_ext, $ext_path) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Process user request for manage mode |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | 6 | public function mode_manage() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Process user request for settings mode |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | 3 | public function mode_settings() |
|
| 112 | { |
||
| 113 | 3 | $this->setup(); |
|
| 114 | |||
| 115 | 3 | add_form_key('phpbb/ads/settings'); |
|
| 116 | 3 | if ($this->request->is_set_post('submit')) |
|
| 117 | 3 | { |
|
| 118 | // Validate form key |
||
| 119 | 2 | if (!check_form_key('phpbb/ads/settings')) |
|
| 120 | 2 | { |
|
| 121 | 1 | $this->errors[] = $this->user->lang('FORM_INVALID'); |
|
| 122 | 1 | } |
|
| 123 | |||
| 124 | 2 | if (empty($this->errors)) |
|
| 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->template->assign_vars(array( |
|
| 135 | 1 | 'S_ERROR' => (bool) count($this->errors), |
|
| 136 | 1 | 'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '', |
|
| 137 | 1 | )); |
|
| 138 | 1 | } |
|
| 139 | |||
| 140 | 2 | $hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true); |
|
| 141 | 2 | $groups = $this->manager->load_groups(); |
|
| 142 | 2 | foreach ($groups as $group) |
|
| 143 | { |
||
| 144 | 2 | $group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name']; |
|
| 145 | |||
| 146 | 2 | $this->template->assign_block_vars('groups', array( |
|
| 147 | 2 | 'ID' => $group['group_id'], |
|
| 148 | 2 | 'NAME' => $group_name, |
|
| 149 | 2 | 'S_SELECTED' => in_array($group['group_id'], $hide_groups), |
|
| 150 | 2 | )); |
|
| 151 | 2 | } |
|
| 152 | |||
| 153 | 2 | $this->template->assign_vars(array( |
|
| 154 | 2 | 'U_ACTION' => $this->u_action, |
|
| 155 | 2 | 'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'], |
|
| 156 | 2 | 'ENABLE_VIEWS' => $this->config['phpbb_ads_enable_views'], |
|
| 157 | 2 | 'ENABLE_CLICKS' => $this->config['phpbb_ads_enable_clicks'], |
|
| 158 | 2 | )); |
|
| 159 | 2 | } |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Set page url |
||
| 163 | * |
||
| 164 | * @param string $u_action Custom form action |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | 38 | public function set_page_url($u_action) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Get ACP page title for Ads module |
||
| 174 | * |
||
| 175 | * @return string Language string for Ads ACP module |
||
| 176 | */ |
||
| 177 | 1 | public function get_page_title() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Add an advertisement |
||
| 184 | * |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | 12 | public function action_add() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Edit an advertisement |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | 14 | public function action_edit() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Enable an advertisement |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | 3 | public function action_enable() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Disable an advertisement |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | 3 | public function action_disable() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Delete an advertisement |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | 3 | public function action_delete() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Display the ads |
||
| 359 | * |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | 1 | public function list_ads() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Perform general tasks |
||
| 400 | * |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | 9 | protected function setup() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Enable/disable an advertisement |
||
| 412 | * |
||
| 413 | * @param bool $enable Enable or disable the advertisement? |
||
| 414 | * @return void |
||
| 415 | */ |
||
| 416 | 4 | protected function ad_enable($enable) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Get admin form data. |
||
| 447 | * |
||
| 448 | * @param string $form_name The form name. |
||
| 449 | * @return array Form data |
||
| 450 | */ |
||
| 451 | 23 | protected function get_form_data($form_name) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Assign form data to the template. |
||
| 523 | * |
||
| 524 | * @param array $data The form data. |
||
| 525 | * @return void |
||
| 526 | */ |
||
| 527 | 21 | protected function assign_form_data($data) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Prepare end date for display |
||
| 546 | * |
||
| 547 | * @param mixed $end_date End date. |
||
| 548 | * @return string End date prepared for display. |
||
| 549 | */ |
||
| 550 | 21 | protected function prepare_end_date($end_date) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Assign template locations data to the template. |
||
| 567 | * |
||
| 568 | * @param mixed $data The form data or nothing. |
||
| 569 | * @return void |
||
| 570 | */ |
||
| 571 | 22 | protected function assign_locations($data = false) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Prepare advertisement preview |
||
| 586 | * |
||
| 587 | * @param string $code Ad code to preview |
||
| 588 | * @return void |
||
| 589 | */ |
||
| 590 | 2 | protected function ad_preview($code) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Print success message. |
||
| 597 | * |
||
| 598 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 599 | */ |
||
| 600 | 6 | protected function success() |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Print error message. |
||
| 607 | * |
||
| 608 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 609 | */ |
||
| 610 | 5 | protected function error() |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Log action |
||
| 617 | * |
||
| 618 | * @param string $action Performed action in uppercase |
||
| 619 | * @param string $ad_name Advertisement name |
||
| 620 | * @return void |
||
| 621 | */ |
||
| 622 | 3 | protected function log($action, $ad_name) |
|
| 626 | } |
||
| 627 |