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 array Form data */ |
||
| 21 | protected $data = array(); |
||
| 22 | |||
| 23 | /** @var \phpbb\template\template */ |
||
| 24 | protected $template; |
||
| 25 | |||
| 26 | /** @var \phpbb\language\language */ |
||
| 27 | protected $language; |
||
| 28 | |||
| 29 | /** @var \phpbb\request\request */ |
||
| 30 | protected $request; |
||
| 31 | |||
| 32 | /** @var \phpbb\ads\ad\manager */ |
||
| 33 | protected $manager; |
||
| 34 | |||
| 35 | /** @var \phpbb\config\db_text */ |
||
| 36 | protected $config_text; |
||
| 37 | |||
| 38 | /** @var \phpbb\config\config */ |
||
| 39 | protected $config; |
||
| 40 | |||
| 41 | /** @var \phpbb\group\helper */ |
||
| 42 | protected $group_helper; |
||
| 43 | |||
| 44 | /** @var \phpbb\ads\controller\admin_input */ |
||
| 45 | protected $input; |
||
| 46 | |||
| 47 | /** @var \phpbb\ads\controller\admin_helper */ |
||
| 48 | protected $helper; |
||
| 49 | |||
| 50 | /** @var \phpbb\ads\analyser\manager */ |
||
| 51 | protected $analyser; |
||
| 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 \phpbb\ads\analyser\manager $analyser Ad code analyser object |
||
| 69 | * @param string $root_path phpBB root path |
||
| 70 | * @param string $php_ext PHP extension |
||
| 71 | */ |
||
| 72 | 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) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Set page url |
||
| 93 | * |
||
| 94 | * @param string $u_action Custom form action |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | 27 | public function set_page_url($u_action) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Get ACP page title for Ads module |
||
| 104 | * |
||
| 105 | * @return string Language string for Ads ACP module |
||
| 106 | */ |
||
| 107 | 1 | public function get_page_title() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Process user request for settings mode |
||
| 114 | * |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | 3 | public function mode_settings() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Process user request for manage mode |
||
| 156 | * |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | 29 | public function mode_manage() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Add an advertisement |
||
| 176 | * |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | 17 | protected function action_add() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Edit an advertisement |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | 7 | protected function action_edit() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Enable an advertisement |
||
| 242 | * |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | 4 | protected function action_enable() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Disable an advertisement |
||
| 252 | * |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | 4 | protected function action_disable() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Delete an advertisement |
||
| 262 | * |
||
| 263 | * @return void |
||
| 264 | */ |
||
| 265 | 3 | protected function action_delete() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Display the list of all ads |
||
| 308 | * |
||
| 309 | * @return void |
||
| 310 | */ |
||
| 311 | 1 | protected function list_ads() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Get what action user wants to do with the form. |
||
| 350 | * Possible options are: |
||
| 351 | * - preview ad code |
||
| 352 | * - upload banner to display in an ad code |
||
| 353 | * - analyse ad code |
||
| 354 | * - submit form (either add or edit an ad) |
||
| 355 | * |
||
| 356 | * @return mixed Action name or false when no action was submitted |
||
| 357 | */ |
||
| 358 | 13 | protected function get_submitted_action() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Enable/disable an advertisement |
||
| 374 | * |
||
| 375 | * @param bool $enable Enable or disable the advertisement? |
||
| 376 | * @return void |
||
| 377 | */ |
||
| 378 | 6 | protected function ad_enable($enable) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Submit action "preview". |
||
| 409 | * Prepare advertisement preview. |
||
| 410 | * |
||
| 411 | * @return void |
||
| 412 | */ |
||
| 413 | 2 | protected function preview() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Submit action "upload_banner". |
||
| 420 | * Upload banner and append it to the ad code. |
||
| 421 | * |
||
| 422 | * @return void |
||
| 423 | */ |
||
| 424 | 1 | protected function upload_banner() |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Submit action "analyse_ad_code". |
||
| 431 | * Upload banner and append it to the ad code. |
||
| 432 | * |
||
| 433 | * @return void |
||
| 434 | */ |
||
| 435 | 1 | protected function analyse_ad_code() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Submit action "submit_add". |
||
| 442 | * Add new ad. |
||
| 443 | * |
||
| 444 | * @return void |
||
| 445 | */ |
||
| 446 | 2 | protected function submit_add() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Submit action "submit_edit". |
||
| 461 | * Edit ad. |
||
| 462 | * |
||
| 463 | * @return void |
||
| 464 | */ |
||
| 465 | 4 | protected function submit_edit() |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Print success message. |
||
| 488 | * |
||
| 489 | * @param string $msg Message lang key |
||
| 490 | */ |
||
| 491 | 6 | protected function success($msg) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Print error message. |
||
| 498 | * |
||
| 499 | * @param string $msg Message lang key |
||
| 500 | */ |
||
| 501 | 6 | protected function error($msg) |
|
| 505 | } |
||
| 506 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.