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 payment 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 payment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class payment { |
||
14 | var $modules, $selected_module; |
||
15 | |||
16 | protected $lang; |
||
17 | |||
18 | function __construct($module = '') { |
||
19 | global $PHP_SELF; |
||
20 | |||
21 | $this->lang = Registry::get('Language'); |
||
22 | |||
23 | if (defined('MODULE_PAYMENT_INSTALLED') && tep_not_null(MODULE_PAYMENT_INSTALLED)) { |
||
24 | $this->modules = explode(';', MODULE_PAYMENT_INSTALLED); |
||
25 | |||
26 | $include_modules = array(); |
||
27 | |||
28 | if ( (tep_not_null($module)) && (in_array($module . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules) || in_array($module, $this->modules)) ) { |
||
29 | $this->selected_module = $module; |
||
30 | |||
31 | if (strpos($module, '\\') !== false) { |
||
32 | $class = Apps::getModuleClass($module, 'Payment'); |
||
33 | $include_modules[] = [ |
||
34 | 'class' => $module, |
||
35 | 'file' => $class |
||
36 | ]; |
||
37 | } else { |
||
38 | $include_modules[] = array('class' => $module, 'file' => $module . '.php'); |
||
39 | } |
||
40 | View Code Duplication | } else { |
|
|
|||
41 | foreach($this->modules as $value) { |
||
42 | if (strpos($value, '\\') !== false) { |
||
43 | $class = Apps::getModuleClass($value, 'Payment'); |
||
44 | $include_modules[] = [ |
||
45 | 'class' => $value, |
||
46 | 'file' => $class |
||
47 | ]; |
||
48 | } else { |
||
49 | $class = basename($value, '.php'); |
||
50 | $include_modules[] = array('class' => $class, 'file' => $value); |
||
51 | } |
||
52 | } |
||
53 | } |
||
54 | |||
55 | View Code Duplication | for ($i=0, $n=sizeof($include_modules); $i<$n; $i++) { |
|
56 | if (strpos($include_modules[$i]['class'], '\\') !== false) { |
||
57 | Registry::set('Payment_' . str_replace('\\', '_', $include_modules[$i]['class']), new $include_modules[$i]['file']); |
||
58 | } else { |
||
59 | $this->lang->loadDefinitions('modules/payment/' . pathinfo($include_modules[$i]['file'], PATHINFO_FILENAME)); |
||
60 | include('includes/modules/payment/' . $include_modules[$i]['file']); |
||
61 | |||
62 | $GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class']; |
||
63 | } |
||
64 | } |
||
65 | |||
66 | // if there is only one payment method, select it as default because in |
||
67 | // checkout_confirmation.php the $_SESSION['payment'] variable is being assigned the |
||
68 | // $_POST['payment'] value which will be empty (no radio button selection possible) |
||
69 | if ( (tep_count_payment_modules() == 1) && (!isset($_SESSION['payment']) || ($_SESSION['payment'] != $include_modules[0]['class'])) ) { |
||
70 | $_SESSION['payment'] = $include_modules[0]['class']; |
||
71 | } |
||
72 | |||
73 | if ( (tep_not_null($module)) && (in_array($module . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules) || in_array($module, $this->modules)) ) { |
||
74 | if (strpos($module, '\\') !== false) { |
||
75 | $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $module)); |
||
76 | |||
77 | if (isset($OSCOM_PM->form_action_url)) { |
||
78 | $this->form_action_url = $OSCOM_PM->form_action_url; |
||
79 | } |
||
80 | } elseif (isset($GLOBALS[$module]->form_action_url)) { |
||
81 | $this->form_action_url = $GLOBALS[$module]->form_action_url; |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | // class methods |
||
88 | /* The following method is needed in the checkout_confirmation.php page |
||
89 | due to a chicken and egg problem with the payment class and order class. |
||
90 | The payment modules needs the order destination data for the dynamic status |
||
91 | feature, and the order class needs the payment module title. |
||
92 | The following method is a work-around to implementing the method in all |
||
93 | payment modules available which would break the modules in the contributions |
||
94 | section. This should be looked into again post 2.2. |
||
95 | */ |
||
96 | function update_status() { |
||
117 | |||
118 | function javascript_validation() { |
||
169 | |||
170 | function checkout_initialization_method() { |
||
192 | |||
193 | function selection() { |
||
217 | |||
218 | View Code Duplication | function pre_confirmation_check() { |
|
233 | |||
234 | View Code Duplication | function confirmation() { |
|
249 | |||
250 | View Code Duplication | function process_button() { |
|
265 | |||
266 | View Code Duplication | function before_process() { |
|
281 | |||
282 | View Code Duplication | function after_process() { |
|
297 | |||
298 | View Code Duplication | function get_error() { |
|
313 | } |
||
314 | ?> |
||
315 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.