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 WC_Settings_API 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 WC_Settings_API, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | abstract class WC_Settings_API { |
||
14 | |||
15 | /** |
||
16 | * The plugin ID. Used for option names. |
||
17 | * @var string |
||
18 | */ |
||
19 | public $plugin_id = 'woocommerce_'; |
||
20 | |||
21 | /** |
||
22 | * ID of the class extending the settings API. Used in option names. |
||
23 | * @var string |
||
24 | */ |
||
25 | public $id = ''; |
||
26 | |||
27 | /** |
||
28 | * Validation errors. |
||
29 | * @var array of strings |
||
30 | */ |
||
31 | public $errors = array(); |
||
32 | |||
33 | /** |
||
34 | * Setting values. |
||
35 | * @var array |
||
36 | */ |
||
37 | public $settings = array(); |
||
38 | |||
39 | /** |
||
40 | * Form option fields. |
||
41 | * @var array |
||
42 | */ |
||
43 | public $form_fields = array(); |
||
44 | |||
45 | /** |
||
46 | * The posted settings data. When empty, $_POST data will be used. |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $data = array(); |
||
50 | |||
51 | /** |
||
52 | * Get the form fields after they are initialized. |
||
53 | * @return array of options |
||
54 | */ |
||
55 | public function get_form_fields() { |
||
58 | |||
59 | /** |
||
60 | * Set default required properties for each field. |
||
61 | * @param array |
||
62 | */ |
||
63 | private function set_defaults( $field ) { |
||
69 | |||
70 | /** |
||
71 | * Output the admin options table. |
||
72 | */ |
||
73 | public function admin_options() { |
||
76 | |||
77 | /** |
||
78 | * Return the name of the option in the WP DB. |
||
79 | * @since 2.6.0 |
||
80 | * @return string |
||
81 | */ |
||
82 | public function get_option_key() { |
||
85 | |||
86 | /** |
||
87 | * Get a fields type. Defaults to "text" if not set. |
||
88 | * @param array $field |
||
89 | * @return string |
||
90 | */ |
||
91 | public function get_field_type( $field ) { |
||
94 | |||
95 | /** |
||
96 | * Get a fields default value. Defaults to "" if not set. |
||
97 | * @param array $field |
||
98 | * @return string |
||
99 | */ |
||
100 | public function get_field_default( $field ) { |
||
103 | |||
104 | /** |
||
105 | * Get a field's posted and validated value. |
||
106 | * @param string $key |
||
107 | * @param array $field |
||
108 | * @param array $post_data |
||
109 | * @return string |
||
110 | */ |
||
111 | public function get_field_value( $key, $field, $post_data = array() ) { |
||
130 | |||
131 | /** |
||
132 | * Sets the POSTed data. This method can be used to set specific data, instead |
||
133 | * of taking it from the $_POST array. |
||
134 | * @param array data |
||
135 | */ |
||
136 | public function set_post_data( $data = array() ) { |
||
139 | |||
140 | /** |
||
141 | * Returns the POSTed data, to be used to save the settings. |
||
142 | * @return array |
||
143 | */ |
||
144 | public function get_post_data() { |
||
150 | |||
151 | /** |
||
152 | * Processes and saves options. |
||
153 | * If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out. |
||
154 | * @return bool was anything saved? |
||
155 | */ |
||
156 | public function process_admin_options() { |
||
173 | |||
174 | /** |
||
175 | * Add an error message for display in admin on save. |
||
176 | * @param string $error |
||
177 | */ |
||
178 | public function add_error( $error ) { |
||
181 | |||
182 | /** |
||
183 | * Get admin error messages. |
||
184 | */ |
||
185 | public function get_errors() { |
||
188 | |||
189 | /** |
||
190 | * Display admin error messages. |
||
191 | */ |
||
192 | public function display_errors() { |
||
201 | |||
202 | /** |
||
203 | * Initialise Settings. |
||
204 | * |
||
205 | * Store all settings in a single database entry |
||
206 | * and make sure the $settings array is either the default |
||
207 | * or the settings stored in the database. |
||
208 | * |
||
209 | * @since 1.0.0 |
||
210 | * @uses get_option(), add_option() |
||
211 | */ |
||
212 | View Code Duplication | public function init_settings() { |
|
221 | |||
222 | /** |
||
223 | * get_option function. |
||
224 | * |
||
225 | * Gets an option from the settings API, using defaults if necessary to prevent undefined notices. |
||
226 | * |
||
227 | * @param string $key |
||
228 | * @param mixed $empty_value |
||
229 | * @return string The value specified for the option or a default value for the option. |
||
230 | */ |
||
231 | View Code Duplication | public function get_option( $key, $empty_value = null ) { |
|
248 | |||
249 | /** |
||
250 | * Prefix key for settings. |
||
251 | * |
||
252 | * @param mixed $key |
||
253 | * @return string |
||
254 | */ |
||
255 | public function get_field_key( $key ) { |
||
258 | |||
259 | /** |
||
260 | * Generate Settings HTML. |
||
261 | * |
||
262 | * Generate the HTML for the fields on the "settings" screen. |
||
263 | * |
||
264 | * @param array $form_fields (default: array()) |
||
265 | * @since 1.0.0 |
||
266 | * @uses method_exists() |
||
267 | * @return string the html for the settings |
||
268 | */ |
||
269 | public function generate_settings_html( $form_fields = array(), $echo = true ) { |
||
291 | |||
292 | /** |
||
293 | * Get HTML for tooltips. |
||
294 | * |
||
295 | * @param array $data |
||
296 | * @return string |
||
297 | */ |
||
298 | public function get_tooltip_html( $data ) { |
||
309 | |||
310 | /** |
||
311 | * Get HTML for descriptions. |
||
312 | * |
||
313 | * @param array $data |
||
314 | * @return string |
||
315 | */ |
||
316 | public function get_description_html( $data ) { |
||
329 | |||
330 | /** |
||
331 | * Get custom attributes. |
||
332 | * |
||
333 | * @param array $data |
||
334 | * @return string |
||
335 | */ |
||
336 | public function get_custom_attribute_html( $data ) { |
||
347 | |||
348 | /** |
||
349 | * Generate Text Input HTML. |
||
350 | * |
||
351 | * @param mixed $key |
||
352 | * @param mixed $data |
||
353 | * @since 1.0.0 |
||
354 | * @return string |
||
355 | */ |
||
356 | View Code Duplication | public function generate_text_html( $key, $data ) { |
|
391 | |||
392 | /** |
||
393 | * Generate Price Input HTML. |
||
394 | * |
||
395 | * @param mixed $key |
||
396 | * @param mixed $data |
||
397 | * @since 1.0.0 |
||
398 | * @return string |
||
399 | */ |
||
400 | View Code Duplication | public function generate_price_html( $key, $data ) { |
|
435 | |||
436 | /** |
||
437 | * Generate Decimal Input HTML. |
||
438 | * |
||
439 | * @param mixed $key |
||
440 | * @param mixed $data |
||
441 | * @since 1.0.0 |
||
442 | * @return string |
||
443 | */ |
||
444 | View Code Duplication | public function generate_decimal_html( $key, $data ) { |
|
479 | |||
480 | /** |
||
481 | * Generate Password Input HTML. |
||
482 | * |
||
483 | * @param mixed $key |
||
484 | * @param mixed $data |
||
485 | * @since 1.0.0 |
||
486 | * @return string |
||
487 | */ |
||
488 | public function generate_password_html( $key, $data ) { |
||
492 | |||
493 | /** |
||
494 | * Generate Color Picker Input HTML. |
||
495 | * |
||
496 | * @param mixed $key |
||
497 | * @param mixed $data |
||
498 | * @since 1.0.0 |
||
499 | * @return string |
||
500 | */ |
||
501 | public function generate_color_html( $key, $data ) { |
||
537 | |||
538 | /** |
||
539 | * Generate Textarea HTML. |
||
540 | * |
||
541 | * @param mixed $key |
||
542 | * @param mixed $data |
||
543 | * @since 1.0.0 |
||
544 | * @return string |
||
545 | */ |
||
546 | View Code Duplication | public function generate_textarea_html( $key, $data ) { |
|
581 | |||
582 | /** |
||
583 | * Generate Checkbox HTML. |
||
584 | * |
||
585 | * @param mixed $key |
||
586 | * @param mixed $data |
||
587 | * @since 1.0.0 |
||
588 | * @return string |
||
589 | */ |
||
590 | public function generate_checkbox_html( $key, $data ) { |
||
630 | |||
631 | /** |
||
632 | * Generate Select HTML. |
||
633 | * |
||
634 | * @param mixed $key |
||
635 | * @param mixed $data |
||
636 | * @since 1.0.0 |
||
637 | * @return string |
||
638 | */ |
||
639 | public function generate_select_html( $key, $data ) { |
||
679 | |||
680 | /** |
||
681 | * Generate Multiselect HTML. |
||
682 | * |
||
683 | * @param mixed $key |
||
684 | * @param mixed $data |
||
685 | * @since 1.0.0 |
||
686 | * @return string |
||
687 | */ |
||
688 | public function generate_multiselect_html( $key, $data ) { |
||
733 | |||
734 | /** |
||
735 | * Generate Title HTML. |
||
736 | * |
||
737 | * @param mixed $key |
||
738 | * @param mixed $data |
||
739 | * @since 1.0.0 |
||
740 | * @return string |
||
741 | */ |
||
742 | public function generate_title_html( $key, $data ) { |
||
763 | |||
764 | /** |
||
765 | * Validate Text Field. |
||
766 | * |
||
767 | * Make sure the data is escaped correctly, etc. |
||
768 | * |
||
769 | * @param string $key Field key |
||
770 | * @param string|null $value Posted Value |
||
771 | * @return string |
||
772 | */ |
||
773 | public function validate_text_field( $key, $value ) { |
||
777 | |||
778 | /** |
||
779 | * Validate Price Field. |
||
780 | * |
||
781 | * Make sure the data is escaped correctly, etc. |
||
782 | * |
||
783 | * @param string $key |
||
784 | * @param string|null $value Posted Value |
||
785 | * @return string |
||
786 | */ |
||
787 | public function validate_price_field( $key, $value ) { |
||
791 | |||
792 | /** |
||
793 | * Validate Decimal Field. |
||
794 | * |
||
795 | * Make sure the data is escaped correctly, etc. |
||
796 | * |
||
797 | * @param string $key |
||
798 | * @param string|null $value Posted Value |
||
799 | * @return string |
||
800 | */ |
||
801 | public function validate_decimal_field( $key, $value ) { |
||
805 | |||
806 | /** |
||
807 | * Validate Password Field. |
||
808 | * |
||
809 | * Make sure the data is escaped correctly, etc. |
||
810 | * |
||
811 | * @param string $key |
||
812 | * @param string|null $value Posted Value |
||
813 | * @return string |
||
814 | */ |
||
815 | public function validate_password_field( $key, $value ) { |
||
819 | |||
820 | /** |
||
821 | * Validate Textarea Field. |
||
822 | * |
||
823 | * @param string $key |
||
824 | * @param string|null $value Posted Value |
||
825 | * @return string |
||
826 | */ |
||
827 | public function validate_textarea_field( $key, $value ) { |
||
838 | |||
839 | /** |
||
840 | * Validate Checkbox Field. |
||
841 | * |
||
842 | * If not set, return "no", otherwise return "yes". |
||
843 | * |
||
844 | * @param string $key |
||
845 | * @param string|null $value Posted Value |
||
846 | * @return string |
||
847 | */ |
||
848 | public function validate_checkbox_field( $key, $value ) { |
||
851 | |||
852 | /** |
||
853 | * Validate Select Field. |
||
854 | * |
||
855 | * @param string $key |
||
856 | * @param string $value Posted Value |
||
857 | * @return string |
||
858 | */ |
||
859 | public function validate_select_field( $key, $value ) { |
||
863 | |||
864 | /** |
||
865 | * Validate Multiselect Field. |
||
866 | * |
||
867 | * @param string $key |
||
868 | * @param string $value Posted Value |
||
869 | * @return string |
||
870 | */ |
||
871 | public function validate_multiselect_field( $key, $value ) { |
||
874 | |||
875 | /** |
||
876 | * Validate the data on the "Settings" form. |
||
877 | * @deprecated 2.6.0 No longer used |
||
878 | */ |
||
879 | public function validate_settings_fields( $form_fields = array() ) { |
||
882 | |||
883 | /** |
||
884 | * Format settings if needed. |
||
885 | * @deprecated 2.6.0 Unused |
||
886 | * @param array $value |
||
887 | * @return array |
||
888 | */ |
||
889 | public function format_settings( $value ) { |
||
893 | } |
||
894 |
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.