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 |
||
37 | class GenericSkuEntityIdMappingObserver extends AbstractProductImportObserver |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * The product bunch processor instance. |
||
42 | * |
||
43 | * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
||
44 | */ |
||
45 | protected $productBunchProcessor; |
||
46 | |||
47 | /** |
||
48 | * The column name with the SKU to map the entity ID for. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $skuColumnName; |
||
53 | |||
54 | /** |
||
55 | * Initialize the observer with the passed product bunch processor instance. |
||
56 | * |
||
57 | * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance |
||
58 | * @param string $skuColumnName The column name with the SKU to map the entity ID for |
||
59 | */ |
||
60 | public function __construct( |
||
67 | |||
68 | /** |
||
69 | * Return's the product bunch processor instance. |
||
70 | * |
||
71 | * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
||
72 | */ |
||
73 | protected function getProductBunchProcessor() |
||
77 | |||
78 | /** |
||
79 | * Returns the column name with the SKU to map the entity ID for. |
||
80 | * |
||
81 | * @return string The column name |
||
82 | */ |
||
83 | protected function getSkuColumnName() |
||
87 | |||
88 | /** |
||
89 | * Process the observer's business logic. |
||
90 | * |
||
91 | * @return void |
||
92 | * @throws \Exception Is thrown, if the product with the SKU can not be loaded |
||
93 | */ |
||
94 | View Code Duplication | protected function process() |
|
121 | |||
122 | /** |
||
123 | * Map the PK for the product with the passed SKU. |
||
124 | * |
||
125 | * @param array $product The product to add the SKU => entity ID mapping for |
||
126 | */ |
||
127 | protected function addSkuPkMapping(array $product) |
||
131 | |||
132 | /** |
||
133 | * Load's and return's the product with the passed SKU. |
||
134 | * |
||
135 | * @param string $sku The SKU of the product to load |
||
136 | * |
||
137 | * @return array The product |
||
138 | */ |
||
139 | protected function loadProduct($sku) |
||
143 | } |
||
144 |
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.