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 |
||
45 | class ProductInventoryObserver extends AbstractProductImportObserver implements DynamicAttributeObserverInterface, StateDetectorAwareObserverInterface, ObserverFactoryInterface |
||
46 | { |
||
47 | |||
48 | /** |
||
49 | * The product bunch processor instance. |
||
50 | * |
||
51 | * @var \TechDivision\Import\Product\Services\ProductBunchProcessorInterface |
||
52 | */ |
||
53 | protected $productBunchProcessor; |
||
54 | |||
55 | /** |
||
56 | * The attribute loader instance. |
||
57 | * |
||
58 | * @var \TechDivision\Import\Observers\AttributeLoaderInterface |
||
59 | */ |
||
60 | protected $attributeLoader; |
||
61 | |||
62 | /** |
||
63 | * The entity merger instance. |
||
64 | * |
||
65 | * @var \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface |
||
66 | */ |
||
67 | protected $entityMerger; |
||
68 | |||
69 | /** |
||
70 | * The array with the column mappings that has to be computed. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $columns = array(); |
||
75 | |||
76 | /** |
||
77 | * Initialize the observer with the passed product bunch processor instance. |
||
78 | * |
||
79 | * @param \TechDivision\Import\Product\Services\ProductBunchProcessorInterface $productBunchProcessor The product bunch processor instance |
||
80 | * @param \TechDivision\Import\Observers\AttributeLoaderInterface $attributeLoader The attribute loader instance |
||
81 | * @param \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface|null $entityMerger The entity merger instance |
||
82 | * @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance to use |
||
83 | */ |
||
84 | 2 | View Code Duplication | public function __construct( |
99 | |||
100 | /** |
||
101 | * Will be invoked by the observer visitor when a factory has been defined to create the observer instance. |
||
102 | * |
||
103 | * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
||
104 | * |
||
105 | * @return \TechDivision\Import\Observers\ObserverInterface The observer instance |
||
106 | */ |
||
107 | public function createObserver(SubjectInterface $subject) |
||
124 | |||
125 | /** |
||
126 | * Returns an array of the columns with their types to detect state. |
||
127 | * |
||
128 | * @return array The array with the column names as key and their type as value |
||
129 | */ |
||
130 | public function getColumns() |
||
134 | |||
135 | /** |
||
136 | * Return's the product bunch processor instance. |
||
137 | * |
||
138 | * @return \TechDivision\Import\Product\Services\ProductBunchProcessorInterface The product bunch processor instance |
||
139 | */ |
||
140 | 1 | protected function getProductBunchProcessor() |
|
144 | |||
145 | /** |
||
146 | * Process the observer's business logic. |
||
147 | * |
||
148 | * @return array The processed row |
||
149 | */ |
||
150 | 2 | protected function process() |
|
163 | |||
164 | /** |
||
165 | * Prepare the basic attributes of the stock status/item entity that has to be persisted. |
||
166 | * |
||
167 | * @return array The prepared stock status/item attributes |
||
168 | */ |
||
169 | 1 | protected function prepareAttributes() |
|
189 | |||
190 | /** |
||
191 | * Merge's and return's the entity with the passed attributes and set's the |
||
192 | * passed status. |
||
193 | * |
||
194 | * @param array $entity The entity to merge the attributes into |
||
195 | * @param array $attr The attributes to be merged |
||
196 | * @param string|null $changeSetName The change set name to use |
||
197 | * |
||
198 | * @return array The merged entity |
||
199 | * @todo https://github.com/techdivision/import/issues/179 |
||
200 | */ |
||
201 | View Code Duplication | protected function mergeEntity(array $entity, array $attr, $changeSetName = null) |
|
209 | |||
210 | /** |
||
211 | * Prepare the stock item attributes of the entity that has to be persisted. |
||
212 | * |
||
213 | * @return array The prepared stock status item |
||
214 | */ |
||
215 | 1 | protected function prepareStockItemAttributes() |
|
219 | |||
220 | /** |
||
221 | * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values. |
||
222 | * |
||
223 | * @param array $data An array with data that will be used to initialize the raw entity with |
||
224 | * |
||
225 | * @return array The initialized entity |
||
226 | */ |
||
227 | 1 | protected function loadRawEntity(array $data = array()) |
|
231 | |||
232 | /** |
||
233 | * Initialize the stock item with the passed attributes and returns an instance. |
||
234 | * |
||
235 | * @param array $attr The stock item attributes |
||
236 | * |
||
237 | * @return array The initialized stock item |
||
238 | */ |
||
239 | 1 | protected function initializeStockItem(array $attr) |
|
243 | |||
244 | /** |
||
245 | * Return's the appings for the table column => CSV column header. |
||
246 | * |
||
247 | * @return array The header stock mappings |
||
248 | */ |
||
249 | 1 | protected function getHeaderStockMappings() |
|
253 | |||
254 | /** |
||
255 | * Persist's the passed stock item data and return's the ID. |
||
256 | * |
||
257 | * @param array $stockItem The stock item data to persist |
||
258 | * |
||
259 | * @return void |
||
260 | */ |
||
261 | 1 | protected function persistStockItem($stockItem) |
|
265 | } |
||
266 |
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.