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\user */ |
||
| 24 | protected $user; |
||
| 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\ads\controller\admin_input */ |
||
| 39 | protected $input; |
||
| 40 | |||
| 41 | /** @var \phpbb\ads\controller\admin_helper */ |
||
| 42 | protected $helper; |
||
| 43 | |||
| 44 | /** @var string root_path */ |
||
| 45 | protected $root_path; |
||
| 46 | |||
| 47 | /** @var string php_ext */ |
||
| 48 | protected $php_ext; |
||
| 49 | |||
| 50 | /** @var string Custom form action */ |
||
| 51 | protected $u_action; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor |
||
| 55 | * |
||
| 56 | * @param \phpbb\template\template $template Template object |
||
| 57 | * @param \phpbb\user $user User object |
||
| 58 | * @param \phpbb\request\request $request Request object |
||
| 59 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
| 60 | * @param \phpbb\config\db_text $config_text Config text object |
||
| 61 | * @param \phpbb\config\config $config Config object |
||
| 62 | * @param \phpbb\ads\controller\admin_input $input Admin input object |
||
| 63 | * @param \phpbb\ads\controller\admin_helper $helper Admin helper object |
||
| 64 | * @param string $root_path phpBB root path |
||
| 65 | * @param string $php_ext PHP extension |
||
| 66 | */ |
||
| 67 | 30 | public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\ads\controller\admin_input $input, \phpbb\ads\controller\admin_helper $helper, $root_path, $php_ext) |
|
| 68 | { |
||
| 69 | 30 | $this->template = $template; |
|
| 70 | 30 | $this->user = $user; |
|
| 71 | 30 | $this->request = $request; |
|
| 72 | 30 | $this->manager = $manager; |
|
| 73 | 30 | $this->config_text = $config_text; |
|
| 74 | 30 | $this->config = $config; |
|
| 75 | 30 | $this->input = $input; |
|
| 76 | 30 | $this->helper = $helper; |
|
| 77 | 30 | $this->root_path = $root_path; |
|
| 78 | 30 | $this->php_ext = $php_ext; |
|
| 79 | 30 | } |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Process user request for manage mode |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | 6 | public function mode_manage() |
|
| 87 | { |
||
| 88 | 6 | $this->setup(); |
|
| 89 | |||
| 90 | // Trigger specific action |
||
| 91 | 6 | $action = $this->request->variable('action', ''); |
|
| 92 | 6 | if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete'))) |
|
| 93 | 6 | { |
|
| 94 | 5 | $this->{'action_' . $action}(); |
|
| 95 | 5 | } |
|
| 96 | |||
| 97 | // Otherwise default to this |
||
| 98 | 6 | $this->list_ads(); |
|
| 99 | 6 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Process user request for settings mode |
||
| 103 | * |
||
| 104 | * @return void |
||
| 105 | */ |
||
| 106 | 3 | public function mode_settings() |
|
| 107 | { |
||
| 108 | 3 | $this->setup(); |
|
| 109 | |||
| 110 | 3 | add_form_key('phpbb/ads/settings'); |
|
| 111 | 3 | if ($this->request->is_set_post('submit')) |
|
| 112 | 3 | { |
|
| 113 | // Validate form key |
||
| 114 | 2 | if (check_form_key('phpbb/ads/settings')) |
|
| 115 | 2 | { |
|
| 116 | 1 | $this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0)); |
|
| 117 | 1 | $this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0)); |
|
| 118 | 1 | $this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0)); |
|
| 119 | 1 | $this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0)))); |
|
| 120 | |||
| 121 | 1 | $this->success('ACP_AD_SETTINGS_SAVED'); |
|
| 122 | } |
||
| 123 | |||
| 124 | 1 | $this->helper->assign_errors(array($this->user->lang('FORM_INVALID'))); |
|
| 125 | 1 | } |
|
| 126 | |||
| 127 | 2 | $hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true); |
|
| 128 | 2 | $groups = $this->manager->load_groups(); |
|
| 129 | 2 | foreach ($groups as $group) |
|
| 130 | { |
||
| 131 | 2 | $group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name']; |
|
| 132 | |||
| 133 | 2 | $this->template->assign_block_vars('groups', array( |
|
| 134 | 2 | 'ID' => $group['group_id'], |
|
| 135 | 2 | 'NAME' => $group_name, |
|
| 136 | 2 | 'S_SELECTED' => in_array($group['group_id'], $hide_groups), |
|
| 137 | 2 | )); |
|
| 138 | 2 | } |
|
| 139 | |||
| 140 | 2 | $this->template->assign_vars(array( |
|
| 141 | 2 | 'U_ACTION' => $this->u_action, |
|
| 142 | 2 | 'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'], |
|
| 143 | 2 | 'ENABLE_VIEWS' => $this->config['phpbb_ads_enable_views'], |
|
| 144 | 2 | 'ENABLE_CLICKS' => $this->config['phpbb_ads_enable_clicks'], |
|
| 145 | 2 | )); |
|
| 146 | 2 | } |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Set page url |
||
| 150 | * |
||
| 151 | * @param string $u_action Custom form action |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | 24 | public function set_page_url($u_action) |
|
| 155 | { |
||
| 156 | 24 | $this->u_action = $u_action; |
|
| 157 | 24 | } |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Get ACP page title for Ads module |
||
| 161 | * |
||
| 162 | * @return string Language string for Ads ACP module |
||
| 163 | */ |
||
| 164 | 1 | public function get_page_title() |
|
| 165 | { |
||
| 166 | 1 | return $this->user->lang('ACP_PHPBB_ADS_TITLE'); |
|
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Add an advertisement |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | 11 | public function action_add() |
|
| 175 | { |
||
| 176 | 5 | $preview = $this->request->is_set_post('preview'); |
|
| 177 | 5 | $submit = $this->request->is_set_post('submit'); |
|
| 178 | 5 | $upload_banner = $this->request->is_set_post('upload_banner'); |
|
| 179 | |||
| 180 | 5 | add_form_key('phpbb/ads/add'); |
|
| 181 | 5 | if ($preview || $submit || $upload_banner) |
|
| 182 | 5 | { |
|
| 183 | 4 | $data = $this->input->get_form_data('phpbb/ads/add'); |
|
| 184 | |||
| 185 | View Code Duplication | if ($preview) |
|
| 186 | 4 | { |
|
| 187 | 1 | $this->ad_preview($data['ad_code']); |
|
| 188 | 1 | } |
|
| 189 | else if ($upload_banner) |
||
| 190 | 3 | { |
|
| 191 | 1 | $data['ad_code'] = $this->input->banner_upload($data['ad_code']); |
|
| 192 | 1 | } |
|
| 193 | 2 | else if (!$this->input->has_errors()) |
|
| 194 | 2 | { |
|
| 195 | 1 | $ad_id = $this->manager->insert_ad($data); |
|
| 196 | 11 | $this->manager->insert_ad_locations($ad_id, $data['ad_locations']); |
|
| 197 | |||
| 198 | 1 | $this->helper->log('ADD', $data['ad_name']); |
|
| 199 | |||
| 200 | 1 | $this->success('ACP_AD_ADD_SUCCESS'); |
|
| 201 | } |
||
| 202 | |||
| 203 | 3 | $this->helper->assign_locations($data['ad_locations']); |
|
| 204 | 3 | $this->helper->assign_form_data($data); |
|
| 205 | 3 | $this->helper->assign_errors($this->input->get_errors()); |
|
| 206 | 3 | } |
|
| 207 | else |
||
| 208 | { |
||
| 209 | 1 | $this->helper->assign_locations(); |
|
| 210 | } |
||
| 211 | |||
| 212 | // Set output vars for display in the template |
||
| 213 | 4 | $this->template->assign_vars(array( |
|
| 214 | 4 | 'S_ADD_AD' => true, |
|
| 215 | 4 | 'U_BACK' => $this->u_action, |
|
| 216 | 4 | 'U_ACTION' => "{$this->u_action}&action=add", |
|
| 217 | 4 | 'PICKER_DATE_FORMAT' => input::DATE_FORMAT, |
|
| 218 | 4 | 'U_FIND_USERNAME' => $this->helper->get_find_username_link(), |
|
| 219 | 4 | )); |
|
| 220 | 4 | } |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Edit an advertisement |
||
| 224 | * |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | 7 | public function action_edit() |
|
| 228 | { |
||
| 229 | 7 | $ad_id = $this->request->variable('id', 0); |
|
| 230 | 7 | $preview = $this->request->is_set_post('preview'); |
|
| 231 | 7 | $submit = $this->request->is_set_post('submit'); |
|
| 232 | 7 | $upload_banner = $this->request->is_set_post('upload_banner'); |
|
| 233 | |||
| 234 | 7 | add_form_key('phpbb/ads/edit/' . $ad_id); |
|
| 235 | 7 | if ($preview || $submit || $upload_banner) |
|
| 236 | 7 | { |
|
| 237 | 5 | $data = $this->input->get_form_data('phpbb/ads/edit/' . $ad_id); |
|
| 238 | |||
| 239 | if ($preview) |
||
| 240 | 5 | { |
|
| 241 | 1 | $this->ad_preview($data['ad_code']); |
|
| 242 | 1 | } |
|
| 243 | View Code Duplication | else if ($upload_banner) |
|
| 244 | 4 | { |
|
| 245 | $data['ad_code'] = $this->input->banner_upload($data['ad_code']); |
||
| 246 | } |
||
| 247 | 4 | else if (!$this->input->has_errors()) |
|
| 248 | 4 | { |
|
| 249 | 2 | $success = $this->manager->update_ad($ad_id, $data); |
|
| 250 | |||
| 251 | if ($success) |
||
| 252 | 2 | { |
|
| 253 | // Only insert new ad locations to DB when ad exists |
||
| 254 | 1 | $this->manager->delete_ad_locations($ad_id); |
|
| 255 | 1 | $this->manager->insert_ad_locations($ad_id, $data['ad_locations']); |
|
| 256 | |||
| 257 | 1 | $this->helper->log('EDIT', $data['ad_name']); |
|
| 258 | |||
| 259 | 1 | $this->success('ACP_AD_EDIT_SUCCESS'); |
|
| 260 | } |
||
| 261 | |||
| 262 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
| 263 | } |
||
| 264 | 3 | } |
|
| 265 | else |
||
| 266 | { |
||
| 267 | 2 | $data = $this->manager->get_ad($ad_id); |
|
| 268 | 2 | if (empty($data)) |
|
| 269 | 2 | { |
|
| 270 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
| 271 | } |
||
| 272 | |||
| 273 | // Load ad template locations |
||
| 274 | 1 | $data['ad_locations'] = $this->manager->get_ad_locations($ad_id); |
|
| 275 | } |
||
| 276 | |||
| 277 | // Set output vars for display in the template |
||
| 278 | 4 | $this->template->assign_vars(array( |
|
| 279 | 4 | 'S_EDIT_AD' => true, |
|
| 280 | 4 | 'EDIT_ID' => $ad_id, |
|
| 281 | 4 | 'U_BACK' => $this->u_action, |
|
| 282 | 4 | 'U_ACTION' => "{$this->u_action}&action=edit&id=" . $ad_id, |
|
| 283 | 4 | 'PICKER_DATE_FORMAT' => input::DATE_FORMAT, |
|
| 284 | 4 | 'U_FIND_USERNAME' => $this->helper->get_find_username_link(), |
|
| 285 | 4 | )); |
|
| 286 | 4 | $this->helper->assign_locations($data['ad_locations']); |
|
| 287 | 4 | $this->helper->assign_form_data($data); |
|
| 288 | 4 | $this->helper->assign_errors($this->input->get_errors()); |
|
| 289 | 4 | } |
|
| 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() |
|
| 404 | { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Enable/disable an advertisement |
||
| 418 | * |
||
| 419 | * @param bool $enable Enable or disable the advertisement? |
||
| 420 | * @return void |
||
| 421 | */ |
||
| 422 | 4 | protected function ad_enable($enable) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Prepare advertisement preview |
||
| 453 | * |
||
| 454 | * @param string $code Ad code to preview |
||
| 455 | * @return void |
||
| 456 | */ |
||
| 457 | 2 | protected function ad_preview($code) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Print success message. |
||
| 464 | * |
||
| 465 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 466 | */ |
||
| 467 | 6 | protected function success() |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Print error message. |
||
| 474 | * |
||
| 475 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 476 | */ |
||
| 477 | 5 | protected function error() |
|
| 481 | } |
||
| 482 |