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 FrmEntryMeta 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 FrmEntryMeta, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmEntryMeta { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @param string $meta_key |
||
| 10 | */ |
||
| 11 | public static function add_entry_meta( $entry_id, $field_id, $meta_key = null, $meta_value ) { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $meta_key |
||
| 42 | */ |
||
| 43 | public static function update_entry_meta( $entry_id, $field_id, $meta_key = null, $meta_value ) { |
||
| 63 | |||
| 64 | public static function update_entry_metas( $entry_id, $values ) { |
||
| 65 | global $wpdb; |
||
| 66 | |||
| 67 | $prev_values = FrmDb::get_col( $wpdb->prefix . 'frm_item_metas', array( 'item_id' => $entry_id, 'field_id !' => 0 ), 'field_id' ); |
||
| 68 | |||
| 69 | foreach ( $values as $field_id => $meta_value ) { |
||
| 70 | $field = false; |
||
| 71 | if ( ! empty( $field_id ) ) { |
||
| 72 | $field = FrmField::getOne( $field_id ); |
||
| 73 | } |
||
| 74 | |||
| 75 | // set the value for the file upload field and add new tags (in Pro version) |
||
| 76 | $meta_value = apply_filters( 'frm_prepare_data_before_db', $meta_value, $field_id, $entry_id, compact( 'field' ) ); |
||
| 77 | |||
| 78 | if ( $prev_values && in_array($field_id, $prev_values) ) { |
||
| 79 | |||
| 80 | if ( ( is_array( $meta_value ) && empty( $meta_value ) ) || ( ! is_array( $meta_value ) && trim( $meta_value ) == '' ) ) { |
||
| 81 | // remove blank fields |
||
| 82 | unset( $values[ $field_id ] ); |
||
| 83 | } else { |
||
| 84 | // if value exists, then update it |
||
| 85 | self::update_entry_meta( $entry_id, $field_id, '', $meta_value ); |
||
| 86 | } |
||
| 87 | } else { |
||
| 88 | // if value does not exist, then create it |
||
| 89 | self::add_entry_meta( $entry_id, $field_id, '', $meta_value ); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | if ( empty($prev_values) ) { |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | |||
| 97 | $prev_values = array_diff($prev_values, array_keys($values)); |
||
| 98 | |||
| 99 | if ( empty($prev_values) ) { |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | // prepare the query |
||
| 104 | $where = array( 'item_id' => $entry_id, 'field_id' => $prev_values ); |
||
| 105 | FrmDb::get_where_clause_and_values( $where ); |
||
| 106 | |||
| 107 | // Delete any leftovers |
||
| 108 | $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); |
||
| 109 | self::clear_cache(); |
||
| 110 | } |
||
| 111 | |||
| 112 | public static function duplicate_entry_metas( $old_id, $new_id ) { |
||
| 113 | $metas = self::get_entry_meta_info($old_id); |
||
| 114 | foreach ( $metas as $meta ) { |
||
| 115 | self::add_entry_meta($new_id, $meta->field_id, null, $meta->meta_value); |
||
| 116 | unset($meta); |
||
| 117 | } |
||
| 118 | self::clear_cache(); |
||
| 119 | } |
||
| 120 | |||
| 121 | public static function delete_entry_meta( $entry_id, $field_id ) { |
||
| 122 | global $wpdb; |
||
| 123 | self::clear_cache(); |
||
| 124 | return $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}frm_item_metas WHERE field_id=%d AND item_id=%d", $field_id, $entry_id)); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Clear entry meta caching |
||
| 129 | * Called when a meta is added or changed |
||
| 130 | * |
||
| 131 | * @since 2.0.5 |
||
| 132 | */ |
||
| 133 | public static function clear_cache() { |
||
| 134 | FrmAppHelper::cache_delete_group( 'frm_entry_meta' ); |
||
| 135 | FrmAppHelper::cache_delete_group( 'frm_item_meta' ); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @since 2.0.9 |
||
| 140 | */ |
||
| 141 | public static function get_meta_value( $entry, $field_id ) { |
||
| 142 | if ( isset( $entry->metas ) ) { |
||
| 143 | return isset( $entry->metas[ $field_id ] ) ? $entry->metas[ $field_id ] : false; |
||
| 144 | } else { |
||
| 145 | return self::get_entry_meta_by_field( $entry->id, $field_id ); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | public static function get_entry_meta_by_field( $entry_id, $field_id ) { |
||
| 150 | global $wpdb; |
||
| 151 | |||
| 152 | if ( is_object( $entry_id ) ) { |
||
| 153 | $entry = $entry_id; |
||
| 154 | $entry_id = $entry->id; |
||
| 155 | $cached = $entry; |
||
| 156 | } else { |
||
| 157 | $entry_id = (int) $entry_id; |
||
| 158 | $cached = FrmAppHelper::check_cache( $entry_id, 'frm_entry' ); |
||
| 159 | } |
||
| 160 | |||
| 161 | if ( $cached && isset( $cached->metas ) && isset( $cached->metas[ $field_id ] ) ) { |
||
| 162 | $result = $cached->metas[ $field_id ]; |
||
| 163 | return stripslashes_deep($result); |
||
| 164 | } |
||
| 165 | |||
| 166 | $get_table = $wpdb->prefix . 'frm_item_metas'; |
||
| 167 | $query = array( 'item_id' => $entry_id ); |
||
| 168 | View Code Duplication | if ( is_numeric($field_id) ) { |
|
| 169 | $query['field_id'] = $field_id; |
||
| 170 | } else { |
||
| 171 | $get_table .= ' it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; |
||
| 172 | $query['fi.field_key'] = $field_id; |
||
| 173 | } |
||
| 174 | |||
| 175 | $result = FrmDb::get_var( $get_table, $query, 'meta_value' ); |
||
| 176 | $result = maybe_unserialize($result); |
||
| 177 | $result = stripslashes_deep($result); |
||
| 178 | |||
| 179 | return $result; |
||
| 180 | } |
||
| 181 | |||
| 182 | public static function get_entry_metas( $entry_id ) { |
||
| 188 | |||
| 189 | public static function get_entry_metas_for_field( $field_id, $order = '', $limit = '', $args = array() ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $order |
||
| 214 | * @param string $limit |
||
| 215 | */ |
||
| 216 | private static function meta_field_query( $field_id, $order, $limit, $args, array &$query ) { |
||
| 241 | |||
| 242 | public static function get_entry_meta_info( $entry_id ) { |
||
| 245 | |||
| 246 | public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) { |
||
| 267 | |||
| 268 | public static function getEntryIds( $where = array(), $order_by = '', $limit = '', $unique = true, $args = array() ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string|array $where |
||
| 288 | * @param string $order_by |
||
| 289 | * @param string $limit |
||
| 290 | */ |
||
| 291 | private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { |
||
| 349 | |||
| 350 | public static function search_entry_metas( $search, $field_id = '', $operator ) { |
||
| 391 | } |
||
| 392 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.