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 |
||
| 16 | class admin_controller |
||
| 17 | { |
||
| 18 | const MAX_NAME_LENGTH = 255; |
||
| 19 | const DATE_FORMAT = 'Y-m-d'; |
||
| 20 | |||
| 21 | /** @var \phpbb\template\template */ |
||
| 22 | protected $template; |
||
| 23 | |||
| 24 | /** @var \phpbb\user */ |
||
| 25 | protected $user; |
||
| 26 | |||
| 27 | /** @var \phpbb\request\request */ |
||
| 28 | protected $request; |
||
| 29 | |||
| 30 | /** @var \phpbb\ads\ad\manager */ |
||
| 31 | protected $manager; |
||
| 32 | |||
| 33 | /** @var \phpbb\ads\location\manager */ |
||
| 34 | protected $location_manager; |
||
| 35 | |||
| 36 | /** @var \phpbb\log\log */ |
||
| 37 | protected $log; |
||
| 38 | |||
| 39 | /** @var \phpbb\config\db_text */ |
||
| 40 | protected $config_text; |
||
| 41 | |||
| 42 | /** @var string php_ext */ |
||
| 43 | protected $php_ext; |
||
| 44 | |||
| 45 | /** @var string ext_path */ |
||
| 46 | protected $ext_path; |
||
| 47 | |||
| 48 | /** @var string Custom form action */ |
||
| 49 | protected $u_action; |
||
| 50 | |||
| 51 | /** @var array Form validation errors */ |
||
| 52 | protected $errors = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Constructor |
||
| 56 | * |
||
| 57 | * @param \phpbb\template\template $template Template object |
||
| 58 | * @param \phpbb\user $user User object |
||
| 59 | * @param \phpbb\request\request $request Request object |
||
| 60 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
| 61 | * @param \phpbb\ads\location\manager $location_manager Template location manager object |
||
| 62 | * @param \phpbb\log\log $log The phpBB log system |
||
| 63 | * @param \phpbb\config\db_text $config_text Config text object |
||
| 64 | * @param string $php_ext PHP extension |
||
| 65 | * @param string $ext_path Path to this extension |
||
| 66 | */ |
||
| 67 | 34 | public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\log\log $log, \phpbb\config\db_text $config_text, $php_ext, $ext_path) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Process user request for manage mode |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | 6 | public function mode_manage() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Process user request for settings mode |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | 3 | public function mode_settings() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Set page url |
||
| 149 | * |
||
| 150 | * @param string $u_action Custom form action |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | 28 | public function set_page_url($u_action) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Get ACP page title for Ads module |
||
| 160 | * |
||
| 161 | * @return string Language string for Ads ACP module |
||
| 162 | */ |
||
| 163 | 1 | public function get_page_title() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Add an advertisement |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | 7 | public function action_add() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Edit an advertisement |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | 9 | public function action_edit() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Enable an advertisement |
||
| 278 | * |
||
| 279 | * @return void |
||
| 280 | */ |
||
| 281 | 3 | public function action_enable() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Disable an advertisement |
||
| 288 | * |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | 3 | public function action_disable() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Delete an advertisement |
||
| 298 | * |
||
| 299 | * @return void |
||
| 300 | */ |
||
| 301 | 3 | public function action_delete() |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Display the ads |
||
| 344 | * |
||
| 345 | * @return void |
||
| 346 | */ |
||
| 347 | 1 | public function list_ads() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Perform general tasks |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | 9 | protected function setup() |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Enable/disable an advertisement |
||
| 389 | * |
||
| 390 | * @param bool $enable Enable or disable the advertisement? |
||
| 391 | * @return void |
||
| 392 | */ |
||
| 393 | 4 | protected function ad_enable($enable) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Get admin form data. |
||
| 424 | * |
||
| 425 | * @param string $form_name The form name. |
||
| 426 | * @return array Form data |
||
| 427 | */ |
||
| 428 | 13 | protected function get_form_data($form_name) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Assign form data to the template. |
||
| 479 | * |
||
| 480 | * @param array $data The form data. |
||
| 481 | * @return void |
||
| 482 | */ |
||
| 483 | 11 | protected function assign_form_data($data) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Prepare end date for display |
||
| 499 | * |
||
| 500 | * @param mixed $end_date End date. |
||
| 501 | * @return string End date prepared for display. |
||
| 502 | */ |
||
| 503 | 11 | protected function prepare_end_date($end_date) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Assign template locations data to the template. |
||
| 520 | * |
||
| 521 | * @param mixed $data The form data or nothing. |
||
| 522 | * @return void |
||
| 523 | */ |
||
| 524 | 12 | protected function assign_locations($data = false) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Prepare advertisement preview |
||
| 539 | * |
||
| 540 | * @param string $code Ad code to preview |
||
| 541 | * @return void |
||
| 542 | */ |
||
| 543 | 2 | protected function ad_preview($code) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Print success message. |
||
| 550 | * |
||
| 551 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 552 | */ |
||
| 553 | 6 | protected function success() |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Print error message. |
||
| 560 | * |
||
| 561 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 562 | */ |
||
| 563 | 5 | protected function error() |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Log action |
||
| 570 | * |
||
| 571 | * @param string $action Performed action in uppercase |
||
| 572 | * @param string $ad_name Advertisement name |
||
| 573 | * @return void |
||
| 574 | */ |
||
| 575 | 3 | protected function log($action, $ad_name) |
|
| 579 | } |
||
| 580 |