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() |
||
220 | |||
221 | /** |
||
222 | * @return bool |
||
223 | * @access public |
||
224 | */ |
||
225 | public function can_use_ppde() |
||
229 | |||
230 | /** |
||
231 | * @return bool |
||
232 | * @access public |
||
233 | */ |
||
234 | public function can_view_ppde_donorlist() |
||
238 | |||
239 | /** |
||
240 | * @param string $set_return_args_url |
||
241 | * |
||
242 | * @return null |
||
243 | * @access private |
||
244 | */ |
||
245 | private function set_return_args_url($set_return_args_url) |
||
267 | |||
268 | /** |
||
269 | * Get content of current donation pages |
||
270 | * |
||
271 | * @param string $return_args_url |
||
272 | * |
||
273 | * @return array |
||
274 | * @access private |
||
275 | */ |
||
276 | private function get_donation_content_data($return_args_url) |
||
282 | |||
283 | /** |
||
284 | * Build pull down menu options of available currency |
||
285 | * |
||
286 | * @param int $config_value Currency identifier; default: 0 |
||
287 | * |
||
288 | * @return null |
||
289 | * @access public |
||
290 | */ |
||
291 | public function build_currency_select_menu($config_value = 0) |
||
311 | |||
312 | /** |
||
313 | * Build pull down menu options of available currency value |
||
314 | * |
||
315 | * @return string List of currency value set in ACP for dropdown menu |
||
316 | * @access private |
||
317 | */ |
||
318 | private function build_currency_value_select_menu() |
||
336 | |||
337 | /** |
||
338 | * Get dropbox config value |
||
339 | * |
||
340 | * @return bool |
||
341 | * @access private |
||
342 | */ |
||
343 | private function get_dropbox_status() |
||
347 | |||
348 | /** |
||
349 | * Force dropbox value to integer |
||
350 | * |
||
351 | * @param int $value |
||
352 | * |
||
353 | * @return int |
||
354 | */ |
||
355 | private function settype_dropbox_int_value($value = 0) |
||
364 | |||
365 | /** |
||
366 | * Build PayPal hidden fields |
||
367 | * |
||
368 | * @return string PayPal hidden field needed to fill PayPal forms |
||
369 | * @access private |
||
370 | */ |
||
371 | private function paypal_hidden_fields() |
||
387 | |||
388 | /** |
||
389 | * Get PayPal account id |
||
390 | * |
||
391 | * @return string $this Paypal account Identifier |
||
392 | * @access private |
||
393 | */ |
||
394 | private function get_account_id() |
||
398 | |||
399 | /** |
||
400 | * Check if Sandbox is enabled |
||
401 | * |
||
402 | * @return bool |
||
403 | * @access public |
||
404 | */ |
||
405 | public function use_sandbox() |
||
409 | |||
410 | /** |
||
411 | * Check if IPN is enabled |
||
412 | * |
||
413 | * @return bool |
||
414 | * @access public |
||
415 | */ |
||
416 | public function use_ipn() |
||
420 | |||
421 | /** |
||
422 | * Generate PayPal return URL |
||
423 | * |
||
424 | * @param string $arg |
||
425 | * |
||
426 | * @return string |
||
427 | * @access private |
||
428 | */ |
||
429 | private function generate_paypal_return_url($arg) |
||
433 | |||
434 | /** |
||
435 | * Generate PayPal return notify URL |
||
436 | * |
||
437 | * @return string |
||
438 | * @access private |
||
439 | */ |
||
440 | private function generate_paypal_notify_return_url() |
||
444 | |||
445 | /** |
||
446 | * Get PayPal URL |
||
447 | * Used in form and in IPN process |
||
448 | * |
||
449 | * @param bool $is_test_ipn |
||
450 | * |
||
451 | * @return string |
||
452 | * @access public |
||
453 | */ |
||
454 | public function get_paypal_url($is_test_ipn = false) |
||
458 | |||
459 | /** |
||
460 | * Assign statistics vars to the template |
||
461 | * |
||
462 | * @return null |
||
463 | * @access public |
||
464 | */ |
||
465 | public function display_stats() |
||
486 | |||
487 | /** |
||
488 | * Get default currency symbol |
||
489 | * |
||
490 | * @param int $id |
||
491 | * |
||
492 | * @return array |
||
493 | * @access public |
||
494 | */ |
||
495 | public function get_default_currency_data($id = 0) |
||
499 | |||
500 | /** |
||
501 | * Retrieve the language key for donation goal |
||
502 | * |
||
503 | * @param string $currency_symbol Currency symbol |
||
504 | * @param bool $on_left Symbol position |
||
505 | * |
||
506 | * @return string |
||
507 | * @access public |
||
508 | */ |
||
509 | public function get_ppde_goal_langkey($currency_symbol, $on_left = true) |
||
526 | |||
527 | /** |
||
528 | * Put the currency on the left or on the right of the amount |
||
529 | * |
||
530 | * @param int $value |
||
531 | * @param string $currency |
||
532 | * @param bool $on_left |
||
533 | * |
||
534 | * @return string |
||
535 | */ |
||
536 | private function get_amount($value, $currency, $on_left = true) |
||
540 | |||
541 | /** |
||
542 | * Retrieve the language key for donation raised |
||
543 | * |
||
544 | * @param string $currency_symbol Currency symbol |
||
545 | * @param bool $on_left Symbol position |
||
546 | * |
||
547 | * @return string |
||
548 | * @access public |
||
549 | */ |
||
550 | public function get_ppde_raised_langkey($currency_symbol, $on_left = true) |
||
563 | |||
564 | /** |
||
565 | * Retrieve the language key for donation used |
||
566 | * |
||
567 | * @param string $currency_symbol Currency symbol |
||
568 | * @param bool $on_left Symbol position |
||
569 | * |
||
570 | * @return string |
||
571 | * @access public |
||
572 | */ |
||
573 | public function get_ppde_used_langkey($currency_symbol, $on_left = true) |
||
590 | |||
591 | /** |
||
592 | * Generate statistics percent for display |
||
593 | * |
||
594 | * @return null |
||
595 | * @access private |
||
596 | */ |
||
597 | private function generate_stats_percent() |
||
609 | |||
610 | /** |
||
611 | * Assign statistics percent vars to template |
||
612 | * |
||
613 | * @param integer $multiplicand |
||
614 | * @param integer $dividend |
||
615 | * @param string $type |
||
616 | * |
||
617 | * @return null |
||
618 | * @access public |
||
619 | */ |
||
620 | private function assign_vars_stats_percent($multiplicand, $dividend, $type = '') |
||
627 | |||
628 | /** |
||
629 | * Send data to the template file |
||
630 | * |
||
631 | * @return \Symfony\Component\HttpFoundation\Response |
||
632 | * @access private |
||
633 | */ |
||
634 | private function send_data_to_template() |
||
647 | |||
648 | /** |
||
649 | * Check if cURL is available |
||
650 | * |
||
651 | * @return bool |
||
652 | * @access public |
||
653 | */ |
||
654 | public function check_curl() |
||
674 | |||
675 | /** |
||
676 | * Get extension metadata |
||
677 | * |
||
678 | * @return null |
||
679 | * @access protected |
||
680 | */ |
||
681 | protected function get_ext_meta() |
||
688 | |||
689 | /** |
||
690 | * Load metadata for this extension |
||
691 | * |
||
692 | * @return array |
||
693 | * @access public |
||
694 | */ |
||
695 | public function load_metadata() |
||
717 | |||
718 | /** |
||
719 | * Retrieve the extension name |
||
720 | * |
||
721 | * @return null |
||
722 | * @access protected |
||
723 | */ |
||
724 | protected function retrieve_ext_name() |
||
729 | |||
730 | /** |
||
731 | * Check if fsockopen is available |
||
732 | * |
||
733 | * @return bool |
||
734 | * @access public |
||
735 | */ |
||
736 | public function check_fsockopen() |
||
751 | } |
||
752 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: