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() |
||
226 | |||
227 | /** |
||
228 | * @return bool |
||
229 | * @access public |
||
230 | */ |
||
231 | public function can_use_ppde() |
||
235 | |||
236 | /** |
||
237 | * @return bool |
||
238 | * @access public |
||
239 | */ |
||
240 | public function can_view_ppde_donorlist() |
||
244 | |||
245 | /** |
||
246 | * @param string $set_return_args_url |
||
247 | * |
||
248 | * @return null |
||
249 | * @access private |
||
250 | */ |
||
251 | private function set_return_args_url($set_return_args_url) |
||
273 | |||
274 | /** |
||
275 | * Get content of current donation pages |
||
276 | * |
||
277 | * @param string $return_args_url |
||
278 | * |
||
279 | * @return array |
||
280 | * @access private |
||
281 | */ |
||
282 | private function get_donation_content_data($return_args_url) |
||
288 | |||
289 | /** |
||
290 | * Build pull down menu options of available currency |
||
291 | * |
||
292 | * @param int $config_value Currency identifier; default: 0 |
||
293 | * |
||
294 | * @return null |
||
295 | * @access public |
||
296 | */ |
||
297 | public function build_currency_select_menu($config_value = 0) |
||
317 | |||
318 | /** |
||
319 | * Build pull down menu options of available currency value |
||
320 | * |
||
321 | * @return string List of currency value set in ACP for dropdown menu |
||
322 | * @access private |
||
323 | */ |
||
324 | private function build_currency_value_select_menu() |
||
342 | |||
343 | /** |
||
344 | * Get dropbox config value |
||
345 | * |
||
346 | * @return bool |
||
347 | * @access private |
||
348 | */ |
||
349 | private function get_dropbox_status() |
||
353 | |||
354 | /** |
||
355 | * Force dropbox value to integer |
||
356 | * |
||
357 | * @param int $value |
||
358 | * |
||
359 | * @return int |
||
360 | */ |
||
361 | private function settype_dropbox_int_value($value = 0) |
||
370 | |||
371 | /** |
||
372 | * Build PayPal hidden fields |
||
373 | * |
||
374 | * @return string PayPal hidden field needed to fill PayPal forms |
||
375 | * @access private |
||
376 | */ |
||
377 | private function paypal_hidden_fields() |
||
393 | |||
394 | /** |
||
395 | * Get PayPal account id |
||
396 | * |
||
397 | * @return string $this Paypal account Identifier |
||
398 | * @access private |
||
399 | */ |
||
400 | private function get_account_id() |
||
404 | |||
405 | /** |
||
406 | * Check if Sandbox is enabled |
||
407 | * |
||
408 | * @return bool |
||
409 | * @access public |
||
410 | */ |
||
411 | public function use_sandbox() |
||
415 | |||
416 | /** |
||
417 | * Check if IPN is enabled |
||
418 | * |
||
419 | * @return bool |
||
420 | * @access public |
||
421 | */ |
||
422 | public function use_ipn() |
||
426 | |||
427 | /** |
||
428 | * Generate PayPal return URL |
||
429 | * |
||
430 | * @param string $arg |
||
431 | * |
||
432 | * @return string |
||
433 | * @access private |
||
434 | */ |
||
435 | private function generate_paypal_return_url($arg) |
||
439 | |||
440 | /** |
||
441 | * Generate PayPal return notify URL |
||
442 | * |
||
443 | * @return string |
||
444 | * @access private |
||
445 | */ |
||
446 | private function generate_paypal_notify_return_url() |
||
450 | |||
451 | /** |
||
452 | * Get PayPal URL |
||
453 | * Used in form and in IPN process |
||
454 | * |
||
455 | * @param bool $is_test_ipn |
||
456 | * |
||
457 | * @return string |
||
458 | * @access public |
||
459 | */ |
||
460 | public function get_paypal_url($is_test_ipn = false) |
||
464 | |||
465 | /** |
||
466 | * Assign statistics vars to the template |
||
467 | * |
||
468 | * @return null |
||
469 | * @access public |
||
470 | */ |
||
471 | public function display_stats() |
||
492 | |||
493 | /** |
||
494 | * Get default currency symbol |
||
495 | * |
||
496 | * @param int $id |
||
497 | * |
||
498 | * @return array |
||
499 | * @access public |
||
500 | */ |
||
501 | public function get_default_currency_data($id = 0) |
||
505 | |||
506 | /** |
||
507 | * Retrieve the language key for donation goal |
||
508 | * |
||
509 | * @param string $currency_symbol Currency symbol |
||
510 | * @param bool $on_left Symbol position |
||
511 | * |
||
512 | * @return string |
||
513 | * @access public |
||
514 | */ |
||
515 | public function get_ppde_goal_langkey($currency_symbol, $on_left = true) |
||
532 | |||
533 | /** |
||
534 | * Put the currency on the left or on the right of the amount |
||
535 | * |
||
536 | * @param int $value |
||
537 | * @param string $currency |
||
538 | * @param bool $on_left |
||
539 | * |
||
540 | * @return string |
||
541 | */ |
||
542 | private function get_amount($value, $currency, $on_left = true) |
||
546 | |||
547 | /** |
||
548 | * Retrieve the language key for donation raised |
||
549 | * |
||
550 | * @param string $currency_symbol Currency symbol |
||
551 | * @param bool $on_left Symbol position |
||
552 | * |
||
553 | * @return string |
||
554 | * @access public |
||
555 | */ |
||
556 | public function get_ppde_raised_langkey($currency_symbol, $on_left = true) |
||
569 | |||
570 | /** |
||
571 | * Retrieve the language key for donation used |
||
572 | * |
||
573 | * @param string $currency_symbol Currency symbol |
||
574 | * @param bool $on_left Symbol position |
||
575 | * |
||
576 | * @return string |
||
577 | * @access public |
||
578 | */ |
||
579 | public function get_ppde_used_langkey($currency_symbol, $on_left = true) |
||
596 | |||
597 | /** |
||
598 | * Generate statistics percent for display |
||
599 | * |
||
600 | * @return null |
||
601 | * @access private |
||
602 | */ |
||
603 | private function generate_stats_percent() |
||
615 | |||
616 | /** |
||
617 | * Assign statistics percent vars to template |
||
618 | * |
||
619 | * @param integer $multiplicand |
||
620 | * @param integer $dividend |
||
621 | * @param string $type |
||
622 | * |
||
623 | * @return null |
||
624 | * @access public |
||
625 | */ |
||
626 | private function assign_vars_stats_percent($multiplicand, $dividend, $type = '') |
||
633 | |||
634 | /** |
||
635 | * Send data to the template file |
||
636 | * |
||
637 | * @return \Symfony\Component\HttpFoundation\Response |
||
638 | * @access private |
||
639 | */ |
||
640 | private function send_data_to_template() |
||
653 | |||
654 | /** |
||
655 | * Check if cURL is available |
||
656 | * |
||
657 | * @return bool |
||
658 | * @access public |
||
659 | */ |
||
660 | public function check_curl() |
||
680 | |||
681 | /** |
||
682 | * Get extension metadata |
||
683 | * |
||
684 | * @return null |
||
685 | * @access protected |
||
686 | */ |
||
687 | protected function get_ext_meta() |
||
694 | |||
695 | /** |
||
696 | * Load metadata for this extension |
||
697 | * |
||
698 | * @return array |
||
699 | * @access public |
||
700 | */ |
||
701 | public function load_metadata() |
||
723 | |||
724 | /** |
||
725 | * Retrieve the extension name |
||
726 | * |
||
727 | * @return null |
||
728 | * @access protected |
||
729 | */ |
||
730 | protected function retrieve_ext_name() |
||
735 | |||
736 | /** |
||
737 | * Check if fsockopen is available |
||
738 | * |
||
739 | * @return bool |
||
740 | * @access public |
||
741 | */ |
||
742 | public function check_fsockopen() |
||
757 | } |
||
758 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.