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 |
||
| 15 | abstract class WordPoints_Importer { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The name of the importer. |
||
| 19 | * |
||
| 20 | * @since 1.0.0 |
||
| 21 | * |
||
| 22 | * @type string $name |
||
| 23 | */ |
||
| 24 | protected $name; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The components supported by this importer. |
||
| 28 | * |
||
| 29 | * The keys are the component slugs, the values arrays of options for importing |
||
| 30 | * to that component. |
||
| 31 | * |
||
| 32 | * @since 1.0.0 |
||
| 33 | * |
||
| 34 | * @type array[] $components |
||
| 35 | */ |
||
| 36 | protected $components = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The feedback provider object. |
||
| 40 | * |
||
| 41 | * This is only set by self::do_import(). |
||
| 42 | * |
||
| 43 | * @since 1.0.0 |
||
| 44 | * |
||
| 45 | * @type WordPoints_Importer_Feedback $feedback |
||
| 46 | */ |
||
| 47 | protected $feedback; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Check if this importer is available. |
||
| 51 | * |
||
| 52 | * @since 1.0.0 |
||
| 53 | * |
||
| 54 | * @return true|WP_Error A WP_Error if the importer is not available. |
||
| 55 | */ |
||
| 56 | abstract public function is_available(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Construct the importer. |
||
| 60 | * |
||
| 61 | * @since 1.0.0 |
||
| 62 | * |
||
| 63 | * @param string $name The name of the importer. |
||
| 64 | */ |
||
| 65 | public function __construct( $name ) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Check if this importer supports a specific component. |
||
| 72 | * |
||
| 73 | * @since 1.0.0 |
||
| 74 | * |
||
| 75 | * @param string $component The slug of a component. |
||
| 76 | * |
||
| 77 | * @return bool True if the component is supported, otherwise false. |
||
| 78 | */ |
||
| 79 | public function supports_component( $component ) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get the import options for a component. |
||
| 86 | * |
||
| 87 | * @since 1.0.0 |
||
| 88 | * |
||
| 89 | * @param string $component The slug of a component. |
||
| 90 | * |
||
| 91 | * @return array[] The options for this component. |
||
| 92 | */ |
||
| 93 | public function get_options_for_component( $component ) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Run the import. |
||
| 104 | * |
||
| 105 | * @since 1.0.0 |
||
| 106 | * |
||
| 107 | * @param array $args The settings for the import. |
||
| 108 | * @param WordPoints_Importer_Feedback $feedback The feedback object. |
||
| 109 | */ |
||
| 110 | public function do_import( array $args, $feedback = null ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Prevent any interruptions from occurring during the import. |
||
| 132 | * |
||
| 133 | * @since 1.2.1 |
||
| 134 | */ |
||
| 135 | protected function no_interruptions() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Validate the import settings for a component. |
||
| 142 | * |
||
| 143 | * @since 1.0.0 |
||
| 144 | * |
||
| 145 | * @param string $component The slug of the component. |
||
| 146 | * @param array $settings The settings supplied for this component. |
||
| 147 | * |
||
| 148 | * @return bool Whether the settings are valid. |
||
| 149 | */ |
||
| 150 | protected function validate_import_settings( $component, $settings ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Run the import for a component. |
||
| 166 | * |
||
| 167 | * @since 1.0.0 |
||
| 168 | * |
||
| 169 | * @param string $component The component to run the import for. |
||
| 170 | * @param array $options The selected options of what to import. |
||
| 171 | */ |
||
| 172 | protected function do_import_for_component( $component, $options ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Run the import for an option. |
||
| 211 | * |
||
| 212 | * The import is split up into different options which the user can select (these |
||
| 213 | * are displayed to the user as checkboxes in the form). This handles the import |
||
| 214 | * for each of the individual things the user has selected to import. These are |
||
| 215 | * all optional, so each is just termed an import "option" here. |
||
| 216 | * |
||
| 217 | * @since 1.0.0 |
||
| 218 | * |
||
| 219 | * @param string $option An import option that has been selected. |
||
| 220 | * @param string $component The component this option is for. |
||
| 221 | * @param array $settings Other settings for this component. |
||
| 222 | */ |
||
| 223 | protected function do_import_for_option( $option, $component, $settings ) { |
||
| 248 | } |
||
| 249 | |||
| 251 |