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 |
||
36 | class ProductInventoryObserver extends AbstractProductImportObserver |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * Will be invoked by the action on the events the listener has been registered for. |
||
41 | * |
||
42 | * @param array $row The row to handle |
||
43 | * |
||
44 | * @return array The modified row |
||
45 | * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle() |
||
46 | */ |
||
47 | View Code Duplication | public function handle(array $row) |
|
64 | |||
65 | /** |
||
66 | * Process the observer's business logic. |
||
67 | * |
||
68 | * @return array The processed row |
||
69 | */ |
||
70 | public function process() |
||
77 | |||
78 | /** |
||
79 | * Prepare the stock status attributes of the entity that has to be persisted. |
||
80 | * |
||
81 | * @return array The prepared stock status attributes |
||
82 | */ |
||
83 | public function prepareStockStatusAttributes() |
||
104 | |||
105 | /** |
||
106 | * Initialize the stock status with the passed attributes and returns an instance. |
||
107 | * |
||
108 | * @param array $attr The stock status attributes |
||
109 | * |
||
110 | * @return array The initialized stock status |
||
111 | */ |
||
112 | public function initializeStockStatus(array $attr) |
||
116 | |||
117 | /** |
||
118 | * Prepare the stock item attributes of the entity that has to be persisted. |
||
119 | * |
||
120 | * @return array The prepared stock status item |
||
121 | */ |
||
122 | public function prepareStockItemAttributes() |
||
150 | |||
151 | /** |
||
152 | * Initialize the stock item with the passed attributes and returns an instance. |
||
153 | * |
||
154 | * @param array $attr The stock item attributes |
||
155 | * |
||
156 | * @return array The initialized stock item |
||
157 | */ |
||
158 | public function initializeStockItem(array $attr) |
||
162 | |||
163 | /** |
||
164 | * Persist's the passed stock item data and return's the ID. |
||
165 | * |
||
166 | * @param array $stockItem The stock item data to persist |
||
167 | * |
||
168 | * @return void |
||
169 | */ |
||
170 | public function persistStockItem($stockItem) |
||
174 | |||
175 | /** |
||
176 | * Persist's the passed stock status data and return's the ID. |
||
177 | * |
||
178 | * @param array $stockStatus The stock status data to persist |
||
179 | * |
||
180 | * @return void |
||
181 | */ |
||
182 | public function persistStockStatus($stockStatus) |
||
186 | |||
187 | /** |
||
188 | * Return's the appings for the table column => CSV column header. |
||
189 | * |
||
190 | * @return array The header stock mappings |
||
191 | */ |
||
192 | public function getHeaderStockMappings() |
||
196 | } |
||
197 |
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.