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 |
||
43 | class MediaGalleryValueObserver extends AbstractProductImportObserver implements DynamicAttributeObserverInterface |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * The product media processor instance. |
||
48 | * |
||
49 | * @var \TechDivision\Import\Product\Media\Services\ProductMediaProcessorInterface |
||
50 | */ |
||
51 | protected $productMediaProcessor; |
||
52 | |||
53 | /** |
||
54 | * The attribute loader instance. |
||
55 | * |
||
56 | * @var \TechDivision\Import\Observers\AttributeLoaderInterface |
||
57 | */ |
||
58 | protected $attributeLoader; |
||
59 | |||
60 | /** |
||
61 | * Initialize the "dymanmic" columns. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $columns = array( |
||
66 | MemberNames::DISABLED => array(ColumnKeys::HIDE_FROM_PRODUCT_PAGE, BackendTypeKeys::BACKEND_TYPE_INT) |
||
67 | ); |
||
68 | |||
69 | /** |
||
70 | * Array with virtual column name mappings (this is a temporary |
||
71 | * solution till techdivision/import#179 as been implemented). |
||
72 | * |
||
73 | * @var array |
||
74 | * @todo https://github.com/techdivision/import/issues/179 |
||
75 | */ |
||
76 | protected $virtualMapping = array( |
||
77 | MemberNames::LABEL => ColumnKeys::IMAGE_LABEL, |
||
78 | MemberNames::POSITION => ColumnKeys::IMAGE_POSITION, |
||
79 | MemberNames::DISABLED => ColumnKeys::HIDE_FROM_PRODUCT_PAGE |
||
80 | ); |
||
81 | |||
82 | /** |
||
83 | * Initialize the observer with the passed product media processor instance. |
||
84 | * |
||
85 | * @param \TechDivision\Import\Product\Media\Services\ProductMediaProcessorInterface $productMediaProcessor The product media processor instance |
||
86 | * @param \TechDivision\Import\Observers\AttributeLoaderInterface $attributeLoader The attribute loader instance |
||
87 | * @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance to use |
||
88 | */ |
||
89 | View Code Duplication | public function __construct( |
|
102 | |||
103 | /** |
||
104 | * Return's the product media processor instance. |
||
105 | * |
||
106 | * @return \TechDivision\Import\Product\Media\Services\ProductMediaProcessorInterface The product media processor instance |
||
107 | */ |
||
108 | protected function getProductMediaProcessor() |
||
112 | |||
113 | /** |
||
114 | * Query whether or not a value for the column with the passed name exists. |
||
115 | * |
||
116 | * @param string $name The column name to query for a valid value |
||
117 | * |
||
118 | * @return boolean TRUE if the value is set, else FALSE |
||
119 | * @todo https://github.com/techdivision/import/issues/179 |
||
120 | */ |
||
121 | public function hasValue($name) |
||
125 | |||
126 | /** |
||
127 | * Process the observer's business logic. |
||
128 | * |
||
129 | * @return array The processed row |
||
130 | */ |
||
131 | protected function process() |
||
139 | |||
140 | /** |
||
141 | * Appends the dynamic to the static attributes for the media type |
||
142 | * gallery attributes and returns them. |
||
143 | * |
||
144 | * @return array The array with all available attributes |
||
145 | */ |
||
146 | protected function prepareDynamicAttributes() |
||
150 | |||
151 | /** |
||
152 | * Prepare the product media gallery value that has to be persisted. |
||
153 | * |
||
154 | * @return array The prepared product media gallery value attributes |
||
155 | */ |
||
156 | protected function prepareAttributes() |
||
191 | |||
192 | /** |
||
193 | * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values. |
||
194 | * |
||
195 | * @param array $data An array with data that will be used to initialize the raw entity with |
||
196 | * |
||
197 | * @return array The initialized entity |
||
198 | */ |
||
199 | protected function loadRawEntity(array $data = array()) |
||
203 | |||
204 | /** |
||
205 | * Initialize the product media gallery value with the passed attributes and returns an instance. |
||
206 | * |
||
207 | * @param array $attr The product media gallery value attributes |
||
208 | * |
||
209 | * @return array The initialized product media gallery value |
||
210 | */ |
||
211 | protected function initializeProductMediaGalleryValue(array $attr) |
||
215 | |||
216 | /** |
||
217 | * Return's the store ID of the actual row, or of the default store |
||
218 | * if no store view code is set in the CSV file. |
||
219 | * |
||
220 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
221 | * |
||
222 | * @return integer The ID of the actual store |
||
223 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
224 | */ |
||
225 | protected function getRowStoreId($default = null) |
||
229 | |||
230 | /** |
||
231 | * Map's the passed SKU of the parent product to it's PK. |
||
232 | * |
||
233 | * @param string $parentSku The SKU of the parent product |
||
234 | * |
||
235 | * @return integer The primary key used to create relations |
||
236 | */ |
||
237 | protected function mapParentSku($parentSku) |
||
241 | |||
242 | /** |
||
243 | * Return the entity ID for the passed SKU. |
||
244 | * |
||
245 | * @param string $sku The SKU to return the entity ID for |
||
246 | * |
||
247 | * @return integer The mapped entity ID |
||
248 | * @throws \Exception Is thrown if the SKU is not mapped yet |
||
249 | */ |
||
250 | protected function mapSkuToEntityId($sku) |
||
254 | |||
255 | /** |
||
256 | * Return's the value ID of the created media gallery entry. |
||
257 | * |
||
258 | * @return integer The ID of the created media gallery entry |
||
259 | */ |
||
260 | protected function getParentValueId() |
||
264 | |||
265 | /** |
||
266 | * Return's the store for the passed store code. |
||
267 | * |
||
268 | * @param string $storeCode The store code to return the store for |
||
269 | * |
||
270 | * @return array The requested store |
||
271 | * @throws \Exception Is thrown, if the requested store is not available |
||
272 | */ |
||
273 | protected function getStoreByStoreCode($storeCode) |
||
277 | |||
278 | /** |
||
279 | * Returns the acutal value of the position counter and raise's it by one. |
||
280 | * |
||
281 | * @return integer The actual value of the position counter |
||
282 | * @deprecated Since 23.0.0 |
||
283 | */ |
||
284 | protected function raisePositionCounter() |
||
288 | |||
289 | /** |
||
290 | * Persist's the passed product media gallery value data. |
||
291 | * |
||
292 | * @param array $productMediaGalleryValue The product media gallery value data to persist |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | protected function persistProductMediaGalleryValue($productMediaGalleryValue) |
||
300 | } |
||
301 |
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.