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 |
||
| 156 | abstract class WordPoints_Importer { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * The name of the importer. |
||
| 160 | * |
||
| 161 | * @since 1.0.0 |
||
| 162 | * |
||
| 163 | * @type string $name |
||
| 164 | */ |
||
| 165 | protected $name; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * The components supported by this importer. |
||
| 169 | * |
||
| 170 | * The keys are the component slugs, the values arrays of options for importing |
||
| 171 | * to that component. |
||
| 172 | * |
||
| 173 | * @since 1.0.0 |
||
| 174 | * |
||
| 175 | * @type array[] $components |
||
| 176 | */ |
||
| 177 | protected $components = array(); |
||
| 178 | |||
| 179 | /** |
||
| 180 | * The feedback provider object. |
||
| 181 | * |
||
| 182 | * This is only set by self::do_import(). |
||
| 183 | * |
||
| 184 | * @since 1.0.0 |
||
| 185 | * |
||
| 186 | * @type WordPoints_Importer_Feedback $feedback |
||
| 187 | */ |
||
| 188 | protected $feedback; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Check if this importer is available. |
||
| 192 | * |
||
| 193 | * @since 1.0.0 |
||
| 194 | * |
||
| 195 | * @return true|WP_Error A WP_Error if the importer is not available. |
||
| 196 | */ |
||
| 197 | abstract public function is_available(); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Construct the importer. |
||
| 201 | * |
||
| 202 | * @since 1.0.0 |
||
| 203 | * |
||
| 204 | * @param string $name The name of the importer. |
||
| 205 | */ |
||
| 206 | public function __construct( $name ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Check if this importer supports a specific component. |
||
| 213 | * |
||
| 214 | * @since 1.0.0 |
||
| 215 | * |
||
| 216 | * @param string $component The slug of a component. |
||
| 217 | * |
||
| 218 | * @return bool True if the component is supported, otherwise false. |
||
| 219 | */ |
||
| 220 | public function supports_component( $component ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get the import options for a component. |
||
| 227 | * |
||
| 228 | * @since 1.0.0 |
||
| 229 | * |
||
| 230 | * @param string $component The slug of a component. |
||
| 231 | * |
||
| 232 | * @return array[] The options for this component. |
||
| 233 | */ |
||
| 234 | public function get_options_for_component( $component ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Run the import. |
||
| 245 | * |
||
| 246 | * @since 1.0.0 |
||
| 247 | * |
||
| 248 | * @param array $args The settings for the import. |
||
| 249 | * @param WordPoints_Importer_Feedback $feedback The feedback object. |
||
| 250 | */ |
||
| 251 | public function do_import( array $args, $feedback = null ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Prevent any interruptions from occurring during the import. |
||
| 272 | * |
||
| 273 | * @since 1.2.1 |
||
| 274 | */ |
||
| 275 | protected function no_interruptions() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Validate the import settings for a component. |
||
| 290 | * |
||
| 291 | * @since 1.0.0 |
||
| 292 | * |
||
| 293 | * @param string $component The slug of the component. |
||
| 294 | * @param array $settings The settings supplied for this component. |
||
| 295 | * |
||
| 296 | * @return bool Whether the settings are valid. |
||
| 297 | */ |
||
| 298 | protected function validate_import_settings( $component, $settings ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Run the import for a component. |
||
| 314 | * |
||
| 315 | * @since 1.0.0 |
||
| 316 | * |
||
| 317 | * @param string $component The component to run the import for. |
||
| 318 | * @param array $options The selected options of what to import. |
||
| 319 | */ |
||
| 320 | protected function do_import_for_component( $component, $options ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Run the import for an option. |
||
| 356 | * |
||
| 357 | * The import is split up into different options which the user can select (these |
||
| 358 | * are displayed to the user as checkboxes in the form). This handles the import |
||
| 359 | * for each of the individual things the user has selected to import. These are |
||
| 360 | * all optional, so each is just termed an import "option" here. |
||
| 361 | * |
||
| 362 | * @since 1.0.0 |
||
| 363 | * |
||
| 364 | * @param string $option An import option that has been selected. |
||
| 365 | * @param string $component The component this option is for. |
||
| 366 | * @param array $settings Other settings for this component. |
||
| 367 | */ |
||
| 368 | protected function do_import_for_option( $option, $component, $settings ) { |
||
| 391 | } |
||
| 392 | |||
| 394 |
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.