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:
| 1 | <?php |
||
| 10 | abstract class Meta_Datastore extends Datastore { |
||
| 11 | /** |
||
| 12 | * Initialization tasks. |
||
| 13 | **/ |
||
| 14 | public function init() {} |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Save the field value(s) into the database. |
||
| 18 | * |
||
| 19 | * @param Field $field The field to save. |
||
| 20 | */ |
||
| 21 | public function save( Field $field ) { |
||
| 22 | if ( ! update_metadata( $this->get_meta_type(), $this->get_id(), $this->get_field_name( $field ), $field->get_value() ) ) { |
||
| 23 | add_metadata( $this->get_meta_type(), $this->get_id(), $this->get_field_name( $field ), $field->get_value(), true ); |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Load the field value(s) from the database. |
||
| 29 | * |
||
| 30 | * @param Field $field The field to retrieve value for. |
||
| 31 | */ |
||
| 32 | public function load( Field $field ) { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Delete the field value(s) from the database. |
||
| 53 | * |
||
| 54 | * @param Field $field The field to delete. |
||
| 55 | */ |
||
| 56 | public function delete( Field $field ) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Load complex field value(s) from the database. |
||
| 62 | * |
||
| 63 | * @param mixed $field The field to load values for. |
||
| 64 | */ |
||
| 65 | public function load_values( $field ) { |
||
| 66 | global $wpdb; |
||
| 67 | |||
| 68 | View Code Duplication | if ( is_object( $field ) && is_subclass_of( $field, 'Carbon_Fields\\Field\\Field' ) ) { |
|
| 69 | $meta_key = $this->get_field_name( $field ); |
||
| 70 | } else { |
||
| 71 | $meta_key = $field; |
||
| 72 | } |
||
| 73 | |||
| 74 | return $wpdb->get_results( ' |
||
| 75 | SELECT meta_key AS field_key, meta_value AS field_value FROM ' . $this->get_table_name() . ' |
||
| 76 | WHERE `meta_key` LIKE "' . addslashes( $meta_key ) . '_%" AND `' . $this->get_table_field_name() . '`="' . intval( $this->get_id() ) . '" |
||
| 77 | ', ARRAY_A ); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Delete complex field value(s) from the database. |
||
| 82 | * |
||
| 83 | * @param mixed $field The field to delete values for. |
||
| 84 | */ |
||
| 85 | public function delete_values( $field ) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Retrieve field name |
||
| 101 | */ |
||
| 102 | public function get_field_name( $field ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Retrieve the type of meta data. |
||
| 110 | */ |
||
| 111 | abstract public function get_meta_type(); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Retrieve the meta table name to query. |
||
| 115 | */ |
||
| 116 | abstract public function get_table_name(); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Retrieve the meta table field name to query by. |
||
| 120 | */ |
||
| 121 | abstract public function get_table_field_name(); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Set the ID of the datastore. |
||
| 125 | */ |
||
| 126 | abstract public function set_id( $id ); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Retrieve the ID of the datastore. |
||
| 130 | */ |
||
| 131 | abstract public function get_id(); |
||
| 132 | } |
||
| 133 |