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:
Complex classes like BunchSubject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BunchSubject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface |
||
37 | { |
||
38 | |||
39 | /** |
||
40 | * The trait that implements the export functionality. |
||
41 | * |
||
42 | * @var \TechDivision\Import\Subjects\ExportableTrait |
||
43 | */ |
||
44 | use ExportableTrait; |
||
45 | |||
46 | /** |
||
47 | * The mapping for the supported backend types (for the product entity) => persist methods. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $backendTypes = array( |
||
52 | 'datetime' => array('persistProductDatetimeAttribute', 'loadProductDatetimeAttribute'), |
||
53 | 'decimal' => array('persistProductDecimalAttribute', 'loadProductDecimalAttribute'), |
||
54 | 'int' => array('persistProductIntAttribute', 'loadProductIntAttribute'), |
||
55 | 'text' => array('persistProductTextAttribute', 'loadProductTextAttribute'), |
||
56 | 'varchar' => array('persistProductVarcharAttribute', 'loadProductVarcharAttribute') |
||
57 | ); |
||
58 | |||
59 | /** |
||
60 | * Mappings for the table column => CSV column header. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $headerStockMappings = array( |
||
65 | 'qty' => array('qty', 'float'), |
||
66 | 'min_qty' => array('out_of_stock_qty', 'float'), |
||
67 | 'use_config_min_qty' => array('use_config_min_qty', 'int'), |
||
68 | 'is_qty_decimal' => array('is_qty_decimal', 'int'), |
||
69 | 'backorders' => array('allow_backorders', 'int'), |
||
70 | 'use_config_backorders' => array('use_config_backorders', 'int'), |
||
71 | 'min_sale_qty' => array('min_cart_qty', 'float'), |
||
72 | 'use_config_min_sale_qty' => array('use_config_min_sale_qty', 'int'), |
||
73 | 'max_sale_qty' => array('max_cart_qty', 'float'), |
||
74 | 'use_config_max_sale_qty' => array('use_config_max_sale_qty', 'int'), |
||
75 | 'is_in_stock' => array('is_in_stock', 'int'), |
||
76 | 'notify_stock_qty' => array('notify_on_stock_below', 'float'), |
||
77 | 'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'), |
||
78 | 'manage_stock' => array('manage_stock', 'int'), |
||
79 | 'use_config_manage_stock' => array('use_config_manage_stock', 'int'), |
||
80 | 'use_config_qty_increments' => array('use_config_qty_increments', 'int'), |
||
81 | 'qty_increments' => array('qty_increments', 'float'), |
||
82 | 'use_config_enable_qty_inc' => array('use_config_enable_qty_inc', 'int'), |
||
83 | 'enable_qty_increments' => array('enable_qty_increments', 'int'), |
||
84 | 'is_decimal_divided' => array('is_decimal_divided', 'int'), |
||
85 | ); |
||
86 | |||
87 | /** |
||
88 | * The array with the available visibility keys. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $availableVisibilities = array( |
||
93 | 'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE, |
||
94 | 'Catalog' => VisibilityKeys::VISIBILITY_IN_CATALOG, |
||
95 | 'Search' => VisibilityKeys::VISIBILITY_IN_SEARCH, |
||
96 | 'Catalog, Search' => VisibilityKeys::VISIBILITY_BOTH |
||
97 | ); |
||
98 | |||
99 | /** |
||
100 | * The attribute set of the product that has to be created. |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $attributeSet = array(); |
||
105 | |||
106 | /** |
||
107 | * The category IDs the product is related with. |
||
108 | * |
||
109 | * @var array |
||
110 | */ |
||
111 | protected $productCategoryIds = array(); |
||
112 | |||
113 | /** |
||
114 | * Set's the attribute set of the product that has to be created. |
||
115 | * |
||
116 | * @param array $attributeSet The attribute set |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | 1 | public function setAttributeSet(array $attributeSet) |
|
124 | |||
125 | /** |
||
126 | * Return's the attribute set of the product that has to be created. |
||
127 | * |
||
128 | * @return array The attribute set |
||
129 | */ |
||
130 | public function getAttributeSet() |
||
134 | |||
135 | /** |
||
136 | * Cast's the passed value based on the backend type information. |
||
137 | * |
||
138 | * @param string $backendType The backend type to cast to |
||
139 | * @param mixed $value The value to be casted |
||
140 | * |
||
141 | * @return mixed The casted value |
||
142 | */ |
||
143 | 1 | public function castValueByBackendType($backendType, $value) |
|
164 | |||
165 | /** |
||
166 | * Return's the mappings for the table column => CSV column header. |
||
167 | * |
||
168 | * @return array The header stock mappings |
||
169 | */ |
||
170 | 1 | public function getHeaderStockMappings() |
|
174 | |||
175 | /** |
||
176 | * Return's mapping for the supported backend types (for the product entity) => persist methods. |
||
177 | * |
||
178 | * @return array The mapping for the supported backend types |
||
179 | */ |
||
180 | public function getBackendTypes() |
||
184 | |||
185 | /** |
||
186 | * Return's the visibility key for the passed visibility string. |
||
187 | * |
||
188 | * @param string $visibility The visibility string to return the key for |
||
189 | * |
||
190 | * @return integer The requested visibility key |
||
191 | * @throws \Exception Is thrown, if the requested visibility is not available |
||
192 | */ |
||
193 | View Code Duplication | public function getVisibilityIdByValue($visibility) |
|
211 | |||
212 | /** |
||
213 | * Add the passed category ID to the product's category list. |
||
214 | * |
||
215 | * @param integer $categoryId The category ID to add |
||
216 | * |
||
217 | * @return void |
||
218 | */ |
||
219 | public function addProductCategoryId($categoryId) |
||
223 | |||
224 | /** |
||
225 | * Return's the list with category IDs the product is related with. |
||
226 | * |
||
227 | * @return array The product's category IDs |
||
228 | */ |
||
229 | public function getProductCategoryIds() |
||
243 | |||
244 | /** |
||
245 | * Return's the attribute option value with the passed value and store ID. |
||
246 | * |
||
247 | * @param mixed $value The option value |
||
248 | * @param integer $storeId The ID of the store |
||
249 | * |
||
250 | * @return array|boolean The attribute option value instance |
||
251 | */ |
||
252 | public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId) |
||
256 | |||
257 | /** |
||
258 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
259 | * |
||
260 | * @param string $entityType The entity type to load the URL rewrites for |
||
261 | * @param integer $entityId The entity ID to laod the rewrites for |
||
262 | * |
||
263 | * @return array The URL rewrites |
||
264 | */ |
||
265 | 1 | public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId) |
|
269 | |||
270 | /** |
||
271 | * Load's and return's the product with the passed SKU. |
||
272 | * |
||
273 | * @param string $sku The SKU of the product to load |
||
274 | * |
||
275 | * @return array The product |
||
276 | */ |
||
277 | public function loadProduct($sku) |
||
281 | |||
282 | /** |
||
283 | * Load's and return's the product website relation with the passed product and website ID. |
||
284 | * |
||
285 | * @param string $productId The product ID of the relation |
||
286 | * @param string $websiteId The website ID of the relation |
||
287 | * |
||
288 | * @return array The product website |
||
289 | */ |
||
290 | public function loadProductWebsite($productId, $websiteId) |
||
294 | |||
295 | /** |
||
296 | * Return's the category product relation with the passed category/product ID. |
||
297 | * |
||
298 | * @param integer $categoryId The category ID of the category product relation to return |
||
299 | * @param integer $productId The product ID of the category product relation to return |
||
300 | * |
||
301 | * @return array The category product relation |
||
302 | */ |
||
303 | public function loadCategoryProduct($categoryId, $productId) |
||
307 | |||
308 | /** |
||
309 | * Load's and return's the stock status with the passed product/website/stock ID. |
||
310 | * |
||
311 | * @param integer $productId The product ID of the stock status to load |
||
312 | * @param integer $websiteId The website ID of the stock status to load |
||
313 | * @param integer $stockId The stock ID of the stock status to load |
||
314 | * |
||
315 | * @return array The stock status |
||
316 | */ |
||
317 | public function loadStockStatus($productId, $websiteId, $stockId) |
||
321 | |||
322 | /** |
||
323 | * Load's and return's the stock status with the passed product/website/stock ID. |
||
324 | * |
||
325 | * @param integer $productId The product ID of the stock item to load |
||
326 | * @param integer $websiteId The website ID of the stock item to load |
||
327 | * @param integer $stockId The stock ID of the stock item to load |
||
328 | * |
||
329 | * @return array The stock item |
||
330 | */ |
||
331 | public function loadStockItem($productId, $websiteId, $stockId) |
||
335 | |||
336 | /** |
||
337 | * Load's and return's the datetime attribute with the passed entity/attribute/store ID. |
||
338 | * |
||
339 | * @param integer $entityId The entity ID of the attribute |
||
340 | * @param integer $attributeId The attribute ID of the attribute |
||
341 | * @param integer $storeId The store ID of the attribute |
||
342 | * |
||
343 | * @return array|null The datetime attribute |
||
344 | */ |
||
345 | public function loadProductDatetimeAttribute($entityId, $attributeId, $storeId) |
||
349 | |||
350 | /** |
||
351 | * Load's and return's the decimal attribute with the passed entity/attribute/store ID. |
||
352 | * |
||
353 | * @param integer $entityId The entity ID of the attribute |
||
354 | * @param integer $attributeId The attribute ID of the attribute |
||
355 | * @param integer $storeId The store ID of the attribute |
||
356 | * |
||
357 | * @return array|null The decimal attribute |
||
358 | */ |
||
359 | public function loadProductDecimalAttribute($entityId, $attributeId, $storeId) |
||
363 | |||
364 | /** |
||
365 | * Load's and return's the integer attribute with the passed entity/attribute/store ID. |
||
366 | * |
||
367 | * @param integer $entityId The entity ID of the attribute |
||
368 | * @param integer $attributeId The attribute ID of the attribute |
||
369 | * @param integer $storeId The store ID of the attribute |
||
370 | * |
||
371 | * @return array|null The integer attribute |
||
372 | */ |
||
373 | public function loadProductIntAttribute($entityId, $attributeId, $storeId) |
||
377 | |||
378 | /** |
||
379 | * Load's and return's the text attribute with the passed entity/attribute/store ID. |
||
380 | * |
||
381 | * @param integer $entityId The entity ID of the attribute |
||
382 | * @param integer $attributeId The attribute ID of the attribute |
||
383 | * @param integer $storeId The store ID of the attribute |
||
384 | * |
||
385 | * @return array|null The text attribute |
||
386 | */ |
||
387 | public function loadProductTextAttribute($entityId, $attributeId, $storeId) |
||
391 | |||
392 | /** |
||
393 | * Load's and return's the varchar attribute with the passed entity/attribute/store ID. |
||
394 | * |
||
395 | * @param integer $entityId The entity ID of the attribute |
||
396 | * @param integer $attributeId The attribute ID of the attribute |
||
397 | * @param integer $storeId The store ID of the attribute |
||
398 | * |
||
399 | * @return array|null The varchar attribute |
||
400 | */ |
||
401 | public function loadProductVarcharAttribute($entityId, $attributeId, $storeId) |
||
405 | |||
406 | /** |
||
407 | * Persist's the passed product data and return's the ID. |
||
408 | * |
||
409 | * @param array $product The product data to persist |
||
410 | * |
||
411 | * @return string The ID of the persisted entity |
||
412 | */ |
||
413 | public function persistProduct($product) |
||
417 | |||
418 | /** |
||
419 | * Persist's the passed product varchar attribute. |
||
420 | * |
||
421 | * @param array $attribute The attribute to persist |
||
422 | * |
||
423 | * @return void |
||
424 | */ |
||
425 | public function persistProductVarcharAttribute($attribute) |
||
429 | |||
430 | /** |
||
431 | * Persist's the passed product integer attribute. |
||
432 | * |
||
433 | * @param array $attribute The attribute to persist |
||
434 | * |
||
435 | * @return void |
||
436 | */ |
||
437 | public function persistProductIntAttribute($attribute) |
||
441 | |||
442 | /** |
||
443 | * Persist's the passed product decimal attribute. |
||
444 | * |
||
445 | * @param array $attribute The attribute to persist |
||
446 | * |
||
447 | * @return void |
||
448 | */ |
||
449 | public function persistProductDecimalAttribute($attribute) |
||
453 | |||
454 | /** |
||
455 | * Persist's the passed product datetime attribute. |
||
456 | * |
||
457 | * @param array $attribute The attribute to persist |
||
458 | * |
||
459 | * @return void |
||
460 | */ |
||
461 | public function persistProductDatetimeAttribute($attribute) |
||
465 | |||
466 | /** |
||
467 | * Persist's the passed product text attribute. |
||
468 | * |
||
469 | * @param array $attribute The attribute to persist |
||
470 | * |
||
471 | * @return void |
||
472 | */ |
||
473 | public function persistProductTextAttribute($attribute) |
||
477 | |||
478 | /** |
||
479 | * Persist's the passed product website data and return's the ID. |
||
480 | * |
||
481 | * @param array $productWebsite The product website data to persist |
||
482 | * |
||
483 | * @return void |
||
484 | */ |
||
485 | public function persistProductWebsite($productWebsite) |
||
489 | |||
490 | /** |
||
491 | * Persist's the passed category product relation. |
||
492 | * |
||
493 | * @param array $categoryProduct The category product relation to persist |
||
494 | * |
||
495 | * @return void |
||
496 | */ |
||
497 | public function persistCategoryProduct($categoryProduct) |
||
501 | |||
502 | /** |
||
503 | * Persist's the passed stock item data and return's the ID. |
||
504 | * |
||
505 | * @param array $stockItem The stock item data to persist |
||
506 | * |
||
507 | * @return void |
||
508 | */ |
||
509 | public function persistStockItem($stockItem) |
||
513 | |||
514 | /** |
||
515 | * Persist's the passed stock status data and return's the ID. |
||
516 | * |
||
517 | * @param array $stockStatus The stock status data to persist |
||
518 | * |
||
519 | * @return void |
||
520 | */ |
||
521 | public function persistStockStatus($stockStatus) |
||
525 | |||
526 | /** |
||
527 | * Persist's the URL write with the passed data. |
||
528 | * |
||
529 | * @param array $row The URL rewrite to persist |
||
530 | * |
||
531 | * @return void |
||
532 | */ |
||
533 | 1 | public function persistUrlRewrite($row) |
|
537 | |||
538 | /** |
||
539 | * Delete's the entity with the passed attributes. |
||
540 | * |
||
541 | * @param array $row The attributes of the entity to delete |
||
542 | * @param string|null $name The name of the prepared statement that has to be executed |
||
543 | * |
||
544 | * @return void |
||
545 | */ |
||
546 | public function deleteProduct($row, $name = null) |
||
550 | |||
551 | /** |
||
552 | * Delete's the URL rewrite(s) with the passed attributes. |
||
553 | * |
||
554 | * @param array $row The attributes of the entity to delete |
||
555 | * @param string|null $name The name of the prepared statement that has to be executed |
||
556 | * |
||
557 | * @return void |
||
558 | */ |
||
559 | 1 | public function deleteUrlRewrite($row, $name = null) |
|
563 | |||
564 | /** |
||
565 | * Delete's the stock item(s) with the passed attributes. |
||
566 | * |
||
567 | * @param array $row The attributes of the entity to delete |
||
568 | * @param string|null $name The name of the prepared statement that has to be executed |
||
569 | * |
||
570 | * @return void |
||
571 | */ |
||
572 | public function deleteStockItem($row, $name = null) |
||
576 | |||
577 | /** |
||
578 | * Delete's the stock status with the passed attributes. |
||
579 | * |
||
580 | * @param array $row The attributes of the entity to delete |
||
581 | * @param string|null $name The name of the prepared statement that has to be executed |
||
582 | * |
||
583 | * @return void |
||
584 | */ |
||
585 | public function deleteStockStatus($row, $name = null) |
||
589 | |||
590 | /** |
||
591 | * Delete's the product website relations with the passed attributes. |
||
592 | * |
||
593 | * @param array $row The attributes of the entity to delete |
||
594 | * @param string|null $name The name of the prepared statement that has to be executed |
||
595 | * |
||
596 | * @return void |
||
597 | */ |
||
598 | public function deleteProductWebsite($row, $name = null) |
||
602 | |||
603 | /** |
||
604 | * Delete's the category product relations with the passed attributes. |
||
605 | * |
||
606 | * @param array $row The attributes of the entity to delete |
||
607 | * @param string|null $name The name of the prepared statement that has to be executed |
||
608 | * |
||
609 | * @return void |
||
610 | */ |
||
611 | public function deleteCategoryProduct($row, $name = null) |
||
615 | } |
||
616 |
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.