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 | |||
| 20 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 21 | protected $db; |
||
| 22 | |||
| 23 | /** @var \phpbb\template\template */ |
||
| 24 | protected $template; |
||
| 25 | |||
| 26 | /** @var \phpbb\user */ |
||
| 27 | protected $user; |
||
| 28 | |||
| 29 | /** @var \phpbb\request\request */ |
||
| 30 | protected $request; |
||
| 31 | |||
| 32 | /** @var \phpbb\admanagement\location\manager */ |
||
| 33 | protected $location_manager; |
||
| 34 | |||
| 35 | /** @var string ads_table */ |
||
| 36 | protected $ads_table; |
||
| 37 | |||
| 38 | /** @var string ad_locations_table */ |
||
| 39 | protected $ad_locations_table; |
||
| 40 | |||
| 41 | /** @var string php_ext */ |
||
| 42 | protected $php_ext; |
||
| 43 | |||
| 44 | /** @var string ext_path */ |
||
| 45 | protected $ext_path; |
||
| 46 | |||
| 47 | /** @var string Custom form action */ |
||
| 48 | protected $u_action; |
||
| 49 | |||
| 50 | /** @var array Form validation errors */ |
||
| 51 | protected $errors = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor |
||
| 55 | * |
||
| 56 | * @param \phpbb\db\driver\driver_interface $db DB driver interface |
||
| 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\admanagement\location\manager $location_manager Template location manager object |
||
| 61 | * @param string $ads_table Ads table |
||
| 62 | * @param string $ad_locations_table Ad locations table |
||
| 63 | * @param string $php_ext PHP extension |
||
| 64 | * @param string $ext_path Path to this extension |
||
| 65 | */ |
||
| 66 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\admanagement\location\manager $location_manager, $ads_table, $ad_locations_table, $php_ext, $ext_path) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Process user request |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | public function main() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set page url |
||
| 101 | * |
||
| 102 | * @param string $u_action Custom form action |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | public function set_page_url($u_action) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get ACP page title for Ads module |
||
| 112 | * |
||
| 113 | * @return string Language string for Ads ACP module |
||
| 114 | */ |
||
| 115 | public function get_page_title() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Add an advertisement |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function action_add() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Edit an advertisement |
||
| 177 | * |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public function action_edit() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Enable an advertisement |
||
| 269 | * |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | public function action_enable() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Disable an advertisement |
||
| 279 | * |
||
| 280 | * @return void |
||
| 281 | */ |
||
| 282 | public function action_disable() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Delete an advertisement |
||
| 289 | * |
||
| 290 | * @return void |
||
| 291 | */ |
||
| 292 | public function action_delete() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Display the ads |
||
| 333 | * |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | public function list_ads() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Enable/disable an advertisement |
||
| 365 | * |
||
| 366 | * @param bool $enable Enable or disable the advertisement? |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | protected function ad_enable($enable) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Check the form key. |
||
| 402 | * |
||
| 403 | * @param string $form_name The name of the form. |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | protected function check_form_key($form_name) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get admin form data. |
||
| 416 | * |
||
| 417 | * @return array Form data |
||
| 418 | */ |
||
| 419 | protected function get_form_data() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Validate form data. |
||
| 432 | * |
||
| 433 | * @param array $data The form data. |
||
| 434 | * @return void |
||
| 435 | */ |
||
| 436 | protected function validate($data) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Assign form data to the template. |
||
| 450 | * |
||
| 451 | * @param array $data The form data. |
||
| 452 | * @return void |
||
| 453 | */ |
||
| 454 | protected function assign_form_data($data) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Assign template locations data to the template. |
||
| 469 | * |
||
| 470 | * @param mixed $data The form data or nothing. |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | protected function assign_locations($data = false) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Print success message. |
||
| 488 | * |
||
| 489 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 490 | */ |
||
| 491 | protected function success() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Print error message. |
||
| 498 | * |
||
| 499 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 500 | */ |
||
| 501 | protected function error() |
||
| 505 | } |
||
| 506 |