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\ad\manager */ |
||
| 33 | protected $manager; |
||
| 34 | |||
| 35 | /** @var \phpbb\admanagement\location\manager */ |
||
| 36 | protected $location_manager; |
||
| 37 | |||
| 38 | /** @var string php_ext */ |
||
| 39 | protected $php_ext; |
||
| 40 | |||
| 41 | /** @var string ext_path */ |
||
| 42 | protected $ext_path; |
||
| 43 | |||
| 44 | /** @var string Custom form action */ |
||
| 45 | protected $u_action; |
||
| 46 | |||
| 47 | /** @var array Form validation errors */ |
||
| 48 | protected $errors = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor |
||
| 52 | * |
||
| 53 | * @param \phpbb\db\driver\driver_interface $db DB driver interface |
||
| 54 | * @param \phpbb\template\template $template Template object |
||
| 55 | * @param \phpbb\user $user User object |
||
| 56 | * @param \phpbb\request\request $request Request object |
||
| 57 | * @param \phpbb\admanagement\ad\manager $manager Advertisement manager object |
||
| 58 | * @param \phpbb\admanagement\location\manager $location_manager Template location manager object |
||
| 59 | * @param string $php_ext PHP extension |
||
| 60 | * @param string $ext_path Path to this extension |
||
| 61 | */ |
||
| 62 | 27 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\admanagement\ad\manager $manager, \phpbb\admanagement\location\manager $location_manager, $php_ext, $ext_path) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Process user request |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | 6 | public function main() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Set page url |
||
| 96 | * |
||
| 97 | * @param string $u_action Custom form action |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | 21 | public function set_page_url($u_action) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Get ACP page title for Ads module |
||
| 107 | * |
||
| 108 | * @return string Language string for Ads ACP module |
||
| 109 | */ |
||
| 110 | 1 | public function get_page_title() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Add an advertisement |
||
| 117 | * |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | 5 | public function action_add() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Edit an advertisement |
||
| 151 | * |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | 14 | public function action_edit() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Enable an advertisement |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | 3 | public function action_enable() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Disable an advertisement |
||
| 215 | * |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | 3 | public function action_disable() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Delete an advertisement |
||
| 225 | * |
||
| 226 | * @return void |
||
| 227 | */ |
||
| 228 | 3 | public function action_delete() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Display the ads |
||
| 262 | * |
||
| 263 | * @return void |
||
| 264 | */ |
||
| 265 | 1 | public function list_ads() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Enable/disable an advertisement |
||
| 290 | * |
||
| 291 | * @param bool $enable Enable or disable the advertisement? |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | 4 | protected function ad_enable($enable) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Get admin form data. |
||
| 325 | * |
||
| 326 | * @return array Form data |
||
| 327 | */ |
||
| 328 | 9 | protected function get_form_data() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Validate form data. |
||
| 341 | * |
||
| 342 | * @param array $data The form data. |
||
| 343 | * @param string $form_name The form name. |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | 9 | protected function validate($data, $form_name) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Assign form data to the template. |
||
| 365 | * |
||
| 366 | * @param array $data The form data. |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | 7 | protected function assign_form_data($data) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Assign template locations data to the template. |
||
| 384 | * |
||
| 385 | * @param mixed $data The form data or nothing. |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | 8 | protected function assign_locations($data = false) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Print success message. |
||
| 403 | * |
||
| 404 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 405 | */ |
||
| 406 | 5 | protected function success() |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Print error message. |
||
| 413 | * |
||
| 414 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 415 | */ |
||
| 416 | 5 | protected function error() |
|
| 420 | } |
||
| 421 |