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 |
||
| 6 | class FrmEntryValues { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @var stdClass |
||
| 10 | */ |
||
| 11 | protected $entry = null; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var int |
||
| 15 | */ |
||
| 16 | protected $form_id; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $fields = array(); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var FrmFieldValue[] |
||
| 25 | */ |
||
| 26 | protected $field_values = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $user_info = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $include_fields = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $exclude_fields = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @since 2.03.11 |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $source = ''; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * FrmEntryValues constructor |
||
| 52 | * |
||
| 53 | * @since 2.03.11 |
||
| 54 | * |
||
| 55 | * @param int|string $entry_id |
||
| 56 | * @param array $atts |
||
| 57 | */ |
||
| 58 | public function __construct( $entry_id, $atts = array() ) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Set the entry property |
||
| 76 | * |
||
| 77 | * @since 2.03.11 |
||
| 78 | * |
||
| 79 | * @param int|string $entry_id |
||
| 80 | */ |
||
| 81 | protected function init_entry( $entry_id ) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Set the form_id property |
||
| 87 | * |
||
| 88 | * @since 2.03.11 |
||
| 89 | */ |
||
| 90 | protected function init_form_id() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Set the include_fields property |
||
| 96 | * |
||
| 97 | * @since 2.03.11 |
||
| 98 | * |
||
| 99 | * @param array $atts |
||
| 100 | */ |
||
| 101 | protected function init_include_fields( $atts ) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Set the exclude_fields property |
||
| 123 | * |
||
| 124 | * @since 2.03.11 |
||
| 125 | * |
||
| 126 | * @param array $atts |
||
| 127 | */ |
||
| 128 | protected function init_exclude_fields( $atts ) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Initialize the source property |
||
| 134 | * |
||
| 135 | * @since 2.03.11 |
||
| 136 | * |
||
| 137 | * @param array $atts |
||
| 138 | */ |
||
| 139 | View Code Duplication | protected function init_source( $atts ) { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Prepare an array property value, such as include_fields and exclude_fields |
||
| 149 | * |
||
| 150 | * @since 2.03.11 |
||
| 151 | * |
||
| 152 | * @param string $index |
||
| 153 | * @param array $atts |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | private function prepare_array_property( $index, $atts ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set the fields property |
||
| 175 | * |
||
| 176 | * @since 2.03.11 |
||
| 177 | */ |
||
| 178 | protected function init_fields() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Set the field_values property |
||
| 184 | * |
||
| 185 | * @since 2.03.11 |
||
| 186 | */ |
||
| 187 | protected function init_field_values() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the field_values property |
||
| 197 | * |
||
| 198 | * @since 2.03.11 |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function get_field_values() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set the user_info property |
||
| 208 | * |
||
| 209 | * @since 2.03.11 |
||
| 210 | */ |
||
| 211 | protected function init_user_info() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get the user_info property |
||
| 245 | * |
||
| 246 | * @since 2.03.11 |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | public function get_user_info() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Check if a field should be included in the values |
||
| 256 | * |
||
| 257 | * @since 2.03.11 |
||
| 258 | * |
||
| 259 | * @param stdClass $field |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | protected function is_field_included( $field ) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Check if a field is in the include fields or exclude fields array |
||
| 277 | * |
||
| 278 | * @since 2.03.11 |
||
| 279 | * |
||
| 280 | * @param stdClass $field |
||
| 281 | * @param array $array |
||
| 282 | * |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | protected function is_field_in_array( $field, $array ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Add a field's values to the field_values property |
||
| 291 | * |
||
| 292 | * @since 2.03.11 |
||
| 293 | * |
||
| 294 | * @param stdClass $field |
||
| 295 | */ |
||
| 296 | protected function add_field_values( $field ) { |
||
| 299 | } |