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 |
||
| 43 | class BundleSelectionObserver extends AbstractProductImportObserver implements DynamicAttributeObserverInterface |
||
| 44 | { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The product bundle processor instance. |
||
| 48 | * |
||
| 49 | * @var \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface |
||
| 50 | */ |
||
| 51 | protected $productBundleProcessor; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The attribute loader instance. |
||
| 55 | * |
||
| 56 | * @var \TechDivision\Import\Observers\AttributeLoaderInterface |
||
| 57 | */ |
||
| 58 | protected $attributeLoader; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Initialize the "dymanmic" columns. |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $columns = array( |
||
| 66 | MemberNames::POSITION => array(ColumnKeys::BUNDLE_VALUE_SELECTION_POSITION, BackendTypeKeys::BACKEND_TYPE_INT), |
||
| 67 | MemberNames::IS_DEFAULT => array(ColumnKeys::BUNDLE_VALUE_DEFAULT, BackendTypeKeys::BACKEND_TYPE_INT), |
||
| 68 | MemberNames::SELECTION_PRICE_VALUE => array(ColumnKeys::BUNDLE_VALUE_PRICE, BackendTypeKeys::BACKEND_TYPE_FLOAT), |
||
| 69 | MemberNames::SELECTION_CAN_CHANGE_QTY => array(ColumnKeys::BUNDLE_VALUE_CAN_CHANGE_QTY, BackendTypeKeys::BACKEND_TYPE_INT) |
||
| 70 | ); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Array with virtual column name mappings (this is a temporary |
||
| 74 | * solution till techdivision/import#179 as been implemented). |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | * @todo https://github.com/techdivision/import/issues/179 |
||
| 78 | */ |
||
| 79 | protected $virtualMapping = array( |
||
| 80 | MemberNames::POSITION => ColumnKeys::BUNDLE_VALUE_SELECTION_POSITION, |
||
| 81 | MemberNames::IS_DEFAULT => ColumnKeys::BUNDLE_VALUE_DEFAULT, |
||
| 82 | MemberNames::SELECTION_PRICE_VALUE => ColumnKeys::BUNDLE_VALUE_PRICE, |
||
| 83 | MemberNames::SELECTION_CAN_CHANGE_QTY => ColumnKeys::BUNDLE_VALUE_CAN_CHANGE_QTY |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Initialize the observer with the passed product bundle processor instance. |
||
| 88 | * |
||
| 89 | * @param \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface $productBundleProcessor The product bundle processor instance |
||
| 90 | * @param \TechDivision\Import\Observers\AttributeLoaderInterface|null $attributeLoader The attribute loader instance |
||
| 91 | * @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function __construct( |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Return's the product bundle processor instance. |
||
| 109 | * |
||
| 110 | * @return \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface The product bundle processor instance |
||
| 111 | */ |
||
| 112 | protected function getProductBundleProcessor() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Query whether or not a value for the column with the passed name exists. |
||
| 119 | * |
||
| 120 | * @param string $name The column name to query for a valid value |
||
| 121 | * |
||
| 122 | * @return boolean TRUE if the value is set, else FALSE |
||
| 123 | * @todo https://github.com/techdivision/import/issues/179 |
||
| 124 | */ |
||
| 125 | public function hasValue($name) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Process the observer's business logic. |
||
| 132 | * |
||
| 133 | * @return array The processed row |
||
| 134 | */ |
||
| 135 | protected function process() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Appends the dynamic attributes to the static ones and returns them. |
||
| 156 | * |
||
| 157 | * @return array The array with all available attributes |
||
| 158 | */ |
||
| 159 | protected function prepareDynamicAttributes() : array |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Prepare the attributes of the entity that has to be persisted. |
||
| 166 | * |
||
| 167 | * @return array The prepared attributes |
||
| 168 | */ |
||
| 169 | protected function prepareAttributes() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Load's and return's a raw entity without primary key but the mandatory members only and nulled values. |
||
| 215 | * |
||
| 216 | * @param array $data An array with data that will be used to initialize the raw entity with |
||
| 217 | * |
||
| 218 | * @return array The initialized entity |
||
| 219 | */ |
||
| 220 | protected function loadRawEntity(array $data = array()) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Initialize the bundle selection with the passed attributes and returns an instance. |
||
| 227 | * |
||
| 228 | * @param array $attr The bundle selection attributes |
||
| 229 | * |
||
| 230 | * @return array The initialized bundle selection |
||
| 231 | */ |
||
| 232 | protected function initializeBundleSelection(array $attr) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Return's the last created option ID. |
||
| 239 | * |
||
| 240 | * @return integer $optionId The last created option ID |
||
| 241 | */ |
||
| 242 | protected function getLastOptionId() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Save's the mapping of the child SKU and the selection ID. |
||
| 249 | * |
||
| 250 | * @param string $childSku The child SKU of the selection |
||
| 251 | * @param integer $selectionId The selection ID to save |
||
| 252 | * |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | protected function addChildSkuSelectionIdMapping($childSku, $selectionId) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Returns the acutal value of the position counter and raise's it by one. |
||
| 262 | * |
||
| 263 | * @return integer The actual value of the position counter |
||
| 264 | * @deprecated Since 22.0.0 |
||
| 265 | */ |
||
| 266 | protected function raisePositionCounter() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Return's the mapping for the passed price type. |
||
| 273 | * |
||
| 274 | * @param string $priceType The price type to map |
||
| 275 | * |
||
| 276 | * @return integer The mapped price type |
||
| 277 | * @throws \Exception Is thrown, if the passed price type can't be mapped |
||
| 278 | */ |
||
| 279 | protected function mapPriceType($priceType) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Return the entity ID for the passed SKU. |
||
| 286 | * |
||
| 287 | * @param string $sku The SKU to return the entity ID for |
||
| 288 | * |
||
| 289 | * @return integer The mapped entity ID |
||
| 290 | * @throws \Exception Is thrown if the SKU is not mapped yet |
||
| 291 | */ |
||
| 292 | protected function mapSku($sku) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return the entity ID for the passed SKU. |
||
| 299 | * |
||
| 300 | * @param string $sku The SKU to return the entity ID for |
||
| 301 | * |
||
| 302 | * @return integer The mapped entity ID |
||
| 303 | * @throws \Exception Is thrown if the SKU is not mapped yet |
||
| 304 | */ |
||
| 305 | protected function mapSkuToEntityId($sku) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Persist's the passed product bundle selection data and return's the ID. |
||
| 312 | * |
||
| 313 | * @param array $productBundleSelection The product bundle selection data to persist |
||
| 314 | * |
||
| 315 | * @return string The ID of the persisted entity |
||
| 316 | */ |
||
| 317 | protected function persistProductBundleSelection($productBundleSelection) |
||
| 321 | } |
||
| 322 |
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.