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() |
||
223 | |||
224 | /** |
||
225 | * @return bool |
||
226 | * @access public |
||
227 | */ |
||
228 | public function can_use_ppde() |
||
232 | |||
233 | /** |
||
234 | * @return bool |
||
235 | * @access public |
||
236 | */ |
||
237 | public function can_view_ppde_donorlist() |
||
241 | |||
242 | /** |
||
243 | * @param string $set_return_args_url |
||
244 | * |
||
245 | * @return null |
||
246 | * @access private |
||
247 | */ |
||
248 | private function set_return_args_url($set_return_args_url) |
||
270 | |||
271 | /** |
||
272 | * Get content of current donation pages |
||
273 | * |
||
274 | * @param string $return_args_url |
||
275 | * |
||
276 | * @return array |
||
277 | * @access private |
||
278 | */ |
||
279 | private function get_donation_content_data($return_args_url) |
||
285 | |||
286 | /** |
||
287 | * Build pull down menu options of available currency |
||
288 | * |
||
289 | * @param int $config_value Currency identifier; default: 0 |
||
290 | * |
||
291 | * @return null |
||
292 | * @access public |
||
293 | */ |
||
294 | public function build_currency_select_menu($config_value = 0) |
||
314 | |||
315 | /** |
||
316 | * Build pull down menu options of available currency value |
||
317 | * |
||
318 | * @return string List of currency value set in ACP for dropdown menu |
||
319 | * @access private |
||
320 | */ |
||
321 | private function build_currency_value_select_menu() |
||
339 | |||
340 | /** |
||
341 | * Get dropbox config value |
||
342 | * |
||
343 | * @return bool |
||
344 | * @access private |
||
345 | */ |
||
346 | private function get_dropbox_status() |
||
350 | |||
351 | /** |
||
352 | * Force dropbox value to integer |
||
353 | * |
||
354 | * @param int $value |
||
355 | * |
||
356 | * @return int |
||
357 | */ |
||
358 | private function settype_dropbox_int_value($value = 0) |
||
367 | |||
368 | /** |
||
369 | * Build PayPal hidden fields |
||
370 | * |
||
371 | * @return string PayPal hidden field needed to fill PayPal forms |
||
372 | * @access private |
||
373 | */ |
||
374 | private function paypal_hidden_fields() |
||
390 | |||
391 | /** |
||
392 | * Get PayPal account id |
||
393 | * |
||
394 | * @return string $this Paypal account Identifier |
||
395 | * @access private |
||
396 | */ |
||
397 | private function get_account_id() |
||
401 | |||
402 | /** |
||
403 | * Check if Sandbox is enabled |
||
404 | * |
||
405 | * @return bool |
||
406 | * @access public |
||
407 | */ |
||
408 | public function use_sandbox() |
||
412 | |||
413 | /** |
||
414 | * Check if IPN is enabled |
||
415 | * |
||
416 | * @return bool |
||
417 | * @access public |
||
418 | */ |
||
419 | public function use_ipn() |
||
423 | |||
424 | /** |
||
425 | * Generate PayPal return URL |
||
426 | * |
||
427 | * @param string $arg |
||
428 | * |
||
429 | * @return string |
||
430 | * @access private |
||
431 | */ |
||
432 | private function generate_paypal_return_url($arg) |
||
436 | |||
437 | /** |
||
438 | * Generate PayPal return notify URL |
||
439 | * |
||
440 | * @return string |
||
441 | * @access private |
||
442 | */ |
||
443 | private function generate_paypal_notify_return_url() |
||
447 | |||
448 | /** |
||
449 | * Get PayPal URL |
||
450 | * Used in form and in IPN process |
||
451 | * |
||
452 | * @param bool $is_test_ipn |
||
453 | * |
||
454 | * @return string |
||
455 | * @access public |
||
456 | */ |
||
457 | public function get_paypal_url($is_test_ipn = false) |
||
461 | |||
462 | /** |
||
463 | * Assign statistics vars to the template |
||
464 | * |
||
465 | * @return null |
||
466 | * @access public |
||
467 | */ |
||
468 | public function display_stats() |
||
489 | |||
490 | /** |
||
491 | * Get default currency symbol |
||
492 | * |
||
493 | * @param int $id |
||
494 | * |
||
495 | * @return array |
||
496 | * @access public |
||
497 | */ |
||
498 | public function get_default_currency_data($id = 0) |
||
502 | |||
503 | /** |
||
504 | * Retrieve the language key for donation goal |
||
505 | * |
||
506 | * @param string $currency_symbol Currency symbol |
||
507 | * @param bool $on_left Symbol position |
||
508 | * |
||
509 | * @return string |
||
510 | * @access public |
||
511 | */ |
||
512 | public function get_ppde_goal_langkey($currency_symbol, $on_left = true) |
||
529 | |||
530 | /** |
||
531 | * Put the currency on the left or on the right of the amount |
||
532 | * |
||
533 | * @param int $value |
||
534 | * @param string $currency |
||
535 | * @param bool $on_left |
||
536 | * |
||
537 | * @return string |
||
538 | */ |
||
539 | private function get_amount($value, $currency, $on_left = true) |
||
543 | |||
544 | /** |
||
545 | * Retrieve the language key for donation raised |
||
546 | * |
||
547 | * @param string $currency_symbol Currency symbol |
||
548 | * @param bool $on_left Symbol position |
||
549 | * |
||
550 | * @return string |
||
551 | * @access public |
||
552 | */ |
||
553 | public function get_ppde_raised_langkey($currency_symbol, $on_left = true) |
||
566 | |||
567 | /** |
||
568 | * Retrieve the language key for donation used |
||
569 | * |
||
570 | * @param string $currency_symbol Currency symbol |
||
571 | * @param bool $on_left Symbol position |
||
572 | * |
||
573 | * @return string |
||
574 | * @access public |
||
575 | */ |
||
576 | public function get_ppde_used_langkey($currency_symbol, $on_left = true) |
||
593 | |||
594 | /** |
||
595 | * Generate statistics percent for display |
||
596 | * |
||
597 | * @return null |
||
598 | * @access private |
||
599 | */ |
||
600 | private function generate_stats_percent() |
||
612 | |||
613 | /** |
||
614 | * Assign statistics percent vars to template |
||
615 | * |
||
616 | * @param integer $multiplicand |
||
617 | * @param integer $dividend |
||
618 | * @param string $type |
||
619 | * |
||
620 | * @return null |
||
621 | * @access public |
||
622 | */ |
||
623 | private function assign_vars_stats_percent($multiplicand, $dividend, $type = '') |
||
630 | |||
631 | /** |
||
632 | * Send data to the template file |
||
633 | * |
||
634 | * @return \Symfony\Component\HttpFoundation\Response |
||
635 | * @access private |
||
636 | */ |
||
637 | private function send_data_to_template() |
||
650 | |||
651 | /** |
||
652 | * Check if cURL is available |
||
653 | * |
||
654 | * @return bool |
||
655 | * @access public |
||
656 | */ |
||
657 | public function check_curl() |
||
677 | |||
678 | /** |
||
679 | * Get extension metadata |
||
680 | * |
||
681 | * @return null |
||
682 | * @access protected |
||
683 | */ |
||
684 | protected function get_ext_meta() |
||
691 | |||
692 | /** |
||
693 | * Load metadata for this extension |
||
694 | * |
||
695 | * @return array |
||
696 | * @access public |
||
697 | */ |
||
698 | public function load_metadata() |
||
720 | |||
721 | /** |
||
722 | * Retrieve the extension name |
||
723 | * |
||
724 | * @return null |
||
725 | * @access protected |
||
726 | */ |
||
727 | protected function retrieve_ext_name() |
||
732 | |||
733 | /** |
||
734 | * Check if fsockopen is available |
||
735 | * |
||
736 | * @return bool |
||
737 | * @access public |
||
738 | */ |
||
739 | public function check_fsockopen() |
||
754 | } |
||
755 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.