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\template\template */ |
||
| 21 | protected $template; |
||
| 22 | |||
| 23 | /** @var \phpbb\user */ |
||
| 24 | protected $user; |
||
| 25 | |||
| 26 | /** @var \phpbb\request\request */ |
||
| 27 | protected $request; |
||
| 28 | |||
| 29 | /** @var \phpbb\admanagement\ad\manager */ |
||
| 30 | protected $manager; |
||
| 31 | |||
| 32 | /** @var \phpbb\admanagement\location\manager */ |
||
| 33 | protected $location_manager; |
||
| 34 | |||
| 35 | /** @var string php_ext */ |
||
| 36 | protected $php_ext; |
||
| 37 | |||
| 38 | /** @var string ext_path */ |
||
| 39 | protected $ext_path; |
||
| 40 | |||
| 41 | /** @var string Custom form action */ |
||
| 42 | protected $u_action; |
||
| 43 | |||
| 44 | /** @var array Form validation errors */ |
||
| 45 | protected $errors = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor |
||
| 49 | * |
||
| 50 | * @param \phpbb\template\template $template Template object |
||
| 51 | * @param \phpbb\user $user User object |
||
| 52 | * @param \phpbb\request\request $request Request object |
||
| 53 | * @param \phpbb\admanagement\ad\manager $manager Advertisement manager object |
||
| 54 | * @param \phpbb\admanagement\location\manager $location_manager Template location manager object |
||
| 55 | * @param string $php_ext PHP extension |
||
| 56 | * @param string $ext_path Path to this extension |
||
| 57 | */ |
||
| 58 | public function __construct(\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) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Process user request |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | public function main() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Set page url |
||
| 91 | * |
||
| 92 | * @param string $u_action Custom form action |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | 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 | public function get_page_title() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Add an advertisement |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function action_add() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Edit an advertisement |
||
| 146 | * |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | public function action_edit() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Enable an advertisement |
||
| 200 | * |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function action_enable() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Disable an advertisement |
||
| 210 | * |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | public function action_disable() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Delete an advertisement |
||
| 220 | * |
||
| 221 | * @return void |
||
| 222 | */ |
||
| 223 | public function action_delete() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Display the ads |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | public function list_ads() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Enable/disable an advertisement |
||
| 285 | * |
||
| 286 | * @param bool $enable Enable or disable the advertisement? |
||
| 287 | * @return void |
||
| 288 | */ |
||
| 289 | protected function ad_enable($enable) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get admin form data. |
||
| 320 | * |
||
| 321 | * @return array Form data |
||
| 322 | */ |
||
| 323 | protected function get_form_data() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Validate form data. |
||
| 336 | * |
||
| 337 | * @param array $data The form data. |
||
| 338 | * @param string $form_name The form name. |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | protected function validate($data, $form_name) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Assign form data to the template. |
||
| 360 | * |
||
| 361 | * @param array $data The form data. |
||
| 362 | * @return void |
||
| 363 | */ |
||
| 364 | protected function assign_form_data($data) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Assign template locations data to the template. |
||
| 379 | * |
||
| 380 | * @param mixed $data The form data or nothing. |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | protected function assign_locations($data = false) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Print success message. |
||
| 398 | * |
||
| 399 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 400 | */ |
||
| 401 | protected function success() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Print error message. |
||
| 408 | * |
||
| 409 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 410 | */ |
||
| 411 | protected function error() |
||
| 415 | } |
||
| 416 |