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\db_text */ |
||
| 40 | protected $config_text; |
||
| 41 | |||
| 42 | /** @var \phpbb\cache\driver\driver_interface */ |
||
| 43 | protected $cache; |
||
| 44 | |||
| 45 | /** @var string php_ext */ |
||
| 46 | protected $php_ext; |
||
| 47 | |||
| 48 | /** @var string ext_path */ |
||
| 49 | protected $ext_path; |
||
| 50 | |||
| 51 | /** @var string Custom form action */ |
||
| 52 | protected $u_action; |
||
| 53 | |||
| 54 | /** @var array Form validation errors */ |
||
| 55 | protected $errors = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor |
||
| 59 | * |
||
| 60 | * @param \phpbb\template\template $template Template object |
||
| 61 | * @param \phpbb\user $user User object |
||
| 62 | * @param \phpbb\request\request $request Request object |
||
| 63 | * @param \phpbb\admanagement\ad\manager $manager Advertisement manager object |
||
| 64 | * @param \phpbb\admanagement\location\manager $location_manager Template location manager object |
||
| 65 | * @param \phpbb\log\log $log The phpBB log system |
||
| 66 | * @param \phpbb\config\db_text $config_text Config text object |
||
| 67 | * @param \phpbb\cache\driver\driver_interface $cache Cache object |
||
| 68 | * @param string $php_ext PHP extension |
||
| 69 | * @param string $ext_path Path to this extension |
||
| 70 | */ |
||
| 71 | 31 | 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\db_text $config_text, \phpbb\cache\driver\driver_interface $cache, $php_ext, $ext_path) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Process user request for manage mode |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | 6 | public function mode_manage() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Process user request for settings mode |
||
| 107 | * |
||
| 108 | * @return void |
||
| 109 | */ |
||
| 110 | public function mode_settings() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Set page url |
||
| 146 | * |
||
| 147 | * @param string $u_action Custom form action |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | 25 | public function set_page_url($u_action) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Get ACP page title for Ads module |
||
| 157 | * |
||
| 158 | * @return string Language string for Ads ACP module |
||
| 159 | */ |
||
| 160 | 1 | public function get_page_title() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Add an advertisement |
||
| 167 | * |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | 15 | public function action_add() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Edit an advertisement |
||
| 212 | * |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | 9 | public function action_edit() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Enable an advertisement |
||
| 273 | * |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | 3 | public function action_enable() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Disable an advertisement |
||
| 283 | * |
||
| 284 | * @return void |
||
| 285 | */ |
||
| 286 | 3 | public function action_disable() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Delete an advertisement |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | 3 | public function action_delete() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Display the ads |
||
| 339 | * |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | 1 | public function list_ads() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Perform general tasks |
||
| 372 | * |
||
| 373 | * @return void |
||
| 374 | */ |
||
| 375 | 6 | protected function setup() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Enable/disable an advertisement |
||
| 384 | * |
||
| 385 | * @param bool $enable Enable or disable the advertisement? |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | 4 | protected function ad_enable($enable) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Get admin form data. |
||
| 419 | * |
||
| 420 | * @param string $form_name The form name. |
||
| 421 | * @return array Form data |
||
| 422 | */ |
||
| 423 | 13 | protected function get_form_data($form_name) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Assign form data to the template. |
||
| 474 | * |
||
| 475 | * @param array $data The form data. |
||
| 476 | * @return void |
||
| 477 | */ |
||
| 478 | 11 | protected function assign_form_data($data) |
|
| 491 | /** |
||
| 492 | * Prepare end date for display |
||
| 493 | * |
||
| 494 | * @param mixed $end_date End date. |
||
| 495 | * @return string End date prepared for display. |
||
| 496 | */ |
||
| 497 | 11 | protected function prepare_end_date($end_date) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Assign template locations data to the template. |
||
| 513 | * |
||
| 514 | * @param mixed $data The form data or nothing. |
||
| 515 | * @return void |
||
| 516 | */ |
||
| 517 | 12 | protected function assign_locations($data = false) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Prepare advertisement preview |
||
| 532 | * |
||
| 533 | * @param string $code Ad code to preview |
||
| 534 | * @return void |
||
| 535 | */ |
||
| 536 | 2 | protected function ad_preview($code) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Print success message. |
||
| 543 | * |
||
| 544 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 545 | */ |
||
| 546 | 5 | protected function success() |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Print error message. |
||
| 553 | * |
||
| 554 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
| 555 | */ |
||
| 556 | 5 | protected function error() |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Log action |
||
| 563 | * |
||
| 564 | * @param string $action Performed action in uppercase |
||
| 565 | * @param string $ad_name Advertisement name |
||
| 566 | * @return void |
||
| 567 | */ |
||
| 568 | 3 | protected function log($action, $ad_name) |
|
| 572 | } |
||
| 573 |
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.