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 |
||
| 38 | class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The trait that implements the export functionality. |
||
| 43 | * |
||
| 44 | * @var \TechDivision\Import\Subjects\ExportableTrait |
||
| 45 | */ |
||
| 46 | use ExportableTrait; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The mapping for the SKU => visibility. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $entityIdVisibilityIdMapping = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The array with the pre-loaded entity IDs. |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | protected $preLoadedEntityIds = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Mappings for the table column => CSV column header. |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $headerStockMappings = array( |
||
| 68 | 'qty' => array('qty', 'float'), |
||
| 69 | 'min_qty' => array('out_of_stock_qty', 'float'), |
||
| 70 | 'use_config_min_qty' => array('use_config_min_qty', 'int'), |
||
| 71 | 'is_qty_decimal' => array('is_qty_decimal', 'int'), |
||
| 72 | 'backorders' => array('allow_backorders', 'int'), |
||
| 73 | 'use_config_backorders' => array('use_config_backorders', 'int'), |
||
| 74 | 'min_sale_qty' => array('min_cart_qty', 'float'), |
||
| 75 | 'use_config_min_sale_qty' => array('use_config_min_sale_qty', 'int'), |
||
| 76 | 'max_sale_qty' => array('max_cart_qty', 'float'), |
||
| 77 | 'use_config_max_sale_qty' => array('use_config_max_sale_qty', 'int'), |
||
| 78 | 'is_in_stock' => array('is_in_stock', 'int'), |
||
| 79 | 'notify_stock_qty' => array('notify_on_stock_below', 'float'), |
||
| 80 | 'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'), |
||
| 81 | 'manage_stock' => array('manage_stock', 'int'), |
||
| 82 | 'use_config_manage_stock' => array('use_config_manage_stock', 'int'), |
||
| 83 | 'use_config_qty_increments' => array('use_config_qty_increments', 'int'), |
||
| 84 | 'qty_increments' => array('qty_increments', 'float'), |
||
| 85 | 'use_config_enable_qty_inc' => array('use_config_enable_qty_inc', 'int'), |
||
| 86 | 'enable_qty_increments' => array('enable_qty_increments', 'int'), |
||
| 87 | 'is_decimal_divided' => array('is_decimal_divided', 'int'), |
||
| 88 | ); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The array with the available visibility keys. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $availableVisibilities = array( |
||
| 96 | 'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE, |
||
| 97 | 'Catalog' => VisibilityKeys::VISIBILITY_IN_CATALOG, |
||
| 98 | 'Search' => VisibilityKeys::VISIBILITY_IN_SEARCH, |
||
| 99 | 'Catalog, Search' => VisibilityKeys::VISIBILITY_BOTH |
||
| 100 | ); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The category IDs the product is related with. |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $productCategoryIds = array(); |
||
| 108 | |||
| 109 | /** |
||
| 110 | * The default callback mappings for the Magento standard product attributes. |
||
| 111 | * |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | protected $defaultCallbackMappings = array( |
||
| 115 | 'visibility' => array('import_product.callback.visibility'), |
||
| 116 | 'tax_class_id' => array('import_product.callback.tax.class'), |
||
| 117 | 'bundle_price_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 118 | 'bundle_sku_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 119 | 'bundle_weight_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 120 | 'bundle_price_view' => array('import_product_bundle.callback.bundle.price.view'), |
||
| 121 | 'bundle_shipment_type' => array('import_product_bundle.callback.bundle.shipment.type') |
||
| 122 | ); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The used URL keys. |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | protected $usedUrlKeys = array(); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * The available entity types. |
||
| 133 | * |
||
| 134 | * @var array |
||
| 135 | */ |
||
| 136 | protected $entityTypes = array(); |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 140 | * |
||
| 141 | * @param string $serial The serial of the actual import |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | 18 | public function setUp($serial) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Clean up the global data after importing the bunch. |
||
| 160 | * |
||
| 161 | * @param string $serial The serial of the actual import |
||
| 162 | * |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | public function tearDown($serial) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Return's the default callback mappings. |
||
| 185 | * |
||
| 186 | * @return array The default callback mappings |
||
| 187 | */ |
||
| 188 | 18 | public function getDefaultCallbackMappings() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Return's the mappings for the table column => CSV column header. |
||
| 195 | * |
||
| 196 | * @return array The header stock mappings |
||
| 197 | */ |
||
| 198 | 1 | public function getHeaderStockMappings() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Return's the visibility key for the passed visibility string. |
||
| 205 | * |
||
| 206 | * @param string $visibility The visibility string to return the key for |
||
| 207 | * |
||
| 208 | * @return integer The requested visibility key |
||
| 209 | * @throws \Exception Is thrown, if the requested visibility is not available |
||
| 210 | */ |
||
| 211 | public function getVisibilityIdByValue($visibility) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Add the entity ID => visibility mapping for the actual entity ID. |
||
| 230 | * |
||
| 231 | * @param string $visibility The visibility of the actual entity to map |
||
| 232 | * |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | public function addEntityIdVisibilityIdMapping($visibility) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return's the visibility for the passed entity ID, if it already has been mapped. The mapping will be created |
||
| 242 | * by calling <code>\TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue</code> which will |
||
| 243 | * be done by the <code>\TechDivision\Import\Product\Callbacks\VisibilityCallback</code>. |
||
| 244 | * |
||
| 245 | * @return integer The visibility ID |
||
| 246 | * @throws \Exception Is thrown, if the entity ID has not been mapped |
||
| 247 | * @see \TechDivision\Import\Product\Subjects\BunchSubject::getVisibilityIdByValue() |
||
| 248 | */ |
||
| 249 | View Code Duplication | public function getEntityIdVisibilityIdMapping() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Add the passed category ID to the product's category list. |
||
| 267 | * |
||
| 268 | * @param integer $categoryId The category ID to add |
||
| 269 | * |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | public function addProductCategoryId($categoryId) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Pre-load the entity ID for the passed product. |
||
| 279 | * |
||
| 280 | * @param array $product The product to be pre-loaded |
||
| 281 | * |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | public function preLoadEntityId(array $product) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Return's the list with category IDs the product is related with. |
||
| 291 | * |
||
| 292 | * @return array The product's category IDs |
||
| 293 | */ |
||
| 294 | public function getProductCategoryIds() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Return's the entity type for the configured entity type code. |
||
| 311 | * |
||
| 312 | * @return array The requested entity type |
||
| 313 | * @throws \Exception Is thrown, if the requested entity type is not available |
||
| 314 | */ |
||
| 315 | 1 | View Code Duplication | public function getEntityType() |
| 330 | |||
| 331 | /** |
||
| 332 | * Return's TRUE, if the passed URL key varchar value IS related with the actual PK. |
||
| 333 | * |
||
| 334 | * @param array $productVarcharAttribute The varchar value to check |
||
| 335 | * |
||
| 336 | * @return boolean TRUE if the URL key is related, else FALSE |
||
| 337 | */ |
||
| 338 | public function isUrlKeyOf(array $productVarcharAttribute) |
||
| 342 | } |
||
| 343 |
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.