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 | 30 | 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 | 30 | $this->template = $template; |
|
| 74 | 30 | $this->language = $language; |
|
| 75 | 30 | $this->request = $request; |
|
| 76 | 30 | $this->manager = $manager; |
|
| 77 | 30 | $this->config_text = $config_text; |
|
| 78 | 30 | $this->config = $config; |
|
| 79 | 30 | $this->group_helper = $group_helper; |
|
| 80 | 30 | $this->input = $input; |
|
| 81 | 30 | $this->helper = $helper; |
|
| 82 | 30 | $this->root_path = $root_path; |
|
| 83 | 30 | $this->php_ext = $php_ext; |
|
| 84 | 30 | } |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Process user request for manage mode |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | 6 | public function mode_manage() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Process user request for settings mode |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | 3 | public function mode_settings() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Set page url |
||
| 153 | * |
||
| 154 | * @param string $u_action Custom form action |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | 24 | public function set_page_url($u_action) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get ACP page title for Ads module |
||
| 164 | * |
||
| 165 | * @return string Language string for Ads ACP module |
||
| 166 | */ |
||
| 167 | 1 | public function get_page_title() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Add an advertisement |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | 12 | public function action_add() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Edit an advertisement |
||
| 227 | * |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | 7 | public function action_edit() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Enable an advertisement |
||
| 296 | * |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | 3 | public function action_enable() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Disable an advertisement |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | 3 | public function action_disable() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Delete an advertisement |
||
| 316 | * |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | 3 | public function action_delete() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Display the ads |
||
| 362 | * |
||
| 363 | * @return void |
||
| 364 | */ |
||
| 365 | 1 | public function list_ads() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Perform general tasks |
||
| 403 | * |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | 9 | protected function setup() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Enable/disable an advertisement |
||
| 419 | * |
||
| 420 | * @param bool $enable Enable or disable the advertisement? |
||
| 421 | * @return void |
||
| 422 | */ |
||
| 423 | 4 | protected function ad_enable($enable) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Prepare advertisement preview |
||
| 454 | * |
||
| 455 | * @param string $code Ad code to preview |
||
| 456 | * @return void |
||
| 457 | */ |
||
| 458 | 2 | protected function ad_preview($code) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Print success message. |
||
| 465 | * |
||
| 466 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 467 | */ |
||
| 468 | 6 | protected function success() |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Print error message. |
||
| 475 | * |
||
| 476 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 477 | */ |
||
| 478 | 5 | protected function error() |
|
| 482 | } |
||
| 483 |