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_Donor_Meta extends Give_DB { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Give_DB_Donor_Meta constructor. |
||
| 28 | * |
||
| 29 | * @access public |
||
| 30 | * @since 1.6 |
||
| 31 | */ |
||
| 32 | public function __construct() { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Get table columns and data types. |
||
| 45 | * |
||
| 46 | * @access public |
||
| 47 | * @since 1.6 |
||
| 48 | * |
||
| 49 | * @return array Columns and formats. |
||
| 50 | */ |
||
| 51 | public function get_columns() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Retrieve donor meta field for a donor. |
||
| 62 | * |
||
| 63 | * For internal use only. Use Give_Donor->get_meta() for public usage. |
||
| 64 | * |
||
| 65 | * @access private |
||
| 66 | * @since 1.6 |
||
| 67 | * |
||
| 68 | * @param int $donor_id Donor ID. |
||
| 69 | * @param string $meta_key The meta key to retrieve. |
||
| 70 | * @param bool $single Whether to return a single value. |
||
| 71 | * |
||
| 72 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function get_meta( $donor_id = 0, $meta_key = '', $single = false ) { |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Add meta data field to a donor. |
||
| 85 | * |
||
| 86 | * For internal use only. Use Give_Donor->add_meta() for public usage. |
||
| 87 | * |
||
| 88 | * @access private |
||
| 89 | * @since 1.6 |
||
| 90 | * |
||
| 91 | * @param int $donor_id Donor ID. |
||
| 92 | * @param string $meta_key Metadata name. |
||
| 93 | * @param mixed $meta_value Metadata value. |
||
| 94 | * @param bool $unique Optional, default is false. Whether the same key should not be added. |
||
| 95 | * |
||
| 96 | * @return bool False for failure. True for success. |
||
| 97 | */ |
||
| 98 | View Code Duplication | public function add_meta( $donor_id = 0, $meta_key = '', $meta_value, $unique = false ) { |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Update donor meta field based on Donor ID. |
||
| 109 | * |
||
| 110 | * For internal use only. Use Give_Donor->update_meta() for public usage. |
||
| 111 | * |
||
| 112 | * Use the $prev_value parameter to differentiate between meta fields with the |
||
| 113 | * same key and Donor ID. |
||
| 114 | * |
||
| 115 | * If the meta field for the donor does not exist, it will be added. |
||
| 116 | * |
||
| 117 | * @access private |
||
| 118 | * @since 1.6 |
||
| 119 | * |
||
| 120 | * @param int $donor_id Donor ID. |
||
| 121 | * @param string $meta_key Metadata key. |
||
| 122 | * @param mixed $meta_value Metadata value. |
||
| 123 | * @param mixed $prev_value Optional. Previous value to check before removing. |
||
| 124 | * |
||
| 125 | * @return bool False on failure, true if success. |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function update_meta( $donor_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) { |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Remove metadata matching criteria from a donor. |
||
| 138 | * |
||
| 139 | * For internal use only. Use Give_Donor->delete_meta() for public usage. |
||
| 140 | * |
||
| 141 | * You can match based on the key, or key and value. Removing based on key and |
||
| 142 | * value, will keep from removing duplicate metadata with the same key. It also |
||
| 143 | * allows removing all metadata matching key, if needed. |
||
| 144 | * |
||
| 145 | * @access private |
||
| 146 | * @since 1.6 |
||
| 147 | * |
||
| 148 | * @param int $donor_id Donor ID. |
||
| 149 | * @param string $meta_key Metadata name. |
||
| 150 | * @param mixed $meta_value Optional. Metadata value. |
||
| 151 | * |
||
| 152 | * @return bool False for failure. True for success. |
||
| 153 | */ |
||
| 154 | public function delete_meta( $donor_id = 0, $meta_key = '', $meta_value = '' ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Remove all meta data matching criteria from a donor id. |
||
| 160 | * |
||
| 161 | * @access private |
||
| 162 | * @since 1.8.14 |
||
| 163 | * |
||
| 164 | * @param int $donor_id Donor ID. |
||
| 165 | * |
||
| 166 | * @return bool False for failure. True for success. |
||
| 167 | */ |
||
| 168 | public function delete_all_meta( $donor_id = 0 ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Create the table |
||
| 175 | * |
||
| 176 | * @access public |
||
| 177 | * @since 1.6 |
||
| 178 | * |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function create_table() { |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Given a donor ID, make sure it's a positive number, greater than zero before inserting or adding. |
||
| 202 | * |
||
| 203 | * @access private |
||
| 204 | * @since 1.6 |
||
| 205 | * |
||
| 206 | * @param int|stripe $donor_id A passed donor ID. |
||
| 207 | * |
||
| 208 | * @return int|bool The normalized donor ID or false if it's found to not be valid. |
||
| 209 | */ |
||
| 210 | private function sanitize_donor_id( $donor_id ) { |
||
| 229 | |||
| 230 | } |
||
| 231 |