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 | |||
| 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 string ads_table */ |
||
| 33 | protected $ads_table; |
||
| 34 | |||
| 35 | /** @var string php_ext */ |
||
| 36 | protected $php_ext; |
||
| 37 | |||
| 38 | /** @var string phpbb_admin_path */ |
||
| 39 | protected $phpbb_admin_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\db\driver\driver_interface $db DB driver interface |
||
| 51 | * @param \phpbb\template\template $template Template object |
||
| 52 | * @param \phpbb\user $user User object |
||
| 53 | * @param \phpbb\request\request $request Request object |
||
| 54 | * @param string $ads_table Ads table |
||
| 55 | * @param string $php_ext PHP extension |
||
| 56 | * @param string $phpbb_admin_path Path to admin |
||
| 57 | */ |
||
| 58 | 16 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, $ads_table, $php_ext, $phpbb_admin_path) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Process user request |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | 5 | public function main() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Set page url |
||
| 116 | * |
||
| 117 | * @param string $u_action Custom form action |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | 11 | public function set_page_url($u_action) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Get ACP page title for Ads module |
||
| 127 | * |
||
| 128 | * @return string Language string for Ads ACP module |
||
| 129 | */ |
||
| 130 | 1 | public function get_page_title() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Add an advertisement |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | 4 | public function action_add() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Edit an advertisement |
||
| 175 | * |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | 6 | public function action_edit() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Enable/disable an advertisement |
||
| 232 | * |
||
| 233 | * @param bool $enable Enable or disable the advertisement? |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | 4 | public function ad_enable($enable) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Delete an advertisement |
||
| 267 | * |
||
| 268 | * @return void |
||
| 269 | */ |
||
| 270 | 1 | public function action_delete() |
|
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * Display the ads |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | 6 | public function list_ads() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Check the form key. |
||
| 338 | * |
||
| 339 | * @param string $form_name The name of the form. |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | 3 | protected function check_form_key($form_name) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get admin form data. |
||
| 352 | * |
||
| 353 | * @return array Form data |
||
| 354 | */ |
||
| 355 | 3 | protected function get_form_data() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Validate form data. |
||
| 367 | * |
||
| 368 | * @param array $data The form data. |
||
| 369 | * @return void |
||
| 370 | */ |
||
| 371 | 3 | protected function validate($data) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Assign errors to the template. |
||
| 385 | * |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | 2 | protected function assign_errors() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Assign form data to the template. |
||
| 398 | * |
||
| 399 | * @param array $data The form data. |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | 2 | protected function assign_form_data($data) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Print success message. |
||
| 414 | * |
||
| 415 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 416 | */ |
||
| 417 | 4 | protected function success() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Print error message. |
||
| 424 | * |
||
| 425 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 426 | */ |
||
| 427 | 2 | protected function error() |
|
| 431 | } |
||
| 432 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.