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 |
||
| 24 | class Give_DB_Log_Meta extends Give_DB { |
||
| 25 | /** |
||
| 26 | * Flag to handle result type |
||
| 27 | * |
||
| 28 | * @since 2.0 |
||
| 29 | * @access private |
||
| 30 | */ |
||
| 31 | private $raw_result = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Give_DB_Log_Meta constructor. |
||
| 35 | * |
||
| 36 | * @access public |
||
| 37 | * @since 2.0 |
||
| 38 | */ |
||
| 39 | public function __construct() { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get table columns and data types. |
||
| 57 | * |
||
| 58 | * @access public |
||
| 59 | * @since 2.0 |
||
| 60 | * |
||
| 61 | * @return array Columns and formats. |
||
| 62 | */ |
||
| 63 | public function get_columns() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Retrieve log meta field for a log. |
||
| 74 | * |
||
| 75 | * @access private |
||
| 76 | * @since 2.0 |
||
| 77 | * |
||
| 78 | * @param int $log_id Log ID. |
||
| 79 | * @param string $meta_key The meta key to retrieve. |
||
| 80 | * @param bool $single Whether to return a single value. |
||
| 81 | * |
||
| 82 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
||
| 83 | */ |
||
| 84 | public function get_meta( $log_id, $meta_key, $single ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add meta data field to a log. |
||
| 109 | * |
||
| 110 | * @access private |
||
| 111 | * @since 2.0 |
||
| 112 | * |
||
| 113 | * @param int $log_id Log ID. |
||
| 114 | * @param string $meta_key Metadata name. |
||
| 115 | * @param mixed $meta_value Metadata value. |
||
| 116 | * @param bool $unique Optional, default is false. Whether the same key should not be added. |
||
| 117 | * |
||
| 118 | * @return bool False for failure. True for success. |
||
| 119 | */ |
||
| 120 | View Code Duplication | public function add_meta( $log_id = 0, $meta_key = '', $meta_value, $unique = false ) { |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Update log meta field based on Log ID. |
||
| 131 | * |
||
| 132 | * @access private |
||
| 133 | * @since 2.0 |
||
| 134 | * |
||
| 135 | * @param int $log_id Log ID. |
||
| 136 | * @param string $meta_key Metadata key. |
||
| 137 | * @param mixed $meta_value Metadata value. |
||
| 138 | * @param mixed $prev_value Optional. Previous value to check before removing. |
||
| 139 | * |
||
| 140 | * @return bool False on failure, true if success. |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function update_meta( $log_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Remove metadata matching criteria from a log. |
||
| 153 | * |
||
| 154 | * For internal use only. Use Give_Log->delete_meta() for public usage. |
||
| 155 | * |
||
| 156 | * You can match based on the key, or key and value. Removing based on key and |
||
| 157 | * value, will keep from removing duplicate metadata with the same key. It also |
||
| 158 | * allows removing all metadata matching key, if needed. |
||
| 159 | * |
||
| 160 | * @access private |
||
| 161 | * @since 2.0 |
||
| 162 | * |
||
| 163 | * @param int $log_id Log ID. |
||
| 164 | * @param string $meta_key Metadata name. |
||
| 165 | * @param mixed $meta_value Optional. Metadata value. |
||
| 166 | * |
||
| 167 | * @return bool False for failure. True for success. |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function delete_meta( $log_id = 0, $meta_key = '', $meta_value = '' ) { |
|
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Delete all log meta |
||
| 182 | * |
||
| 183 | * @since 2.0 |
||
| 184 | * @access public |
||
| 185 | * |
||
| 186 | * @param int $log_id |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | View Code Duplication | public function delete_row( $log_id = 0 ) { |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Create the table |
||
| 210 | * |
||
| 211 | * @access public |
||
| 212 | * @since 2.0 |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | public function create_table() { |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * Add support for hidden functions. |
||
| 239 | * |
||
| 240 | * @since 2.0 |
||
| 241 | * @access public |
||
| 242 | * |
||
| 243 | * @param $name |
||
| 244 | * @param $arguments |
||
| 245 | * |
||
| 246 | * @return mixed |
||
| 247 | */ |
||
| 248 | public function __call( $name, $arguments ) { |
||
| 285 | } |
||
| 286 |