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 |
||
35 | class PreLoadEntityIdObserver extends AbstractProductImportObserver |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * The product bunch processor instance. |
||
40 | * |
||
41 | * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
||
42 | */ |
||
43 | protected $productBunchProcessor; |
||
44 | |||
45 | /** |
||
46 | * Initialize the observer with the passed product bunch processor instance. |
||
47 | * |
||
48 | * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance |
||
49 | */ |
||
50 | 3 | public function __construct(ProductBunchProcessorInterface $productBunchProcessor) |
|
54 | |||
55 | /** |
||
56 | * Return's the product bunch processor instance. |
||
57 | * |
||
58 | * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
||
59 | */ |
||
60 | 2 | protected function getProductBunchProcessor() |
|
64 | |||
65 | /** |
||
66 | * Process the observer's business logic. |
||
67 | * |
||
68 | * @return array The processed row |
||
69 | * @throws \Exception Is thrown, if the product with the SKU can not be loaded |
||
70 | */ |
||
71 | 3 | View Code Duplication | protected function process() |
95 | |||
96 | /** |
||
97 | * Pre-load the entity ID for the passed product. |
||
98 | * |
||
99 | * @param array $product The product to be pre-loaded |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | 1 | protected function preLoadEntityId(array $product) |
|
107 | |||
108 | /** |
||
109 | * Load's and return's the product with the passed SKU. |
||
110 | * |
||
111 | * @param string $sku The SKU of the product to load |
||
112 | * |
||
113 | * @return array The product |
||
114 | */ |
||
115 | 2 | protected function loadProduct($sku) |
|
119 | } |
||
120 |
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.