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 main_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 main_controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class main_controller |
||
| 16 | { |
||
| 17 | protected $auth; |
||
| 18 | protected $config; |
||
| 19 | protected $container; |
||
| 20 | protected $extension_manager; |
||
| 21 | protected $helper; |
||
| 22 | protected $ppde_entity_currency; |
||
| 23 | protected $ppde_entity_donation_pages; |
||
| 24 | protected $ppde_entity_transactions; |
||
| 25 | protected $ppde_operator_currency; |
||
| 26 | protected $ppde_operator_donation_pages; |
||
| 27 | protected $ppde_operator_transactions; |
||
| 28 | protected $request; |
||
| 29 | protected $template; |
||
| 30 | protected $user; |
||
| 31 | protected $root_path; |
||
| 32 | protected $php_ext; |
||
| 33 | /** @var array */ |
||
| 34 | protected $ext_meta = array(); |
||
| 35 | /** @var string */ |
||
| 36 | protected $ext_name; |
||
| 37 | /** @var string */ |
||
| 38 | private $donation_body; |
||
| 39 | /** @var array */ |
||
| 40 | private $donation_content_data; |
||
| 41 | /** @var string */ |
||
| 42 | private $return_args_url; |
||
| 43 | /** @var string */ |
||
| 44 | private $u_action; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Constructor |
||
| 48 | * |
||
| 49 | * @param \phpbb\auth\auth $auth Auth object |
||
| 50 | * @param \phpbb\config\config $config Config object |
||
| 51 | * @param ContainerInterface $container Service container interface |
||
| 52 | * @param \phpbb\extension\manager $extension_manager An instance of the phpBB extension |
||
| 53 | * manager |
||
| 54 | * @param \phpbb\controller\helper $helper Controller helper object |
||
| 55 | * @param \skouat\ppde\entity\currency $ppde_entity_currency Currency entity object |
||
| 56 | * @param \skouat\ppde\entity\donation_pages $ppde_entity_donation_pages Donation pages entity object |
||
| 57 | * @param \skouat\ppde\entity\transactions $ppde_entity_transactions Transactions log entity object |
||
| 58 | * @param \skouat\ppde\operators\currency $ppde_operator_currency Currency operator object |
||
| 59 | * @param \skouat\ppde\operators\donation_pages $ppde_operator_donation_pages Donation pages operator object |
||
| 60 | * @param \skouat\ppde\operators\transactions $ppde_operator_transactions Transactions log operator object |
||
| 61 | * @param \phpbb\request\request $request Request object |
||
| 62 | * @param \phpbb\template\template $template Template object |
||
| 63 | * @param \phpbb\user $user User object |
||
| 64 | * @param string $root_path phpBB root path |
||
| 65 | * @param string $php_ext phpEx |
||
| 66 | * |
||
| 67 | * @return \skouat\ppde\controller\main_controller |
||
| 68 | * @access public |
||
| 69 | */ |
||
| 70 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, ContainerInterface $container, \phpbb\extension\manager $extension_manager, \phpbb\controller\helper $helper, \skouat\ppde\entity\currency $ppde_entity_currency, \skouat\ppde\entity\donation_pages $ppde_entity_donation_pages, \skouat\ppde\entity\transactions $ppde_entity_transactions, \skouat\ppde\operators\currency $ppde_operator_currency, \skouat\ppde\operators\donation_pages $ppde_operator_donation_pages, \skouat\ppde\operators\transactions $ppde_operator_transactions, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $root_path, $php_ext) |
||
| 89 | |||
| 90 | public function handle() |
||
| 136 | |||
| 137 | public function donorlist_handle() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Set the sort key value |
||
| 236 | * |
||
| 237 | * @param string $sk |
||
| 238 | * @param string $sk_comp |
||
| 239 | * @param string $sd |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | * @access private |
||
| 243 | */ |
||
| 244 | private function set_sort_key($sk, $sk_comp, $sd) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Simply adds an url or & delimiter to the url when params is empty |
||
| 251 | * |
||
| 252 | * @param $url |
||
| 253 | * @param $params |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | * @access private |
||
| 257 | */ |
||
| 258 | private function set_url_delim($url, $params) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param $params |
||
| 265 | * |
||
| 266 | * @return array |
||
| 267 | * @access private |
||
| 268 | */ |
||
| 269 | private function check_params($params_ary, $excluded_keys) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return bool |
||
| 294 | * @access public |
||
| 295 | */ |
||
| 296 | public function can_use_ppde() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return bool |
||
| 303 | * @access public |
||
| 304 | */ |
||
| 305 | public function can_view_ppde_donorlist() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $set_return_args_url |
||
| 312 | * |
||
| 313 | * @return null |
||
| 314 | * @access private |
||
| 315 | */ |
||
| 316 | private function set_return_args_url($set_return_args_url) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get content of current donation pages |
||
| 341 | * |
||
| 342 | * @param string $return_args_url |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | * @access private |
||
| 346 | */ |
||
| 347 | private function get_donation_content_data($return_args_url) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Build pull down menu options of available currency |
||
| 356 | * |
||
| 357 | * @param int $config_value Currency identifier; default: 0 |
||
| 358 | * |
||
| 359 | * @return null |
||
| 360 | * @access public |
||
| 361 | */ |
||
| 362 | public function build_currency_select_menu($config_value = 0) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Build pull down menu options of available currency value |
||
| 385 | * |
||
| 386 | * @return string List of currency value set in ACP for dropdown menu |
||
| 387 | * @access private |
||
| 388 | */ |
||
| 389 | private function build_currency_value_select_menu() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get dropbox config value |
||
| 410 | * |
||
| 411 | * @return bool |
||
| 412 | * @access private |
||
| 413 | */ |
||
| 414 | private function get_dropbox_status() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Force dropbox value to integer |
||
| 421 | * |
||
| 422 | * @param int $value |
||
| 423 | * |
||
| 424 | * @return int |
||
| 425 | */ |
||
| 426 | private function settype_dropbox_int_value($value = 0) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Build PayPal hidden fields |
||
| 438 | * |
||
| 439 | * @return string PayPal hidden field needed to fill PayPal forms |
||
| 440 | * @access private |
||
| 441 | */ |
||
| 442 | private function paypal_hidden_fields() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Get PayPal account id |
||
| 461 | * |
||
| 462 | * @return string $this Paypal account Identifier |
||
| 463 | * @access private |
||
| 464 | */ |
||
| 465 | private function get_account_id() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Check if Sandbox is enabled |
||
| 472 | * |
||
| 473 | * @return bool |
||
| 474 | * @access public |
||
| 475 | */ |
||
| 476 | public function use_sandbox() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Check if IPN is enabled |
||
| 483 | * |
||
| 484 | * @return bool |
||
| 485 | * @access public |
||
| 486 | */ |
||
| 487 | public function use_ipn() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Generate PayPal return URL |
||
| 494 | * |
||
| 495 | * @param string $arg |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | * @access private |
||
| 499 | */ |
||
| 500 | private function generate_paypal_return_url($arg) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Generate PayPal return notify URL |
||
| 507 | * |
||
| 508 | * @return string |
||
| 509 | * @access private |
||
| 510 | */ |
||
| 511 | private function generate_paypal_notify_return_url() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Get PayPal URL |
||
| 518 | * Used in form and in IPN process |
||
| 519 | * |
||
| 520 | * @param bool $is_test_ipn |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | * @access public |
||
| 524 | */ |
||
| 525 | public function get_paypal_url($is_test_ipn = false) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Assign statistics vars to the template |
||
| 532 | * |
||
| 533 | * @return null |
||
| 534 | * @access public |
||
| 535 | */ |
||
| 536 | public function display_stats() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Get default currency symbol |
||
| 560 | * |
||
| 561 | * @param int $id |
||
| 562 | * |
||
| 563 | * @return array |
||
| 564 | * @access public |
||
| 565 | */ |
||
| 566 | public function get_default_currency_data($id = 0) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Retrieve the language key for donation goal |
||
| 573 | * |
||
| 574 | * @param string $currency_symbol Currency symbol |
||
| 575 | * @param bool $on_left Symbol position |
||
| 576 | * |
||
| 577 | * @return string |
||
| 578 | * @access public |
||
| 579 | */ |
||
| 580 | public function get_ppde_goal_langkey($currency_symbol, $on_left = true) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Put the currency on the left or on the right of the amount |
||
| 600 | * |
||
| 601 | * @param int $value |
||
| 602 | * @param string $currency |
||
| 603 | * @param bool $on_left |
||
| 604 | * |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | private function get_amount($value, $currency, $on_left = true) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Retrieve the language key for donation raised |
||
| 614 | * |
||
| 615 | * @param string $currency_symbol Currency symbol |
||
| 616 | * @param bool $on_left Symbol position |
||
| 617 | * |
||
| 618 | * @return string |
||
| 619 | * @access public |
||
| 620 | */ |
||
| 621 | public function get_ppde_raised_langkey($currency_symbol, $on_left = true) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Retrieve the language key for donation used |
||
| 637 | * |
||
| 638 | * @param string $currency_symbol Currency symbol |
||
| 639 | * @param bool $on_left Symbol position |
||
| 640 | * |
||
| 641 | * @return string |
||
| 642 | * @access public |
||
| 643 | */ |
||
| 644 | public function get_ppde_used_langkey($currency_symbol, $on_left = true) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Generate statistics percent for display |
||
| 664 | * |
||
| 665 | * @return null |
||
| 666 | * @access private |
||
| 667 | */ |
||
| 668 | private function generate_stats_percent() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Assign statistics percent vars to template |
||
| 683 | * |
||
| 684 | * @param integer $multiplicand |
||
| 685 | * @param integer $dividend |
||
| 686 | * @param string $type |
||
| 687 | * |
||
| 688 | * @return null |
||
| 689 | * @access public |
||
| 690 | */ |
||
| 691 | private function assign_vars_stats_percent($multiplicand, $dividend, $type = '') |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Send data to the template file |
||
| 701 | * |
||
| 702 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 703 | * @access private |
||
| 704 | */ |
||
| 705 | private function send_data_to_template() |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Check if cURL is available |
||
| 721 | * |
||
| 722 | * @return bool |
||
| 723 | * @access public |
||
| 724 | */ |
||
| 725 | public function check_curl() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Get extension metadata |
||
| 748 | * |
||
| 749 | * @return null |
||
| 750 | * @access protected |
||
| 751 | */ |
||
| 752 | protected function get_ext_meta() |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Load metadata for this extension |
||
| 762 | * |
||
| 763 | * @return array |
||
| 764 | * @access public |
||
| 765 | */ |
||
| 766 | public function load_metadata() |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Retrieve the extension name |
||
| 791 | * |
||
| 792 | * @return null |
||
| 793 | * @access protected |
||
| 794 | */ |
||
| 795 | protected function retrieve_ext_name() |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Check if fsockopen is available |
||
| 803 | * |
||
| 804 | * @return bool |
||
| 805 | * @access public |
||
| 806 | */ |
||
| 807 | public function check_fsockopen() |
||
| 822 | } |
||
| 823 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: