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\admanagement\ad\manager */ |
||
| 31 | protected $manager; |
||
| 32 | |||
| 33 | /** @var \phpbb\admanagement\location\manager */ |
||
| 34 | protected $location_manager; |
||
| 35 | |||
| 36 | /** @var \phpbb\log\log */ |
||
| 37 | protected $log; |
||
| 38 | |||
| 39 | /** @var \phpbb\config\config */ |
||
| 40 | protected $config; |
||
| 41 | |||
| 42 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 43 | protected $db; |
||
| 44 | |||
| 45 | /** @var string php_ext */ |
||
| 46 | protected $php_ext; |
||
| 47 | |||
| 48 | /** @var string ext_path */ |
||
| 49 | protected $ext_path; |
||
| 50 | |||
| 51 | /** @var string groups_table */ |
||
| 52 | protected $groups_table; |
||
| 53 | |||
| 54 | /** @var string Custom form action */ |
||
| 55 | protected $u_action; |
||
| 56 | |||
| 57 | /** @var array Form validation errors */ |
||
| 58 | protected $errors = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructor |
||
| 62 | * |
||
| 63 | 31 | * @param \phpbb\template\template $template Template object |
|
| 64 | * @param \phpbb\user $user User object |
||
| 65 | 31 | * @param \phpbb\request\request $request Request object |
|
| 66 | 31 | * @param \phpbb\admanagement\ad\manager $manager Advertisement manager object |
|
| 67 | 31 | * @param \phpbb\admanagement\location\manager $location_manager Template location manager object |
|
| 68 | 31 | * @param \phpbb\log\log $log The phpBB log system |
|
| 69 | 31 | * @param \phpbb\config\config $config Config object |
|
| 70 | 31 | * @param \phpbb\db\driver\driver_interface $db Database object |
|
| 71 | 31 | * @param string $php_ext PHP extension |
|
| 72 | 31 | * @param string $ext_path Path to this extension |
|
| 73 | 31 | * @param string $groups_table Groups table |
|
| 74 | */ |
||
| 75 | 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, \phpbb\log\log $log, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, $php_ext, $ext_path, $groups_table) |
||
| 89 | 6 | ||
| 90 | 5 | /** |
|
| 91 | 5 | * Process user request for manage mode |
|
| 92 | * |
||
| 93 | * @return void |
||
| 94 | 6 | */ |
|
| 95 | 6 | public function mode_manage() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Process user request for settings mode |
||
| 112 | * |
||
| 113 | 1 | * @return void |
|
| 114 | */ |
||
| 115 | 1 | public function mode_settings() |
|
| 149 | 5 | ||
| 150 | /** |
||
| 151 | * Set page url |
||
| 152 | 1 | * |
|
| 153 | * @param string $u_action Custom form action |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | 6 | public function set_page_url($u_action) |
|
| 160 | 6 | ||
| 161 | 6 | /** |
|
| 162 | * Get ACP page title for Ads module |
||
| 163 | * |
||
| 164 | * @return string Language string for Ads ACP module |
||
| 165 | */ |
||
| 166 | public function get_page_title() |
||
| 170 | 9 | ||
| 171 | 9 | /** |
|
| 172 | 9 | * Add an advertisement |
|
| 173 | * |
||
| 174 | 9 | * @return void |
|
| 175 | 9 | */ |
|
| 176 | 9 | public function action_add() |
|
| 215 | 6 | ||
| 216 | 6 | /** |
|
| 217 | 6 | * Edit an advertisement |
|
| 218 | 6 | * |
|
| 219 | 6 | * @return void |
|
| 220 | 6 | */ |
|
| 221 | 6 | public function action_edit() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Enable an advertisement |
||
| 279 | * |
||
| 280 | 1 | * @return void |
|
| 281 | 1 | */ |
|
| 282 | 1 | public function action_enable() |
|
| 286 | |||
| 287 | 1 | /** |
|
| 288 | 1 | * Disable an advertisement |
|
| 289 | * |
||
| 290 | * @return void |
||
| 291 | */ |
||
| 292 | public function action_disable() |
||
| 296 | |||
| 297 | 1 | /** |
|
| 298 | * Delete an advertisement |
||
| 299 | 1 | * |
|
| 300 | 1 | * @return void |
|
| 301 | 1 | */ |
|
| 302 | 1 | public function action_delete() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Display the ads |
||
| 345 | * |
||
| 346 | * @return void |
||
| 347 | */ |
||
| 348 | public function list_ads() |
||
| 375 | |||
| 376 | 13 | /** |
|
| 377 | 13 | * Perform general tasks |
|
| 378 | 2 | * |
|
| 379 | 2 | * @return void |
|
| 380 | */ |
||
| 381 | protected function setup() |
||
| 387 | 13 | ||
| 388 | 2 | /** |
|
| 389 | 2 | * Enable/disable an advertisement |
|
| 390 | * |
||
| 391 | * @param bool $enable Enable or disable the advertisement? |
||
| 392 | 13 | * @return void |
|
| 393 | 13 | */ |
|
| 394 | 4 | protected function ad_enable($enable) |
|
| 422 | 11 | ||
| 423 | 11 | /** |
|
| 424 | * Get admin form data. |
||
| 425 | 11 | * |
|
| 426 | 11 | * @param string $form_name The form name. |
|
| 427 | 11 | * @return array Form data |
|
| 428 | 11 | */ |
|
| 429 | 11 | protected function get_form_data($form_name) |
|
| 477 | 2 | ||
| 478 | /** |
||
| 479 | 2 | * Assign form data to the template. |
|
| 480 | 2 | * |
|
| 481 | * @param array $data The form data. |
||
| 482 | * @return void |
||
| 483 | */ |
||
| 484 | protected function assign_form_data($data) |
||
| 497 | 5 | /** |
|
| 498 | * Prepare end date for display |
||
| 499 | 5 | * |
|
| 500 | * @param mixed $end_date End date. |
||
| 501 | * @return string End date prepared for display. |
||
| 502 | */ |
||
| 503 | protected function prepare_end_date($end_date) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Assign template locations data to the template. |
||
| 519 | * |
||
| 520 | * @param mixed $data The form data or nothing. |
||
| 521 | * @return void |
||
| 522 | */ |
||
| 523 | protected function assign_locations($data = false) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Prepare advertisement preview |
||
| 538 | * |
||
| 539 | * @param string $code Ad code to preview |
||
| 540 | * @return void |
||
| 541 | */ |
||
| 542 | protected function ad_preview($code) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Print success message. |
||
| 549 | * |
||
| 550 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 551 | */ |
||
| 552 | protected function success() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Print error message. |
||
| 559 | * |
||
| 560 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 561 | */ |
||
| 562 | protected function error() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Log action |
||
| 569 | * |
||
| 570 | * @param string $action Performed action in uppercase |
||
| 571 | * @param string $ad_name Advertisement name |
||
| 572 | * @return void |
||
| 573 | */ |
||
| 574 | protected function log($action, $ad_name) |
||
| 578 | } |
||
| 579 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.