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 FrmField 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 FrmField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class FrmField { |
||
7 | static $use_cache = true; |
||
8 | static $transient_size = 200; |
||
9 | |||
10 | public static function field_selection() { |
||
11 | $fields = apply_filters('frm_available_fields', array( |
||
12 | 'text' => __( 'Single Line Text', 'formidable' ), |
||
13 | 'textarea' => __( 'Paragraph Text', 'formidable' ), |
||
14 | 'checkbox' => __( 'Checkboxes', 'formidable' ), |
||
15 | 'radio' => __( 'Radio Buttons', 'formidable' ), |
||
16 | 'select' => __( 'Dropdown', 'formidable' ), |
||
17 | 'email' => __( 'Email Address', 'formidable' ), |
||
18 | 'url' => __( 'Website/URL', 'formidable' ), |
||
19 | 'captcha' => __( 'reCAPTCHA', 'formidable' ), |
||
20 | )); |
||
21 | |||
22 | return $fields; |
||
23 | } |
||
24 | |||
25 | public static function pro_field_selection() { |
||
26 | return apply_filters('frm_pro_available_fields', array( |
||
27 | 'end_divider' => array( |
||
28 | 'name' => __( 'End Section', 'formidable' ), |
||
29 | 'switch_from' => 'divider', |
||
30 | ), |
||
31 | 'divider' => __( 'Section', 'formidable' ), |
||
32 | 'break' => __( 'Page Break', 'formidable' ), |
||
33 | 'file' => __( 'File Upload', 'formidable' ), |
||
34 | 'rte' => __( 'Rich Text', 'formidable' ), |
||
35 | 'number' => __( 'Number', 'formidable' ), |
||
36 | 'phone' => __( 'Phone Number', 'formidable' ), |
||
37 | 'date' => __( 'Date', 'formidable' ), |
||
38 | 'time' => __( 'Time', 'formidable' ), |
||
39 | 'image' => __( 'Image URL', 'formidable' ), |
||
40 | 'scale' => __( 'Scale', 'formidable' ), |
||
41 | 'data' => __( 'Dynamic Field', 'formidable' ), |
||
42 | 'lookup' => __( 'Lookup', 'formidable' ), |
||
43 | 'form' => __( 'Embed Form', 'formidable' ), |
||
44 | 'hidden' => __( 'Hidden Field', 'formidable' ), |
||
45 | 'user_id' => __( 'User ID (hidden)', 'formidable' ), |
||
46 | 'password' => __( 'Password', 'formidable' ), |
||
47 | 'html' => __( 'HTML', 'formidable' ), |
||
48 | 'tag' => __( 'Tags', 'formidable' ), |
||
49 | 'credit_card' => __( 'Credit Card', 'formidable' ), |
||
50 | 'address' => __( 'Address', 'formidable' ), |
||
51 | )); |
||
52 | } |
||
53 | |||
54 | public static function create( $values, $return = true ) { |
||
55 | global $wpdb, $frm_duplicate_ids; |
||
56 | |||
57 | $new_values = array(); |
||
58 | $key = isset($values['field_key']) ? $values['field_key'] : $values['name']; |
||
59 | $new_values['field_key'] = FrmAppHelper::get_unique_key( $key, $wpdb->prefix . 'frm_fields', 'field_key' ); |
||
60 | |||
61 | foreach ( array( 'name', 'description', 'type', 'default_value' ) as $col ) { |
||
62 | $new_values[ $col ] = $values[ $col ]; |
||
63 | } |
||
64 | |||
65 | $new_values['options'] = $values['options']; |
||
66 | |||
67 | $new_values['field_order'] = isset($values['field_order']) ? (int) $values['field_order'] : null; |
||
68 | $new_values['required'] = isset($values['required']) ? (int) $values['required'] : 0; |
||
69 | $new_values['form_id'] = isset($values['form_id']) ? (int) $values['form_id'] : null; |
||
70 | $new_values['field_options'] = $values['field_options']; |
||
71 | $new_values['created_at'] = current_time('mysql', 1); |
||
72 | |||
73 | if ( isset( $values['id'] ) ) { |
||
74 | $frm_duplicate_ids[ $values['field_key'] ] = $new_values['field_key']; |
||
75 | $new_values = apply_filters('frm_duplicated_field', $new_values); |
||
76 | } |
||
77 | |||
78 | foreach ( $new_values as $k => $v ) { |
||
79 | if ( is_array( $v ) ) { |
||
80 | $new_values[ $k ] = serialize( $v ); |
||
81 | } |
||
82 | unset( $k, $v ); |
||
83 | } |
||
84 | |||
85 | //if(isset($values['id']) and is_numeric($values['id'])) |
||
86 | // $new_values['id'] = $values['id']; |
||
87 | |||
88 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_fields', $new_values ); |
||
89 | $new_id = 0; |
||
90 | if ( $query_results ) { |
||
91 | self::delete_form_transient( $new_values['form_id'] ); |
||
92 | $new_id = $wpdb->insert_id; |
||
93 | } |
||
94 | |||
95 | if ( ! $return ) { |
||
96 | return; |
||
97 | } |
||
98 | |||
99 | if ( $query_results ) { |
||
100 | if ( isset( $values['id'] ) ) { |
||
101 | $frm_duplicate_ids[ $values['id'] ] = $new_id; |
||
102 | } |
||
103 | return $new_id; |
||
104 | } else { |
||
105 | return false; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | public static function duplicate( $old_form_id, $form_id, $copy_keys = false, $blog_id = false ) { |
||
110 | global $frm_duplicate_ids; |
||
111 | |||
112 | $where = array( array( 'or' => 1, 'fi.form_id' => $old_form_id, 'fr.parent_form_id' => $old_form_id ) ); |
||
113 | $fields = self::getAll( $where, 'field_order', '', $blog_id ); |
||
114 | |||
115 | foreach ( (array) $fields as $field ) { |
||
116 | $new_key = ($copy_keys) ? $field->field_key : ''; |
||
117 | if ( $copy_keys && substr($field->field_key, -1) == 2 ) { |
||
118 | $new_key = rtrim($new_key, 2); |
||
119 | } |
||
120 | |||
121 | $values = array(); |
||
122 | FrmFieldsHelper::fill_field( $values, $field, $form_id, $new_key ); |
||
123 | |||
124 | // If this is a repeating section, create new form |
||
125 | if ( self::is_repeating_field( $field ) ) { |
||
126 | // create the repeatable form |
||
127 | $new_repeat_form_id = apply_filters( 'frm_create_repeat_form', 0, array( 'parent_form_id' => $form_id, 'field_name' => $field->name ) ); |
||
128 | |||
129 | // Save old form_select |
||
130 | $old_repeat_form_id = $field->field_options['form_select']; |
||
131 | |||
132 | // Update form_select for repeating field |
||
133 | $values['field_options']['form_select'] = $new_repeat_form_id; |
||
134 | } |
||
135 | |||
136 | // If this is a field inside of a repeating section, associate it with the correct form |
||
137 | if ( $field->form_id != $old_form_id && isset( $old_repeat_form_id ) && isset( $new_repeat_form_id ) && $field->form_id == $old_repeat_form_id ) { |
||
138 | $values['form_id'] = $new_repeat_form_id; |
||
139 | } |
||
140 | |||
141 | $values = apply_filters('frm_duplicated_field', $values); |
||
142 | $new_id = self::create($values); |
||
143 | $frm_duplicate_ids[ $field->id ] = $new_id; |
||
144 | $frm_duplicate_ids[ $field->field_key ] = $new_id; |
||
145 | unset($field); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | public static function update( $id, $values ) { |
||
198 | |||
199 | /** |
||
200 | * Keep backslashes in the phone format option |
||
201 | * |
||
202 | * @since 2.0.8 |
||
203 | * @param $values array - pass by reference |
||
204 | */ |
||
205 | private static function preserve_phone_format_backslashes( &$values ) { |
||
210 | |||
211 | public static function destroy( $id ) { |
||
227 | |||
228 | public static function delete_form_transient( $form_id ) { |
||
249 | |||
250 | /** |
||
251 | * If $field is numeric, get the field object |
||
252 | */ |
||
253 | public static function maybe_get_field( &$field ) { |
||
258 | |||
259 | public static function getOne( $id ) { |
||
285 | |||
286 | /** |
||
287 | * Get the field type by key or id |
||
288 | * @param int|string The field id or key |
||
289 | * @param mixed $col The name of the column in the fields database table |
||
290 | */ |
||
291 | public static function &get_type( $id, $col = 'type' ) { |
||
301 | |||
302 | public static function get_all_types_in_form( $form_id, $type, $limit = '', $inc_sub = 'exclude' ) { |
||
342 | |||
343 | public static function get_all_for_form( $form_id, $limit = '', $inc_embed = 'exclude', $inc_repeat = 'include' ) { |
||
382 | |||
383 | /** |
||
384 | * If repeating fields should be included, adjust $where accordingly |
||
385 | * |
||
386 | * @param string $inc_repeat |
||
387 | * @param array $where - pass by reference |
||
388 | */ |
||
389 | private static function maybe_include_repeating_fields( $inc_repeat, &$where ) { |
||
396 | |||
397 | public static function include_sub_fields( &$results, $inc_embed, $type = 'all' ) { |
||
423 | |||
424 | public static function getAll( $where = array(), $order_by = '', $limit = '', $blog_id = false ) { |
||
477 | |||
478 | /** |
||
479 | * @since 2.0.8 |
||
480 | */ |
||
481 | private static function format_field_results( &$results ) { |
||
500 | |||
501 | /** |
||
502 | * Unserialize all the serialized field data |
||
503 | * @since 2.0 |
||
504 | */ |
||
505 | private static function prepare_options( &$results ) { |
||
511 | |||
512 | /** |
||
513 | * If a form has too many fields, thay won't all save into a single transient. |
||
514 | * We'll break them into groups of 200 |
||
515 | * @since 2.0.1 |
||
516 | */ |
||
517 | private static function get_fields_from_transients( $form_id, $args ) { |
||
522 | |||
523 | /** |
||
524 | * Called by get_fields_from_transients |
||
525 | * @since 2.0.1 |
||
526 | */ |
||
527 | private static function get_next_transient( &$fields, $base_name, $next = 0 ) { |
||
541 | |||
542 | /** |
||
543 | * Save the transients in chunks for large forms |
||
544 | * @since 2.0.1 |
||
545 | */ |
||
546 | private static function set_field_transient( &$fields, $form_id, $next = 0, $args = array() ) { |
||
565 | |||
566 | public static function getIds( $where = '', $order_by = '', $limit = '' ) { |
||
583 | |||
584 | public static function is_no_save_field( $type ) { |
||
587 | |||
588 | public static function no_save_fields() { |
||
591 | |||
592 | /** |
||
593 | * Check if this field can hold an array of values |
||
594 | * |
||
595 | * @since 2.0.9 |
||
596 | * |
||
597 | * @param array|object $field |
||
598 | * @return boolean |
||
599 | */ |
||
600 | public static function is_field_with_multiple_values( $field ) { |
||
627 | |||
628 | /** |
||
629 | * Check if this is a multiselect dropdown field |
||
630 | * |
||
631 | * @since 2.0.9 |
||
632 | * @return boolean |
||
633 | */ |
||
634 | public static function is_multiple_select( $field ) { |
||
641 | |||
642 | /** |
||
643 | * Check if a field is read only. Read only can be set in the field options, |
||
644 | * but disabled with the shortcode options |
||
645 | * |
||
646 | * @since 2.0.9 |
||
647 | */ |
||
648 | public static function is_read_only( $field ) { |
||
652 | |||
653 | /** |
||
654 | * @since 2.0.9 |
||
655 | */ |
||
656 | public static function is_required( $field ) { |
||
661 | |||
662 | /** |
||
663 | * @since 2.0.9 |
||
664 | */ |
||
665 | public static function is_option_true( $field, $option ) { |
||
672 | |||
673 | /** |
||
674 | * @since 2.0.9 |
||
675 | */ |
||
676 | public static function is_option_empty( $field, $option ) { |
||
683 | |||
684 | public static function is_option_true_in_array( $field, $option ) { |
||
687 | |||
688 | public static function is_option_true_in_object( $field, $option ) { |
||
691 | |||
692 | public static function is_option_empty_in_array( $field, $option ) { |
||
695 | |||
696 | public static function is_option_empty_in_object( $field, $option ) { |
||
699 | |||
700 | public static function is_option_value_in_object( $field, $option ) { |
||
703 | |||
704 | /** |
||
705 | * @since 2.0.18 |
||
706 | */ |
||
707 | public static function get_option( $field, $option ) { |
||
715 | |||
716 | public static function get_option_in_array( $field, $option ) { |
||
719 | |||
720 | public static function get_option_in_object( $field, $option ) { |
||
723 | |||
724 | /** |
||
725 | * @since 2.0.09 |
||
726 | */ |
||
727 | public static function is_repeating_field( $field ) { |
||
735 | |||
736 | /** |
||
737 | * @param string $key |
||
738 | * @return int field id |
||
739 | */ |
||
740 | public static function get_id_by_key( $key ) { |
||
744 | |||
745 | /** |
||
746 | * @param string $id |
||
747 | * @return string |
||
748 | */ |
||
749 | public static function get_key_by_id( $id ) { |
||
752 | } |
||
753 |