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 BundleOptionObserver extends AbstractProductImportObserver |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * The product bundle processor instance. |
||
43 | * |
||
44 | * @var \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface |
||
45 | */ |
||
46 | protected $productBundleProcessor; |
||
47 | |||
48 | /** |
||
49 | * Initialize the observer with the passed product bundle processor instance. |
||
50 | * |
||
51 | * @param \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface $productBundleProcessor The product bundle processor instance |
||
52 | */ |
||
53 | public function __construct(ProductBundleProcessorInterface $productBundleProcessor) |
||
57 | |||
58 | /** |
||
59 | * Return's the product bundle processor instance. |
||
60 | * |
||
61 | * @return \TechDivision\Import\Product\Bundle\Services\ProductBundleProcessorInterface The product bundle processor instance |
||
62 | */ |
||
63 | protected function getProductBundleProcessor() |
||
67 | |||
68 | /** |
||
69 | * Process the observer's business logic. |
||
70 | * |
||
71 | * @return array The processed row |
||
72 | */ |
||
73 | View Code Duplication | protected function process() |
|
96 | |||
97 | /** |
||
98 | * Prepare the attributes of the entity that has to be persisted. |
||
99 | * |
||
100 | * @return array The prepared attributes |
||
101 | */ |
||
102 | protected function prepareAttributes() |
||
130 | |||
131 | /** |
||
132 | * Initialize the bundle option with the passed attributes and returns an instance. |
||
133 | * |
||
134 | * @param array $attr The bundle option attributes |
||
135 | * |
||
136 | * @return array The initialized bundle option |
||
137 | */ |
||
138 | protected function initializeBundleOption(array $attr) |
||
142 | |||
143 | /** |
||
144 | * Reset the position counter to 1. |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | protected function resetPositionCounter() |
||
152 | |||
153 | /** |
||
154 | * Add's the mapping for the passed name => option ID. |
||
155 | * |
||
156 | * @param string $name The name of the option |
||
157 | * @param integer $optionId The created option ID |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | protected function addNameOptionIdMapping($name, $optionId) |
||
165 | |||
166 | /** |
||
167 | * Query whether or not the option with the passed name has already been created. |
||
168 | * |
||
169 | * @param string $name The option name to query for |
||
170 | * |
||
171 | * @return boolean TRUE if the option already exists, else FALSE |
||
172 | */ |
||
173 | protected function exists($name) |
||
177 | |||
178 | /** |
||
179 | * Return the entity ID for the passed SKU. |
||
180 | * |
||
181 | * @param string $sku The SKU to return the entity ID for |
||
182 | * |
||
183 | * @return integer The mapped entity ID |
||
184 | * @throws \Exception Is thrown if the SKU is not mapped yet |
||
185 | */ |
||
186 | protected function mapSku($sku) |
||
190 | |||
191 | /** |
||
192 | * Persist's the passed product bundle option data and return's the ID. |
||
193 | * |
||
194 | * @param array $productBundleOption The product bundle option data to persist |
||
195 | * |
||
196 | * @return string The ID of the persisted entity |
||
197 | */ |
||
198 | protected function persistProductBundleOption($productBundleOption) |
||
202 | } |
||
203 |
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.