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 ProductObserver 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 | public function handle(array $row) |
||
92 | |||
93 | /** |
||
94 | * Initialize the product with the passed data and returns an instance. |
||
95 | * |
||
96 | * @param string $sku The product's SKU |
||
97 | * @param string $createdAt The product's creation date |
||
98 | * @param string $updatedAt The product's last update date |
||
99 | * @param integer $hasOptions Marks the product to has options |
||
100 | * @param integer $requiredOptions Marks the product that some of the options are required |
||
101 | * @param string $typeId The product's type ID |
||
102 | * @param integer $attributeSetId The product's attribute set ID |
||
103 | * |
||
104 | * @return array The initialized product |
||
105 | */ |
||
106 | 2 | public function initializeProduct( |
|
127 | |||
128 | /** |
||
129 | * Persist's the passed product data and return's the ID. |
||
130 | * |
||
131 | * @param array $product The product data to persist |
||
132 | * |
||
133 | * @return string The ID of the persisted entity |
||
134 | */ |
||
135 | public function persistProduct($product) |
||
139 | |||
140 | /** |
||
141 | * Set's the attribute set of the product that has to be created. |
||
142 | * |
||
143 | * @param array $attributeSet The attribute set |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | public function setAttributeSet(array $attributeSet) |
||
151 | |||
152 | /** |
||
153 | * Return's the attribute set with the passed attribute set name. |
||
154 | * |
||
155 | * @param string $attributeSetName The name of the requested attribute set |
||
156 | * |
||
157 | * @return array The attribute set data |
||
158 | */ |
||
159 | public function getAttributeSetByAttributeSetName($attributeSetName) |
||
163 | |||
164 | /** |
||
165 | * Set's the ID of the product that has been created recently. |
||
166 | * |
||
167 | * @param string $lastEntityId The entity ID |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | public function setLastEntityId($lastEntityId) |
||
175 | } |
||
176 |
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.