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 wpshop_options 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 wpshop_options, and based on these observations, apply Extract Interface, too.
1 | <?php if ( !defined( 'ABSPATH' ) ) exit; |
||
35 | class wpshop_options { |
||
36 | |||
37 | /** |
||
38 | * Declare the different groups/subgroups for wpshop options. Execute a filter in order to accept ption from modules/addons |
||
39 | * |
||
40 | * @return array A list with all options groups and subgroup to create. All option fields are defined in each module/addons |
||
41 | */ |
||
42 | public static function declare_options_groups() { |
||
43 | $groups = array(); |
||
44 | |||
45 | $groups['wpshop_general_option'] = |
||
46 | array( 'label' => __('General', 'wpshop'), |
||
47 | 'subgroups' => array( |
||
48 | 'wpshop_general_config' => array('class' => 'wpshop_admin_box_options_general'), |
||
49 | 'wpshop_company_info' => array('class' => 'wpshop_admin_box_options_company'), |
||
50 | ), |
||
51 | ); |
||
52 | $groups['wpshop_catalog_option'] = |
||
53 | array( 'label' => __('Catalog', 'wpshop'), |
||
54 | 'subgroups' => array( |
||
55 | 'wpshop_catalog_product_option' => array('class' => ' wpshop_admin_box_options_product'), |
||
56 | 'wpshop_catalog_main_option' => array('class' => ' wpshop_admin_box_options_catalog'), |
||
57 | 'wpshop_catalog_categories_option' => array('class' => ' wpshop_admin_box_options_category'), |
||
58 | ), |
||
59 | ); |
||
60 | $groups['wpshop_pages_option'] = |
||
61 | array( 'label' => __('Pages', 'wpshop'), |
||
62 | 'subgroups' => array( |
||
63 | 'wpshop_pages_option' => array('class' => ' wpshop_admin_box_options_pages'), |
||
64 | ), |
||
65 | ); |
||
66 | $groups['wpshop_display_option'] = |
||
67 | array( 'label' => __('Display', 'wpshop'), |
||
68 | 'subgroups' => array( |
||
69 | 'wpshop_display_option' => array('class' => ' wpshop_admin_box_options_display'), |
||
70 | 'wpshop_customize_display_option' => array('class' => ' wpshop_admin_box_options_display'), |
||
71 | 'wpshop_admin_display_option' => array('class' => ' wpshop_admin_box_options_admin_display'), |
||
72 | ), |
||
73 | ); |
||
74 | $groups['wpshop_emails_option'] = |
||
75 | array( 'label' => __('Emails', 'wpshop'), |
||
76 | 'subgroups' => array( |
||
77 | 'wpshop_emails' => array('class' => ' wpshop_admin_box_options_email'), |
||
78 | 'wpshop_messages' => array('class' => ' wpshop_admin_box_options_message'), |
||
79 | ), |
||
80 | ); |
||
81 | |||
82 | $wpshop_shop_type = !empty( $_POST['wpshop_shop_type'] ) ? sanitize_text_field( $_POST['wpshop_shop_type'] ) : ''; |
||
83 | |||
84 | /** Some options are available only when sale mode is active */ |
||
85 | if((WPSHOP_DEFINED_SHOP_TYPE == 'sale') && !isset($wpshop_shop_type) || (isset($wpshop_shop_type) && ($wpshop_shop_type != 'presentation'))) : |
||
86 | $groups['wpshop_cart_option'] = |
||
87 | array( 'label' => __('Cart', 'wpshop'), |
||
88 | 'subgroups' => array( |
||
89 | 'wpshop_cart_info' => array('class' => ' wpshop_admin_box_options_cart'), |
||
90 | ), |
||
91 | ); |
||
92 | $groups['wpshop_payments_option'] = |
||
93 | array( 'label' => __('Payments', 'wpshop'), |
||
94 | 'subgroups' => array( |
||
95 | 'wpshop_paymentMethod' => array('class' => ' wpshop_admin_box_options_payment_method'), |
||
96 | 'wpshop_payment_partial_on_command' => array('class' => ' wpshop_admin_box_options_payment_partial'), |
||
97 | ), |
||
98 | ); |
||
99 | $groups['wpshop_shipping_option'] = |
||
100 | array( 'label' => __('Shipping', 'wpshop'), |
||
101 | 'subgroups' => array( |
||
102 | 'wpshop_shipping_rules' => array('class' => ' wpshop_admin_box_options_shipping_rules') |
||
103 | ), |
||
104 | ); |
||
105 | endif; |
||
106 | |||
107 | $groups['wpshop_addons_option'] = |
||
108 | array( 'label' => __('Addons', 'wpshop'), |
||
109 | 'subgroups' => array( |
||
110 | 'wpshop_addons_options' => array('class' => ' wpshop_admin_box_options_addons'), |
||
111 | ), |
||
112 | ); |
||
113 | |||
114 | $groups['wpshop_advanced_options'] = |
||
115 | array( 'label' => __('Advanced options', 'wpshop'), |
||
116 | 'subgroups' => array( |
||
117 | 'wpshop_extra_options' => array('class' => ' wpshop_admin_box_options_advanced'), |
||
118 | ), |
||
119 | ); |
||
120 | |||
121 | /** Allows modules and addons to add options to existing list */ |
||
122 | $groups = apply_filters('wpshop_options', $groups); |
||
123 | |||
124 | return $groups; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Display the main option page. Read all groups/subgroups and options fields defined in wpshop core and modules/addons |
||
129 | */ |
||
130 | public static function option_main_page() { |
||
164 | |||
165 | |||
166 | /** |
||
167 | * Declare the different options for the plugin |
||
168 | */ |
||
169 | public static function add_options(){ |
||
170 | global $wpshop_display_option; |
||
171 | |||
172 | |||
173 | /*Catalog - Main */ |
||
174 | register_setting('wpshop_options', 'wpshop_catalog_main_option', array('wpshop_options', 'wpshop_options_validate_catalog_main_option')); |
||
175 | add_settings_section('wpshop_catalog_main_section', '<span class="dashicons dashicons-category"></span>'.__('Catalog', 'wpshop'), array('wpshop_options', 'plugin_section_text'), 'wpshop_catalog_main_option'); |
||
176 | add_settings_field('wpshop_catalog_empty_price_behaviour', __('Empty price', 'wpshop'), array('wpshop_options', 'wpshop_catalog_empty_price_behaviour'), 'wpshop_catalog_main_option', 'wpshop_catalog_main_section'); |
||
177 | /* Catalog - Product */ |
||
178 | register_setting('wpshop_options', 'wpshop_catalog_product_option', array('wpshop_options', 'wpshop_options_validate_catalog_product_option')); |
||
179 | add_settings_section('wpshop_catalog_product_section', '<span class="dashicons dashicons-archive"></span>'.__('Products', 'wpshop'), array('wpshop_options', 'plugin_section_text'), 'wpshop_catalog_product_option'); |
||
180 | add_settings_field('wpshop_catalog_product_slug', __('Products common rewrite param', 'wpshop'), array('wpshop_options', 'wpshop_catalog_product_slug_field'), 'wpshop_catalog_product_option', 'wpshop_catalog_product_section'); |
||
181 | /* Catalog - Categories */ |
||
182 | register_setting('wpshop_options', 'wpshop_catalog_categories_option', array('wpshop_options', 'wpshop_options_validate_catalog_categories_option')); |
||
183 | add_settings_section('wpshop_catalog_categories_section', '<span class="dashicons dashicons-portfolio"></span>'.__('Categories', 'wpshop'), array('wpshop_options', 'plugin_section_text'), 'wpshop_catalog_categories_option'); |
||
184 | add_settings_field('wpshop_catalog_categories_slug', __('Categories common rewrite param', 'wpshop'), array('wpshop_options', 'wpshop_catalog_categories_slug_field'), 'wpshop_catalog_categories_option', 'wpshop_catalog_categories_section'); |
||
185 | add_settings_field('wpshop_catalog_no_category_slug', __('Default category slug for unassociated product', 'wpshop'), array('wpshop_options', 'wpshop_catalog_no_category_slug_field'), 'wpshop_catalog_categories_option', 'wpshop_catalog_categories_section'); |
||
186 | |||
187 | /* General option */ |
||
188 | wpshop_general_options::declare_options(); |
||
189 | |||
190 | /* Company */ |
||
191 | wpshop_company_options::declare_options(); |
||
192 | |||
193 | /* Payments */ |
||
194 | $wpshop_shop_type = !empty( $_POST['wpshop_shop_type'] ) ? sanitize_text_field( $_POST['wpshop_shop_type'] ) : ''; |
||
195 | $old_wpshop_shop_type = !empty( $_POST['old_wpshop_shop_type'] ) ? sanitize_text_field( $_POST['old_wpshop_shop_type'] ) : ''; |
||
196 | View Code Duplication | if((WPSHOP_DEFINED_SHOP_TYPE == 'sale') && !isset($wpshop_shop_type) || (isset($wpshop_shop_type) && ($wpshop_shop_type != 'presentation')) && !isset($old_wpshop_shop_type) || (isset($old_wpshop_shop_type) && ($old_wpshop_shop_type != 'presentation'))){ |
|
|
|||
197 | wpshop_payment_options::declare_options(); |
||
198 | } |
||
199 | |||
200 | /* Cart */ |
||
201 | if((WPSHOP_DEFINED_SHOP_TYPE == 'sale') && !isset($wpshop_shop_type) || (isset($wpshop_shop_type) && ($wpshop_shop_type != 'presentation')) && !isset($old_wpshop_shop_type) || (isset($old_wpshop_shop_type) && ($old_wpshop_shop_type != 'presentation'))){ |
||
202 | register_setting('wpshop_options', 'wpshop_cart_option', array('wpshop_options', 'wpshop_options_validate_cart')); |
||
203 | add_settings_section('wpshop_cart_info', '<span class="dashicons dashicons-cart"></span>'.__('Cart', 'wpshop'), array('wpshop_options', 'plugin_section_text'), 'wpshop_cart_info'); |
||
204 | add_settings_field('wpshop_cart_product_added_behaviour', __('Action when produt is added succesfully into cart', 'wpshop'), array('wpshop_options', 'wpshop_cart_product_added_behaviour_field'), 'wpshop_cart_info', 'wpshop_cart_info'); |
||
205 | add_settings_field('wpshop_cart_product_added_to_quotation_behaviour', __('Action when produt is added succesfully into a quotation', 'wpshop'), array('wpshop_options', 'wpshop_cart_product_added_to_quotation_behaviour_field'), 'wpshop_cart_info', 'wpshop_cart_info'); |
||
206 | add_settings_field('wpshop_cart_total_item_nb', __('Only a limited number of products in cart', 'wpshop'), array('wpshop_options', 'wpshop_cart_total_item_nb_field'), 'wpshop_cart_info', 'wpshop_cart_info'); |
||
207 | //add_settings_field('wpshop_cart_same_item_nb', __('Number of same item allowed into cart', 'wpshop'), array('wpshop_options', 'wpshop_cart_same_item_nb_field'), 'wpshop_cart_info', 'wpshop_cart_info'); |
||
208 | register_setting('wpshop_options', 'wpshop_catalog_product_option', array('wpshop_options', 'wpshop_catalog_product_variation_option_validate')); |
||
209 | add_settings_field('wpshop_catalog_product_option', __('Variation product display options for all products', 'wpshop'), array('wpshop_options', 'wpshop_catalog_varition_product_field'), 'wpshop_catalog_product_option', 'wpshop_catalog_product_section'); |
||
210 | do_action('wsphop_options'); |
||
211 | } |
||
212 | |||
213 | do_action('wsphop_options'); |
||
214 | |||
215 | /* Pages */ |
||
216 | wpshop_page_options::declare_options(); |
||
217 | |||
218 | /* Emails */ |
||
219 | wpshop_email_options::declare_options(); |
||
220 | |||
221 | /* Addons */ |
||
222 | $wpshop_addons_settings = new wpshop_addons_settings(); |
||
223 | $wpshop_addons_settings->declare_options(); |
||
224 | |||
225 | /* Advanced Settings */ |
||
226 | $wpshop_advanced_settings = new wpshop_advanced_settings(); |
||
227 | $wpshop_advanced_settings->declare_options(); |
||
228 | |||
229 | /* Shipping section */ |
||
230 | View Code Duplication | if((WPSHOP_DEFINED_SHOP_TYPE == 'sale') && !isset($wpshop_shop_type) || (isset($wpshop_shop_type) && ($wpshop_shop_type != 'presentation')) && !isset($old_wpshop_shop_type) || (isset($old_wpshop_shop_type) && ($old_wpshop_shop_type != 'presentation'))){ |
|
231 | $wpshop_shipping_options = new wpshop_shipping_options(); |
||
232 | $wpshop_shipping_options->declare_options(); |
||
233 | } |
||
234 | } |
||
235 | |||
236 | // Common section description |
||
237 | public static function plugin_section_text() { |
||
240 | |||
241 | /* ------------------------------ */ |
||
242 | /* --------- CATALOG INFO ------- */ |
||
243 | /* ------------------------------ */ |
||
244 | public static function wpshop_catalog_empty_price_behaviour() { |
||
269 | |||
270 | /* Processing */ |
||
271 | public static function wpshop_options_validate_catalog_product_option($input){ |
||
307 | |||
308 | public static function wpshop_catalog_varition_product_field () { |
||
316 | |||
317 | public static function wpshop_catalog_product_variation_option_validate ($input) { |
||
320 | |||
321 | /* ------------------------- */ |
||
322 | /* --------- CART ------- */ |
||
323 | /* ------------------------- */ |
||
324 | public static function wpshop_cart_total_item_nb_field() { |
||
376 | |||
377 | |||
378 | public static function wpshop_cart_product_added_to_quotation_behaviour_field() { |
||
407 | |||
408 | |||
409 | } |
||
410 | |||
412 |
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.