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 |
||
| 45 | class BundleOptionObserver extends AbstractProductImportObserver implements DynamicAttributeObserverInterface |
||
| 46 | { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The product bundle processor instance. |
||
| 50 | * |
||
| 51 | * @var \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface |
||
| 52 | */ |
||
| 53 | protected $productBundleProcessor; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The attribute loader instance. |
||
| 57 | * |
||
| 58 | * @var \TechDivision\Import\Observers\AttributeLoaderInterface |
||
| 59 | */ |
||
| 60 | protected $attributeLoader; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The entity merger instance. |
||
| 64 | * |
||
| 65 | * @var \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface |
||
| 66 | */ |
||
| 67 | protected $entityMerger; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Initialize the "dymanmic" columns. |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $columns = array(MemberNames::POSITION => array(ColumnKeys::BUNDLE_VALUE_POSITION, BackendTypeKeys::BACKEND_TYPE_INT)); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Initialize the observer with the passed product bundle processor instance. |
||
| 78 | * |
||
| 79 | * @param \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface $productBundleProcessor The product bundle processor instance |
||
| 80 | * @param \TechDivision\Import\Observers\AttributeLoaderInterface|null $attributeLoader The attribute loader instance |
||
| 81 | * @param \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface $entityMerger The entity merger instance |
||
| 82 | * @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function __construct( |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Return's the product bundle processor instance. |
||
| 102 | * |
||
| 103 | * @return \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface The product bundle processor instance |
||
| 104 | */ |
||
| 105 | protected function getProductBundleProcessor() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Process the observer's business logic. |
||
| 112 | * |
||
| 113 | * @return array The processed row |
||
| 114 | */ |
||
| 115 | protected function process() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Appends the dynamic attributes to the static ones and returns them. |
||
| 144 | * |
||
| 145 | * @return array The array with all available attributes |
||
| 146 | */ |
||
| 147 | protected function prepareDynamicAttributes() : array |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Prepare the attributes of the entity that has to be persisted. |
||
| 154 | * |
||
| 155 | * @return array The prepared attributes |
||
| 156 | */ |
||
| 157 | protected function prepareAttributes() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Merge's and return's the entity with the passed attributes and set's the |
||
| 185 | * passed status. |
||
| 186 | * |
||
| 187 | * @param array $entity The entity to merge the attributes into |
||
| 188 | * @param array $attr The attributes to be merged |
||
| 189 | * @param string|null $changeSetName The change set name to use |
||
| 190 | * |
||
| 191 | * @return array The merged entity |
||
| 192 | * @todo https://github.com/techdivision/import/issues/179 |
||
| 193 | */ |
||
| 194 | View Code Duplication | protected function mergeEntity(array $entity, array $attr, $changeSetName = null) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Load's and return's a raw entity without primary key but the mandatory members only and nulled values. |
||
| 205 | * |
||
| 206 | * @param array $data An array with data that will be used to initialize the raw entity with |
||
| 207 | * |
||
| 208 | * @return array The initialized entity |
||
| 209 | */ |
||
| 210 | protected function loadRawEntity(array $data = array()) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Initialize the bundle option with the passed attributes and returns an instance. |
||
| 217 | * |
||
| 218 | * @param array $attr The bundle option attributes |
||
| 219 | * |
||
| 220 | * @return array The initialized bundle option |
||
| 221 | */ |
||
| 222 | protected function initializeBundleOption(array $attr) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Reset the position counter to 1. |
||
| 229 | * |
||
| 230 | * @return void |
||
| 231 | * @deprecated Since 22.0.0 |
||
| 232 | */ |
||
| 233 | protected function resetPositionCounter() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Add's the passed mapping to the subject. |
||
| 240 | * |
||
| 241 | * @param array $mapping The mapping to add |
||
| 242 | * |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | protected function addParentSkuNameMapping($mapping = array()) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Query whether or not the option for the passed parent SKU and name has already been created. |
||
| 252 | * |
||
| 253 | * @param string $parentSku The parent SKU to query for |
||
| 254 | * @param string $name The option name to query for |
||
| 255 | * |
||
| 256 | * @return boolean TRUE if the option already exists, else FALSE |
||
| 257 | */ |
||
| 258 | protected function exists($parentSku, $name) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Return the entity ID for the passed SKU. |
||
| 265 | * |
||
| 266 | * @param string $sku The SKU to return the entity ID for |
||
| 267 | * |
||
| 268 | * @return integer The mapped entity ID |
||
| 269 | * @throws \Exception Is thrown if the SKU is not mapped yet |
||
| 270 | */ |
||
| 271 | protected function mapSku($sku) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Persist's the passed product bundle option data and return's the ID. |
||
| 278 | * |
||
| 279 | * @param array $productBundleOption The product bundle option data to persist |
||
| 280 | * |
||
| 281 | * @return string The ID of the persisted entity |
||
| 282 | */ |
||
| 283 | protected function persistProductBundleOption($productBundleOption) |
||
| 287 | } |
||
| 288 |
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.