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 string root_path */ |
||
| 48 | protected $root_path; |
||
| 49 | |||
| 50 | /** @var string php_ext */ |
||
| 51 | protected $php_ext; |
||
| 52 | |||
| 53 | /** @var string Custom form action */ |
||
| 54 | protected $u_action; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor |
||
| 58 | * |
||
| 59 | * @param \phpbb\template\template $template Template object |
||
| 60 | * @param \phpbb\language\language $language Language object |
||
| 61 | * @param \phpbb\request\request $request Request object |
||
| 62 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
| 63 | * @param \phpbb\config\db_text $config_text Config text object |
||
| 64 | * @param \phpbb\config\config $config Config object |
||
| 65 | * @param \phpbb\group\helper $group_helper Group helper object |
||
| 66 | * @param \phpbb\ads\controller\admin_input $input Admin input object |
||
| 67 | * @param \phpbb\ads\controller\admin_helper $helper Admin helper object |
||
| 68 | * @param string $root_path phpBB root path |
||
| 69 | * @param string $php_ext PHP extension |
||
| 70 | */ |
||
| 71 | 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, $root_path, $php_ext) |
||
| 72 | { |
||
| 73 | $this->template = $template; |
||
| 74 | $this->language = $language; |
||
| 75 | $this->request = $request; |
||
| 76 | $this->manager = $manager; |
||
| 77 | $this->config_text = $config_text; |
||
| 78 | $this->config = $config; |
||
| 79 | $this->group_helper = $group_helper; |
||
| 80 | $this->input = $input; |
||
| 81 | $this->helper = $helper; |
||
| 82 | $this->root_path = $root_path; |
||
| 83 | $this->php_ext = $php_ext; |
||
| 84 | 48 | } |
|
| 85 | |||
| 86 | 48 | /** |
|
| 87 | 48 | * Process user request for manage mode |
|
| 88 | 48 | * |
|
| 89 | 48 | * @return void |
|
| 90 | 48 | */ |
|
| 91 | 48 | public function mode_manage() |
|
| 105 | |||
| 106 | 6 | /** |
|
| 107 | * Process user request for settings mode |
||
| 108 | 6 | * |
|
| 109 | * @return void |
||
| 110 | 6 | */ |
|
| 111 | 6 | public function mode_settings() |
|
| 112 | { |
||
| 113 | $this->setup(); |
||
| 114 | |||
| 115 | add_form_key('phpbb/ads/settings'); |
||
| 116 | 6 | if ($this->request->is_set_post('submit')) |
|
| 117 | 6 | { |
|
| 118 | 6 | // Validate form key |
|
| 119 | 5 | if (check_form_key('phpbb/ads/settings')) |
|
| 120 | 5 | { |
|
| 121 | $this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0)); |
||
| 122 | $this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0)); |
||
| 123 | 6 | $this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0)); |
|
| 124 | 6 | $this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0)))); |
|
| 125 | |||
| 126 | $this->success('ACP_AD_SETTINGS_SAVED'); |
||
| 127 | } |
||
| 128 | |||
| 129 | $this->helper->assign_errors(array($this->language->lang('FORM_INVALID'))); |
||
| 130 | } |
||
| 131 | 3 | ||
| 132 | $hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true); |
||
| 133 | 3 | $groups = $this->manager->load_groups(); |
|
| 134 | View Code Duplication | foreach ($groups as $group) |
|
| 135 | 3 | { |
|
| 136 | 3 | $this->template->assign_block_vars('groups', array( |
|
| 137 | 3 | 'ID' => $group['group_id'], |
|
| 138 | 'NAME' => $this->group_helper->get_name($group['group_name']), |
||
| 139 | 2 | 'S_SELECTED' => in_array($group['group_id'], $hide_groups), |
|
| 140 | 2 | )); |
|
| 141 | 1 | } |
|
| 142 | 1 | ||
| 143 | $this->template->assign_vars(array( |
||
| 144 | 2 | 'U_ACTION' => $this->u_action, |
|
| 145 | 2 | 'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'], |
|
| 146 | 1 | 'ENABLE_VIEWS' => $this->config['phpbb_ads_enable_views'], |
|
| 147 | 1 | 'ENABLE_CLICKS' => $this->config['phpbb_ads_enable_clicks'], |
|
| 148 | 1 | )); |
|
| 149 | 1 | } |
|
| 150 | |||
| 151 | 1 | /** |
|
| 152 | * Set page url |
||
| 153 | * |
||
| 154 | 1 | * @param string $u_action Custom form action |
|
| 155 | 1 | * @return void |
|
| 156 | 1 | */ |
|
| 157 | 1 | public function set_page_url($u_action) |
|
| 161 | 2 | ||
| 162 | 2 | /** |
|
| 163 | * Get ACP page title for Ads module |
||
| 164 | 2 | * |
|
| 165 | * @return string Language string for Ads ACP module |
||
| 166 | 2 | */ |
|
| 167 | 2 | public function get_page_title() |
|
| 168 | 2 | { |
|
| 169 | 2 | return $this->language->lang('ACP_PHPBB_ADS_TITLE'); |
|
| 170 | 2 | } |
|
| 171 | 2 | ||
| 172 | /** |
||
| 173 | 2 | * Add an advertisement |
|
| 174 | 2 | * |
|
| 175 | 2 | * @return void |
|
| 176 | 2 | */ |
|
| 177 | 2 | public function action_add() |
|
| 178 | 2 | { |
|
| 179 | 2 | $preview = $this->request->is_set_post('preview'); |
|
| 180 | $submit = $this->request->is_set_post('submit'); |
||
| 181 | $upload_banner = $this->request->is_set_post('upload_banner'); |
||
| 182 | |||
| 183 | add_form_key('phpbb/ads/add'); |
||
| 184 | if ($preview || $submit || $upload_banner) |
||
| 185 | { |
||
| 186 | $data = $this->input->get_form_data('phpbb/ads/add'); |
||
| 187 | 42 | ||
| 188 | View Code Duplication | if ($preview) |
|
| 189 | 42 | { |
|
| 190 | 42 | $this->ad_preview($data['ad_code']); |
|
| 191 | } |
||
| 192 | else if ($upload_banner) |
||
| 193 | { |
||
| 194 | $data['ad_code'] = $this->input->banner_upload($data['ad_code']); |
||
| 195 | } |
||
| 196 | else if (!$this->input->has_errors()) |
||
| 197 | 1 | { |
|
| 198 | $ad_id = $this->manager->insert_ad($data); |
||
| 199 | 1 | $this->manager->insert_ad_locations($ad_id, $data['ad_locations']); |
|
| 200 | |||
| 201 | $this->helper->log('ADD', $data['ad_name']); |
||
| 202 | |||
| 203 | $this->success('ACP_AD_ADD_SUCCESS'); |
||
| 204 | } |
||
| 205 | |||
| 206 | $this->helper->assign_locations($data['ad_locations']); |
||
| 207 | 15 | $this->helper->assign_form_data($data); |
|
| 208 | $this->helper->assign_errors($this->input->get_errors()); |
||
| 209 | 15 | } |
|
| 210 | 15 | else |
|
| 211 | 15 | { |
|
| 212 | $this->helper->assign_locations(); |
||
| 213 | 15 | } |
|
| 214 | 15 | ||
| 215 | 15 | // Set output vars for display in the template |
|
| 216 | 14 | $this->template->assign_vars(array( |
|
| 217 | 'S_ADD_AD' => true, |
||
| 218 | 'U_BACK' => $this->u_action, |
||
| 219 | 14 | 'U_ACTION' => "{$this->u_action}&action=add", |
|
| 220 | 1 | 'PICKER_DATE_FORMAT' => input::DATE_FORMAT, |
|
| 221 | 1 | 'U_FIND_USERNAME' => $this->helper->get_find_username_link(), |
|
| 222 | )); |
||
| 223 | 13 | } |
|
| 224 | 2 | ||
| 225 | 2 | /** |
|
| 226 | * Edit an advertisement |
||
| 227 | 11 | * |
|
| 228 | 1 | * @return void |
|
| 229 | 1 | */ |
|
| 230 | public function action_edit() |
||
| 231 | 1 | { |
|
| 232 | $ad_id = $this->request->variable('id', 0); |
||
| 233 | 1 | $preview = $this->request->is_set_post('preview'); |
|
| 234 | $submit = $this->request->is_set_post('submit'); |
||
| 235 | $upload_banner = $this->request->is_set_post('upload_banner'); |
||
| 236 | 13 | ||
| 237 | 13 | add_form_key('phpbb/ads/edit/' . $ad_id); |
|
| 238 | 13 | if ($preview || $submit || $upload_banner) |
|
| 239 | { |
||
| 240 | $data = $this->input->get_form_data('phpbb/ads/edit/' . $ad_id); |
||
| 241 | 1 | ||
| 242 | if ($preview) |
||
| 243 | { |
||
| 244 | $this->ad_preview($data['ad_code']); |
||
| 245 | 14 | } |
|
| 246 | 14 | View Code Duplication | else if ($upload_banner) |
| 247 | 14 | { |
|
| 248 | 14 | $data['ad_code'] = $this->input->banner_upload($data['ad_code']); |
|
| 249 | 14 | } |
|
| 250 | 14 | else if (!$this->input->has_errors()) |
|
| 251 | 14 | { |
|
| 252 | 14 | $success = $this->manager->update_ad($ad_id, $data); |
|
| 253 | |||
| 254 | if ($success) |
||
| 255 | { |
||
| 256 | // Only insert new ad locations to DB when ad exists |
||
| 257 | $this->manager->delete_ad_locations($ad_id); |
||
| 258 | $this->manager->insert_ad_locations($ad_id, $data['ad_locations']); |
||
| 259 | 15 | ||
| 260 | $this->helper->log('EDIT', $data['ad_name']); |
||
| 261 | 15 | ||
| 262 | 15 | $this->success('ACP_AD_EDIT_SUCCESS'); |
|
| 263 | 15 | } |
|
| 264 | 15 | ||
| 265 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
||
| 266 | 15 | } |
|
| 267 | 15 | } |
|
| 268 | 15 | else |
|
| 269 | 13 | { |
|
| 270 | $data = $this->manager->get_ad($ad_id); |
||
| 271 | if (empty($data)) |
||
| 272 | 13 | { |
|
| 273 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
| 274 | 1 | } |
|
| 275 | |||
| 276 | 12 | // Load ad template locations |
|
| 277 | $data['ad_locations'] = $this->manager->get_ad_locations($ad_id); |
||
| 278 | } |
||
| 279 | |||
| 280 | 12 | // Set output vars for display in the template |
|
| 281 | 2 | $this->template->assign_vars(array( |
|
| 282 | 'S_EDIT_AD' => true, |
||
| 283 | 'EDIT_ID' => $ad_id, |
||
| 284 | 2 | 'U_BACK' => $this->u_action, |
|
| 285 | 'U_ACTION' => "{$this->u_action}&action=edit&id=" . $ad_id, |
||
| 286 | 1 | 'PICKER_DATE_FORMAT' => input::DATE_FORMAT, |
|
| 287 | 1 | 'U_FIND_USERNAME' => $this->helper->get_find_username_link(), |
|
| 288 | )); |
||
| 289 | 1 | $this->helper->assign_locations($data['ad_locations']); |
|
| 290 | $this->helper->assign_form_data($data); |
||
| 291 | 1 | $this->helper->assign_errors($this->input->get_errors()); |
|
| 292 | } |
||
| 293 | |||
| 294 | 1 | /** |
|
| 295 | * Enable an advertisement |
||
| 296 | 11 | * |
|
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | 2 | public function action_enable() |
|
| 300 | 2 | { |
|
| 301 | 2 | $this->ad_enable(true); |
|
| 302 | 1 | } |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Disable an advertisement |
||
| 306 | 1 | * |
|
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | public function action_disable() |
||
| 313 | 12 | ||
| 314 | 12 | /** |
|
| 315 | 12 | * Delete an advertisement |
|
| 316 | 12 | * |
|
| 317 | 12 | * @return void |
|
| 318 | 12 | */ |
|
| 319 | 12 | public function action_delete() |
|
| 359 | 2 | ||
| 360 | /** |
||
| 361 | * Display the ads |
||
| 362 | 2 | * |
|
| 363 | 2 | * @return void |
|
| 364 | 1 | */ |
|
| 365 | public function list_ads() |
||
| 400 | 1 | ||
| 401 | 1 | /** |
|
| 402 | 1 | * Perform general tasks |
|
| 403 | 1 | * |
|
| 404 | 1 | * @return void |
|
| 405 | */ |
||
| 406 | 1 | protected function setup() |
|
| 418 | 1 | ||
| 419 | 1 | /** |
|
| 420 | * Enable/disable an advertisement |
||
| 421 | * |
||
| 422 | 1 | * @param bool $enable Enable or disable the advertisement? |
|
| 423 | 1 | * @return void |
|
| 424 | 1 | */ |
|
| 425 | 1 | protected function ad_enable($enable) |
|
| 453 | 4 | ||
| 454 | 4 | /** |
|
| 455 | * Prepare advertisement preview |
||
| 456 | * |
||
| 457 | 4 | * @param string $code Ad code to preview |
|
| 458 | 4 | * @return void |
|
| 459 | */ |
||
| 460 | protected function ad_preview($code) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Print success message. |
||
| 467 | * |
||
| 468 | 4 | * It takes arguments in the form of a language key, followed by language substitution values. |
|
| 469 | 2 | */ |
|
| 470 | protected function success() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Print error message. |
||
| 477 | * |
||
| 478 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 479 | */ |
||
| 480 | protected function error() |
||
| 484 | } |
||
| 485 |