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 FrmFieldValue { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @since 2.03.11 |
||
| 10 | * |
||
| 11 | * @var stdClass |
||
| 12 | */ |
||
| 13 | protected $field = null; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @since 2.03.11 |
||
| 17 | * |
||
| 18 | * @var stdClass |
||
| 19 | */ |
||
| 20 | protected $entry = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @since 2.03.11 |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $source = ''; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @since 2.03.11 |
||
| 31 | * |
||
| 32 | * @var mixed |
||
| 33 | */ |
||
| 34 | protected $saved_value = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @since 2.03.11 |
||
| 38 | * |
||
| 39 | * @var mixed |
||
| 40 | */ |
||
| 41 | protected $displayed_value = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * FrmFieldValue constructor. |
||
| 45 | * |
||
| 46 | * @param stdClass $field |
||
| 47 | * @param stdClass $entry |
||
| 48 | * @param array $atts |
||
| 49 | */ |
||
| 50 | public function __construct( $field, $entry, $atts = array() ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Initialize the source property |
||
| 64 | * |
||
| 65 | * @since 2.03.11 |
||
| 66 | * |
||
| 67 | * @param array $atts |
||
| 68 | */ |
||
| 69 | View Code Duplication | protected function init_source( $atts ) { |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Initialize the saved_value property |
||
| 79 | * |
||
| 80 | * @since 2.03.11 |
||
| 81 | */ |
||
| 82 | protected function init_saved_value() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Initialize a field's displayed value |
||
| 94 | * |
||
| 95 | * @since 2.03.11 |
||
| 96 | */ |
||
| 97 | protected function init_displayed_value() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get the saved_value property |
||
| 105 | * |
||
| 106 | * @since 2.03.11 |
||
| 107 | */ |
||
| 108 | public function get_saved_value() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the field property's label |
||
| 114 | * |
||
| 115 | * @since 2.03.11 |
||
| 116 | */ |
||
| 117 | public function get_field_label() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the field property's key |
||
| 123 | * |
||
| 124 | * @since 2.03.11 |
||
| 125 | */ |
||
| 126 | public function get_field_key() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the field property's type |
||
| 132 | * |
||
| 133 | * @since 2.03.11 |
||
| 134 | */ |
||
| 135 | public function get_field_type() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the displayed_value property |
||
| 141 | * |
||
| 142 | * @since 2.03.11 |
||
| 143 | */ |
||
| 144 | public function get_displayed_value() { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Filter the displayed_value property |
||
| 150 | * |
||
| 151 | * @since 2.03.11 |
||
| 152 | */ |
||
| 153 | protected function filter_displayed_value() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Clean a field's saved value |
||
| 179 | * |
||
| 180 | * @since 2.03.11 |
||
| 181 | */ |
||
| 182 | protected function clean_saved_value() { |
||
| 192 | } |
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.