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 | */ |
||
| 70 | 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) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Set page url |
||
| 91 | * |
||
| 92 | * @param string $u_action Custom form action |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | 27 | public function set_page_url($u_action) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Get ACP page title for Ads module |
||
| 102 | * |
||
| 103 | * @return string Language string for Ads ACP module |
||
| 104 | */ |
||
| 105 | 1 | public function get_page_title() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Process user request for settings mode |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | 3 | public function mode_settings() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Process user request for manage mode |
||
| 154 | * |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | 29 | public function mode_manage() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Add an advertisement |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | 17 | protected function action_add() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Edit an advertisement |
||
| 203 | * |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | 7 | protected function action_edit() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Enable an advertisement |
||
| 240 | * |
||
| 241 | * @return void |
||
| 242 | */ |
||
| 243 | 4 | protected function action_enable() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Disable an advertisement |
||
| 250 | * |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | 4 | protected function action_disable() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Delete an advertisement |
||
| 260 | * |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | 3 | protected function action_delete() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Display the list of all ads |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | 1 | protected function list_ads() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Get what action user wants to do with the form. |
||
| 348 | * Possible options are: |
||
| 349 | * - preview ad code |
||
| 350 | * - upload banner to display in an ad code |
||
| 351 | * - analyse ad code |
||
| 352 | * - submit form (either add or edit an ad) |
||
| 353 | * |
||
| 354 | * @return string|false Action name or false when no action was submitted |
||
| 355 | */ |
||
| 356 | 13 | protected function get_submitted_action() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Enable/disable an advertisement |
||
| 372 | * |
||
| 373 | * @param bool $enable Enable or disable the advertisement? |
||
| 374 | * @return void |
||
| 375 | */ |
||
| 376 | 6 | protected function ad_enable($enable) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Submit action "preview". |
||
| 407 | * Prepare advertisement preview. |
||
| 408 | * |
||
| 409 | * @return void |
||
| 410 | */ |
||
| 411 | 2 | protected function preview() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Submit action "upload_banner". |
||
| 418 | * Upload banner and append it to the ad code. |
||
| 419 | * |
||
| 420 | * @return void |
||
| 421 | */ |
||
| 422 | 1 | protected function upload_banner() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Submit action "analyse_ad_code". |
||
| 429 | * Upload banner and append it to the ad code. |
||
| 430 | * |
||
| 431 | * @return void |
||
| 432 | */ |
||
| 433 | 1 | protected function analyse_ad_code() |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Submit action "submit_add". |
||
| 440 | * Add new ad. |
||
| 441 | * |
||
| 442 | * @return void |
||
| 443 | */ |
||
| 444 | 2 | protected function submit_add() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Submit action "submit_edit". |
||
| 459 | * Edit ad. |
||
| 460 | * |
||
| 461 | * @return void |
||
| 462 | */ |
||
| 463 | 4 | protected function submit_edit() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Print success message. |
||
| 486 | * |
||
| 487 | * @param string $msg Message lang key |
||
| 488 | */ |
||
| 489 | 6 | protected function success($msg) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Print error message. |
||
| 496 | * |
||
| 497 | * @param string $msg Message lang key |
||
| 498 | */ |
||
| 499 | 6 | protected function error($msg) |
|
| 503 | } |
||
| 504 |