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 | private $data = array(); |
||
| 21 | |||
| 22 | /** @var \phpbb\template\template */ |
||
| 23 | protected $template; |
||
| 24 | |||
| 25 | /** @var \phpbb\language\language */ |
||
| 26 | protected $language; |
||
| 27 | |||
| 28 | /** @var \phpbb\request\request */ |
||
| 29 | protected $request; |
||
| 30 | |||
| 31 | /** @var \phpbb\ads\ad\manager */ |
||
| 32 | protected $manager; |
||
| 33 | |||
| 34 | /** @var \phpbb\config\db_text */ |
||
| 35 | protected $config_text; |
||
| 36 | |||
| 37 | /** @var \phpbb\config\config */ |
||
| 38 | protected $config; |
||
| 39 | |||
| 40 | /** @var \phpbb\group\helper */ |
||
| 41 | protected $group_helper; |
||
| 42 | |||
| 43 | /** @var \phpbb\ads\controller\admin_input */ |
||
| 44 | protected $input; |
||
| 45 | |||
| 46 | /** @var \phpbb\ads\controller\admin_helper */ |
||
| 47 | protected $helper; |
||
| 48 | |||
| 49 | /** @var \phpbb\ads\analyser\manager */ |
||
| 50 | protected $analyser; |
||
| 51 | |||
| 52 | /** @var string root_path */ |
||
| 53 | protected $root_path; |
||
| 54 | |||
| 55 | /** @var string php_ext */ |
||
| 56 | protected $php_ext; |
||
| 57 | |||
| 58 | /** @var string Custom form action */ |
||
| 59 | protected $u_action; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param \phpbb\template\template $template Template object |
||
| 65 | * @param \phpbb\language\language $language Language object |
||
| 66 | * @param \phpbb\request\request $request Request object |
||
| 67 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
| 68 | * @param \phpbb\config\db_text $config_text Config text object |
||
| 69 | * @param \phpbb\config\config $config Config object |
||
| 70 | * @param \phpbb\group\helper $group_helper Group helper object |
||
| 71 | * @param \phpbb\ads\controller\admin_input $input Admin input object |
||
| 72 | * @param \phpbb\ads\controller\admin_helper $helper Admin helper object |
||
| 73 | * @param \phpbb\ads\analyser\manager $analyser Ad code analyser object |
||
| 74 | * @param string $root_path phpBB root path |
||
| 75 | * @param string $php_ext PHP extension |
||
| 76 | */ |
||
| 77 | 26 | 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) |
|
| 78 | { |
||
| 79 | 26 | $this->template = $template; |
|
| 80 | 26 | $this->language = $language; |
|
| 81 | 26 | $this->request = $request; |
|
| 82 | 26 | $this->manager = $manager; |
|
| 83 | 26 | $this->config_text = $config_text; |
|
| 84 | 26 | $this->config = $config; |
|
| 85 | 26 | $this->group_helper = $group_helper; |
|
| 86 | 26 | $this->input = $input; |
|
| 87 | 26 | $this->helper = $helper; |
|
| 88 | 26 | $this->analyser = $analyser; |
|
| 89 | 26 | $this->root_path = $root_path; |
|
| 90 | 26 | $this->php_ext = $php_ext; |
|
| 91 | 26 | } |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Process user request for manage mode |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | 6 | public function mode_manage() |
|
| 99 | { |
||
| 100 | 6 | $this->setup(); |
|
| 101 | |||
| 102 | // Trigger specific action |
||
| 103 | 6 | $action = $this->request->variable('action', ''); |
|
| 104 | 6 | if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete'))) |
|
| 105 | 6 | { |
|
| 106 | 5 | $this->{'action_' . $action}(); |
|
| 107 | 5 | } |
|
| 108 | |||
| 109 | // Otherwise default to this |
||
| 110 | 6 | $this->list_ads(); |
|
| 111 | 6 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Process user request for settings mode |
||
| 115 | * |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | 3 | public function mode_settings() |
|
| 119 | { |
||
| 120 | 3 | $this->setup(); |
|
| 121 | |||
| 122 | 3 | add_form_key('phpbb/ads/settings'); |
|
| 123 | 3 | if ($this->request->is_set_post('submit')) |
|
| 124 | 3 | { |
|
| 125 | // Validate form key |
||
| 126 | 2 | if (check_form_key('phpbb/ads/settings')) |
|
| 127 | 2 | { |
|
| 128 | 1 | $this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0)); |
|
| 129 | 1 | $this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0)); |
|
| 130 | 1 | $this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0)); |
|
| 131 | 1 | $this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0)))); |
|
| 132 | |||
| 133 | 1 | $this->success('ACP_AD_SETTINGS_SAVED'); |
|
| 134 | } |
||
| 135 | |||
| 136 | 1 | $this->helper->assign_errors(array($this->language->lang('FORM_INVALID'))); |
|
| 137 | 1 | } |
|
| 138 | |||
| 139 | 2 | $hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true); |
|
| 140 | 2 | $groups = $this->manager->load_groups(); |
|
| 141 | 2 | View Code Duplication | foreach ($groups as $group) |
| 142 | { |
||
| 143 | 2 | $this->template->assign_block_vars('groups', array( |
|
| 144 | 2 | 'ID' => $group['group_id'], |
|
| 145 | 2 | 'NAME' => $this->group_helper->get_name($group['group_name']), |
|
| 146 | 2 | 'S_SELECTED' => in_array($group['group_id'], $hide_groups), |
|
| 147 | 2 | )); |
|
| 148 | 2 | } |
|
| 149 | |||
| 150 | 2 | $this->template->assign_vars(array( |
|
| 151 | 2 | 'U_ACTION' => $this->u_action, |
|
| 152 | 2 | 'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'], |
|
| 153 | 2 | 'ENABLE_VIEWS' => $this->config['phpbb_ads_enable_views'], |
|
| 154 | 2 | 'ENABLE_CLICKS' => $this->config['phpbb_ads_enable_clicks'], |
|
| 155 | 2 | )); |
|
| 156 | 2 | } |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Set page url |
||
| 160 | * |
||
| 161 | * @param string $u_action Custom form action |
||
| 162 | * @return void |
||
| 163 | */ |
||
| 164 | 20 | public function set_page_url($u_action) |
|
| 165 | { |
||
| 166 | 20 | $this->u_action = $u_action; |
|
| 167 | 20 | } |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Get ACP page title for Ads module |
||
| 171 | * |
||
| 172 | * @return string Language string for Ads ACP module |
||
| 173 | */ |
||
| 174 | 1 | public function get_page_title() |
|
| 175 | { |
||
| 176 | 1 | return $this->language->lang('ACP_PHPBB_ADS_TITLE'); |
|
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Add an advertisement |
||
| 181 | * |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | 8 | public function action_add() |
|
| 185 | { |
||
| 186 | 2 | add_form_key('phpbb/ads/add'); |
|
| 187 | |||
| 188 | 2 | $action = $this->get_submitted_action(); |
|
| 189 | if ($action) |
||
|
|
|||
| 190 | 2 | { |
|
| 191 | 1 | $this->data = $this->input->get_form_data('phpbb/ads/add/'); |
|
| 192 | $this->{$action}(); |
||
| 193 | $this->assign_data(); |
||
| 194 | } |
||
| 195 | else |
||
| 196 | 8 | { |
|
| 197 | 1 | $this->helper->assign_locations(); |
|
| 198 | } |
||
| 199 | |||
| 200 | // Set output vars for display in the template |
||
| 201 | 1 | $this->template->assign_vars(array( |
|
| 202 | 1 | 'S_ADD_AD' => true, |
|
| 203 | 1 | 'U_BACK' => $this->u_action, |
|
| 204 | 1 | 'U_ACTION' => "{$this->u_action}&action=add", |
|
| 205 | 1 | 'PICKER_DATE_FORMAT' => input::DATE_FORMAT, |
|
| 206 | 1 | 'U_FIND_USERNAME' => $this->helper->get_find_username_link(), |
|
| 207 | 1 | )); |
|
| 208 | 1 | } |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Edit an advertisement |
||
| 212 | * |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | 4 | public function action_edit() |
|
| 216 | { |
||
| 217 | 4 | $ad_id = $this->request->variable('id', 0); |
|
| 218 | 4 | add_form_key('phpbb/ads/edit' . $ad_id); |
|
| 219 | |||
| 220 | 4 | $action = $this->get_submitted_action(); |
|
| 221 | if ($action) |
||
| 222 | 4 | { |
|
| 223 | 2 | $this->data = $this->input->get_form_data('phpbb/ads/edit' . $ad_id); |
|
| 224 | $this->{$action}(); |
||
| 225 | } |
||
| 226 | else |
||
| 227 | { |
||
| 228 | 2 | $this->data = $this->manager->get_ad($ad_id); |
|
| 229 | 2 | if (empty($this->data)) |
|
| 230 | 2 | { |
|
| 231 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
| 232 | } |
||
| 233 | |||
| 234 | // Load ad template locations |
||
| 235 | 1 | $this->data['ad_locations'] = $this->manager->get_ad_locations($ad_id); |
|
| 236 | } |
||
| 237 | |||
| 238 | // Set output vars for display in the template |
||
| 239 | 1 | $this->template->assign_vars(array( |
|
| 240 | 1 | 'S_EDIT_AD' => true, |
|
| 241 | 1 | 'EDIT_ID' => $ad_id, |
|
| 242 | 1 | 'U_BACK' => $this->u_action, |
|
| 243 | 1 | 'U_ACTION' => "{$this->u_action}&action=edit&id=" . $ad_id, |
|
| 244 | 1 | 'PICKER_DATE_FORMAT' => input::DATE_FORMAT, |
|
| 245 | 1 | 'U_FIND_USERNAME' => $this->helper->get_find_username_link(), |
|
| 246 | 1 | )); |
|
| 247 | 1 | $this->assign_data(); |
|
| 248 | 1 | } |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Enable an advertisement |
||
| 252 | * |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | 4 | public function action_enable() |
|
| 256 | { |
||
| 257 | 4 | $this->ad_enable(true); |
|
| 258 | 1 | } |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Disable an advertisement |
||
| 262 | * |
||
| 263 | * @return void |
||
| 264 | */ |
||
| 265 | 4 | public function action_disable() |
|
| 266 | { |
||
| 267 | 4 | $this->ad_enable(false); |
|
| 268 | 1 | } |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Delete an advertisement |
||
| 272 | * |
||
| 273 | * @return void |
||
| 274 | */ |
||
| 275 | 3 | public function action_delete() |
|
| 276 | { |
||
| 277 | 3 | $ad_id = $this->request->variable('id', 0); |
|
| 278 | if ($ad_id) |
||
| 279 | 3 | { |
|
| 280 | 3 | if (confirm_box(true)) |
|
| 281 | 3 | { |
|
| 282 | // Get ad data so that we can log ad name |
||
| 283 | 2 | $ad_data = $this->manager->get_ad($ad_id); |
|
| 284 | |||
| 285 | // Delete ad and it's template locations |
||
| 286 | 2 | $this->manager->delete_ad_locations($ad_id); |
|
| 287 | 2 | $success = $this->manager->delete_ad($ad_id); |
|
| 288 | |||
| 289 | // Only notify user on error or if not ajax |
||
| 290 | 2 | if (!$success) |
|
| 291 | 2 | { |
|
| 292 | 1 | $this->error('ACP_AD_DELETE_ERRORED'); |
|
| 293 | } |
||
| 294 | else |
||
| 295 | { |
||
| 296 | 1 | $this->helper->log('DELETE', $ad_data['ad_name']); |
|
| 297 | |||
| 298 | 1 | if (!$this->request->is_ajax()) |
|
| 299 | 1 | { |
|
| 300 | 1 | $this->success('ACP_AD_DELETE_SUCCESS'); |
|
| 301 | } |
||
| 302 | } |
||
| 303 | } |
||
| 304 | else |
||
| 305 | { |
||
| 306 | 1 | confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields(array( |
|
| 307 | 1 | 'id' => $ad_id, |
|
| 308 | 1 | 'i' => $this->request->variable('i', ''), |
|
| 309 | 1 | 'mode' => $this->request->variable('mode', ''), |
|
| 310 | 'action' => 'delete' |
||
| 311 | 1 | ))); |
|
| 312 | } |
||
| 313 | 1 | } |
|
| 314 | 1 | } |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Display the ads |
||
| 318 | * |
||
| 319 | * @return void |
||
| 320 | */ |
||
| 321 | 1 | public function list_ads() |
|
| 322 | { |
||
| 323 | 1 | foreach ($this->manager->get_all_ads() as $row) |
|
| 324 | { |
||
| 325 | 1 | $ad_enabled = (int) $row['ad_enabled']; |
|
| 326 | 1 | $ad_end_date = (int) $row['ad_end_date']; |
|
| 327 | 1 | $ad_expired = ($ad_end_date > 0 && $ad_end_date < time()) || ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit']) || ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit']); |
|
| 328 | 1 | if ($ad_expired && $ad_enabled) |
|
| 329 | 1 | { |
|
| 330 | 1 | $ad_enabled = 0; |
|
| 331 | 1 | $this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0)); |
|
| 332 | 1 | } |
|
| 333 | |||
| 334 | 1 | $this->template->assign_block_vars($ad_expired ? 'expired' : 'ads', array( |
|
| 335 | 1 | 'NAME' => $row['ad_name'], |
|
| 336 | 1 | 'PRIORITY' => $row['ad_priority'], |
|
| 337 | 1 | 'END_DATE' => $this->helper->prepare_end_date($ad_end_date), |
|
| 338 | 1 | 'VIEWS' => $row['ad_views'], |
|
| 339 | 1 | 'CLICKS' => $row['ad_clicks'], |
|
| 340 | 1 | 'VIEWS_LIMIT' => $row['ad_views_limit'], |
|
| 341 | 1 | 'CLICKS_LIMIT' => $row['ad_clicks_limit'], |
|
| 342 | 1 | 'S_EXPIRED' => $ad_expired, |
|
| 343 | 1 | 'S_ENABLED' => $ad_enabled, |
|
| 344 | 1 | 'U_ENABLE' => $this->u_action . '&action=' . ($ad_enabled ? 'disable' : 'enable') . '&id=' . $row['ad_id'], |
|
| 345 | 1 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['ad_id'], |
|
| 346 | 1 | 'U_DELETE' => $this->u_action . '&action=delete&id=' . $row['ad_id'], |
|
| 347 | 1 | )); |
|
| 348 | 1 | } |
|
| 349 | |||
| 350 | // Set output vars for display in the template |
||
| 351 | 1 | $this->template->assign_vars(array( |
|
| 352 | 1 | 'U_ACTION_ADD' => $this->u_action . '&action=add', |
|
| 353 | 1 | 'S_VIEWS_ENABLED' => $this->config['phpbb_ads_enable_views'], |
|
| 354 | 1 | 'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'], |
|
| 355 | 1 | )); |
|
| 356 | 1 | } |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Perform general tasks |
||
| 360 | * |
||
| 361 | * @return void |
||
| 362 | */ |
||
| 363 | 9 | protected function setup() |
|
| 364 | { |
||
| 365 | 9 | if (!function_exists('user_get_id_name')) |
|
| 366 | 9 | { |
|
| 367 | include $this->root_path . 'includes/functions_user.' . $this->php_ext; |
||
| 368 | } |
||
| 369 | |||
| 370 | 9 | $this->language->add_lang('posting'); // Used by banner_upload() file errors |
|
| 371 | 9 | $this->language->add_lang('acp', 'phpbb/ads'); |
|
| 372 | |||
| 373 | 9 | $this->template->assign_var('S_PHPBB_ADS', true); |
|
| 374 | 9 | } |
|
| 375 | |||
| 376 | 6 | protected function get_submitted_action() |
|
| 377 | { |
||
| 378 | 6 | $actions = array('preview', 'submit', 'upload_banner', 'analyse_ad_code'); |
|
| 379 | 6 | foreach ($actions as $action) |
|
| 380 | { |
||
| 381 | 6 | if ($this->request->is_set_post($action)) |
|
| 382 | 6 | { |
|
| 383 | 3 | return $action; |
|
| 384 | } |
||
| 385 | 6 | } |
|
| 386 | |||
| 387 | 3 | return false; |
|
| 388 | } |
||
| 389 | |||
| 390 | 1 | protected function assign_data() |
|
| 391 | { |
||
| 392 | 1 | $this->helper->assign_locations($this->data['ad_locations']); |
|
| 393 | 1 | $this->helper->assign_form_data($this->data); |
|
| 394 | 1 | $this->helper->assign_errors($this->input->get_errors()); |
|
| 395 | 1 | } |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Enable/disable an advertisement |
||
| 399 | * |
||
| 400 | * @param bool $enable Enable or disable the advertisement? |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | 6 | protected function ad_enable($enable) |
|
| 404 | { |
||
| 405 | 6 | $ad_id = $this->request->variable('id', 0); |
|
| 406 | |||
| 407 | 6 | $success = $this->manager->update_ad($ad_id, array( |
|
| 408 | 6 | 'ad_enabled' => (int) $enable, |
|
| 409 | 6 | )); |
|
| 410 | |||
| 411 | // If AJAX was used, show user a result message |
||
| 412 | 6 | if ($this->request->is_ajax()) |
|
| 413 | 6 | { |
|
| 414 | 2 | $json_response = new \phpbb\json_response; |
|
| 415 | 2 | $json_response->send(array( |
|
| 416 | 2 | 'text' => $this->language->lang($enable ? 'ENABLED' : 'DISABLED'), |
|
| 417 | 2 | 'title' => $this->language->lang('AD_ENABLE_TITLE', (int) $enable), |
|
| 418 | 2 | )); |
|
| 419 | } |
||
| 420 | |||
| 421 | // Otherwise, show traditional infobox |
||
| 422 | if ($success) |
||
| 423 | 4 | { |
|
| 424 | 2 | $this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS'); |
|
| 425 | } |
||
| 426 | else |
||
| 427 | { |
||
| 428 | 2 | $this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED'); |
|
| 429 | } |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Prepare advertisement preview |
||
| 434 | * |
||
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | protected function preview() |
||
| 438 | { |
||
| 439 | $this->template->assign_var('PREVIEW', htmlspecialchars_decode($this->data['ad_code'])); |
||
| 440 | } |
||
| 441 | |||
| 442 | protected function upload_banner() |
||
| 443 | { |
||
| 444 | $this->data['ad_code'] = $this->input->banner_upload($this->data['ad_code']); |
||
| 445 | } |
||
| 446 | |||
| 447 | protected function analyse_ad_code() |
||
| 448 | { |
||
| 449 | $this->analyser->run($this->data['ad_code']); |
||
| 450 | } |
||
| 451 | |||
| 452 | protected function submit() |
||
| 453 | { |
||
| 454 | $ad_id = $this->request->variable('id', 0); |
||
| 455 | if ($ad_id) |
||
| 456 | { |
||
| 457 | $success = $this->manager->update_ad($ad_id, $this->data); |
||
| 458 | |||
| 459 | if ($success) |
||
| 460 | { |
||
| 461 | // Only insert new ad locations to DB when ad exists |
||
| 462 | $this->manager->delete_ad_locations($ad_id); |
||
| 463 | $this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']); |
||
| 464 | |||
| 465 | $this->helper->log('EDIT', $this->data['ad_name']); |
||
| 466 | |||
| 467 | $this->success('ACP_AD_EDIT_SUCCESS'); |
||
| 468 | } |
||
| 469 | |||
| 470 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
||
| 471 | } |
||
| 472 | |||
| 473 | $ad_id = $this->manager->insert_ad($this->data); |
||
| 474 | $this->manager->insert_ad_locations($ad_id, $this->data['ad_locations']); |
||
| 475 | |||
| 476 | $this->helper->log('ADD', $this->data['ad_name']); |
||
| 477 | |||
| 478 | $this->success('ACP_AD_ADD_SUCCESS'); |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Print success message. |
||
| 483 | * |
||
| 484 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 485 | */ |
||
| 486 | 4 | protected function success() |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Print error message. |
||
| 493 | * |
||
| 494 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 495 | */ |
||
| 496 | 4 | protected function error() |
|
| 500 | } |
||
| 501 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: