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 | View Code Duplication | class Give_DB_Form_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_Form_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 form meta field for a form. |
||
| 74 | * |
||
| 75 | * For internal use only. Use Give_Form->get_meta() for public usage. |
||
| 76 | * |
||
| 77 | * @access public |
||
| 78 | * @since 2.0 |
||
| 79 | * |
||
| 80 | * @param int $form_id Form ID. |
||
| 81 | * @param string $meta_key The meta key to retrieve. |
||
| 82 | * @param bool $single Whether to return a single value. |
||
| 83 | * |
||
| 84 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
||
| 85 | */ |
||
| 86 | public function get_meta( $form_id = 0, $meta_key = '', $single = false ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Add meta data field to a form. |
||
| 111 | * |
||
| 112 | * For internal use only. Use Give_Form->add_meta() for public usage. |
||
| 113 | * |
||
| 114 | * @access private |
||
| 115 | * @since 2.0 |
||
| 116 | * |
||
| 117 | * @param int $form_id Form ID. |
||
| 118 | * @param string $meta_key Metadata name. |
||
| 119 | * @param mixed $meta_value Metadata value. |
||
| 120 | * @param bool $unique Optional, default is false. Whether the same key should not be added. |
||
| 121 | * |
||
| 122 | * @return bool False for failure. True for success. |
||
| 123 | */ |
||
| 124 | public function add_meta( $form_id = 0, $meta_key = '', $meta_value, $unique = false ) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Update form meta field based on Form ID. |
||
| 137 | * |
||
| 138 | * For internal use only. Use Give_Form->update_meta() for public usage. |
||
| 139 | * |
||
| 140 | * Use the $prev_value parameter to differentiate between meta fields with the |
||
| 141 | * same key and Form ID. |
||
| 142 | * |
||
| 143 | * If the meta field for the form does not exist, it will be added. |
||
| 144 | * |
||
| 145 | * @access public |
||
| 146 | * @since 2.0 |
||
| 147 | * |
||
| 148 | * @param int $form_id Form ID. |
||
| 149 | * @param string $meta_key Metadata key. |
||
| 150 | * @param mixed $meta_value Metadata value. |
||
| 151 | * @param mixed $prev_value Optional. Previous value to check before removing. |
||
| 152 | * |
||
| 153 | * @return bool False on failure, true if success. |
||
| 154 | */ |
||
| 155 | public function update_meta( $form_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Remove metadata matching criteria from a form. |
||
| 168 | * |
||
| 169 | * You can match based on the key, or key and value. Removing based on key and |
||
| 170 | * value, will keep from removing duplicate metadata with the same key. It also |
||
| 171 | * allows removing all metadata matching key, if needed. |
||
| 172 | * |
||
| 173 | * @access public |
||
| 174 | * @since 2.0 |
||
| 175 | * |
||
| 176 | * @param int $form_id Form ID. |
||
| 177 | * @param string $meta_key Metadata name. |
||
| 178 | * @param mixed $meta_value Optional. Metadata value. |
||
| 179 | * |
||
| 180 | * @return bool False for failure. True for success. |
||
| 181 | */ |
||
| 182 | public function delete_meta( $form_id = 0, $meta_key = '', $meta_value = '' ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Create the table |
||
| 195 | * |
||
| 196 | * @access public |
||
| 197 | * @since 2.0 |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | public function create_table() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Add support for hidden functions. |
||
| 224 | * |
||
| 225 | * @since 2.0 |
||
| 226 | * @access public |
||
| 227 | * |
||
| 228 | * @param $name |
||
| 229 | * @param $arguments |
||
| 230 | * |
||
| 231 | * @return mixed |
||
| 232 | */ |
||
| 233 | public function __call( $name, $arguments ) { |
||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * Check if current id of donation form type or not |
||
| 279 | * |
||
| 280 | * @since 2.0 |
||
| 281 | * @access private |
||
| 282 | * |
||
| 283 | * @param $ID |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | private function is_form( $ID ) { |
||
| 290 | } |
||
| 291 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.