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 |
||
| 12 | View Code Duplication | class OptinForm extends OptinFormBase { |
|
|
|
|||
| 13 | |||
| 14 | /** |
||
| 15 | * The base name of our option in the options table. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $option = 'yikes_easy_mailchimp_extender_forms'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Get a form with the given ID. |
||
| 23 | * |
||
| 24 | * @author Freddie Mixell |
||
| 25 | * |
||
| 26 | * @param int $form_id The ID of the form to retrieve. |
||
| 27 | * |
||
| 28 | * @return array The array of form data. |
||
| 29 | */ |
||
| 30 | public function find( $form_id ) { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Get the IDs of all registered forms. |
||
| 38 | * |
||
| 39 | * @author Jeremy Pry |
||
| 40 | * @return array All form IDs. |
||
| 41 | */ |
||
| 42 | public function get_form_ids() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Update the data for a particular form. |
||
| 48 | * |
||
| 49 | * @author Jeremy Pry |
||
| 50 | * |
||
| 51 | * @param int $form_id The form ID to update. |
||
| 52 | * @param array $data The form data to update. |
||
| 53 | * |
||
| 54 | * @return bool Whether the form was successfully updated. |
||
| 55 | */ |
||
| 56 | public function update_form( $form_id, $data ) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Create a new form. |
||
| 73 | * |
||
| 74 | * @author Jeremy Pry |
||
| 75 | * |
||
| 76 | * @param array $form_data Data to apply to the new form. |
||
| 77 | * |
||
| 78 | * @return int|bool The new form ID, or false on failure. |
||
| 79 | */ |
||
| 80 | public function create_form( $form_data ) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Delete a form. |
||
| 108 | * |
||
| 109 | * @author Jeremy Pry |
||
| 110 | * |
||
| 111 | * @param int $form_id The form ID to delete. |
||
| 112 | * |
||
| 113 | * @return bool Whether the form was successfully deleted. |
||
| 114 | */ |
||
| 115 | public function delete_form( $form_id ) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get all data for all forms. |
||
| 129 | * |
||
| 130 | * @author Jeremy Pry |
||
| 131 | * @return array All form data, indexed by form ID. |
||
| 132 | */ |
||
| 133 | public function get_all_forms() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get the name of the option used for saving the forms. |
||
| 139 | * |
||
| 140 | * @author Jeremy Pry |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function get_option_name() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Import forms in bulk. |
||
| 149 | * |
||
| 150 | * @author Jeremy Pry |
||
| 151 | * |
||
| 152 | * @param array $form_data Array of form data, indexed by form ID. |
||
| 153 | * @param bool $replace_existing Whether to replace existing forms. |
||
| 154 | */ |
||
| 155 | public function import_forms( $form_data, $replace_existing ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Add our option to the database. |
||
| 169 | * |
||
| 170 | * @author Jeremy Pry |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function create_option() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get our forms option from the database. |
||
| 179 | * |
||
| 180 | * @author Jeremy Pry |
||
| 181 | * @return array The array of form data. |
||
| 182 | */ |
||
| 183 | protected function get_option() { |
||
| 186 | } |
||
| 187 |
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.