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 |
||
34 | class ProductInventoryUpdateObserver extends ProductInventoryObserver |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Initialize the stock status with the passed attributes and returns an instance. |
||
39 | * |
||
40 | * @param array $attr The stock status attributes |
||
41 | * |
||
42 | * @return array The initialized stock status |
||
43 | */ |
||
44 | View Code Duplication | public function initializeStockStatus(array $attr) |
|
62 | |||
63 | /** |
||
64 | * Initialize the stock item with the passed attributes and returns an instance. |
||
65 | * |
||
66 | * @param array $attr The stock item attributes |
||
67 | * |
||
68 | * @return array The initialized stock item |
||
69 | */ |
||
70 | View Code Duplication | public function initializeStockItem(array $attr) |
|
88 | |||
89 | /** |
||
90 | * Load's and return's the stock status with the passed product/website/stock ID. |
||
91 | * |
||
92 | * @param integer $productId The product ID of the stock status to load |
||
93 | * @param integer $websiteId The website ID of the stock status to load |
||
94 | * @param integer $stockId The stock ID of the stock status to load |
||
95 | * |
||
96 | * @return array The stock status |
||
97 | */ |
||
98 | public function loadStockStatus($productId, $websiteId, $stockId) |
||
102 | |||
103 | /** |
||
104 | * Load's and return's the stock status with the passed product/website/stock ID. |
||
105 | * |
||
106 | * @param integer $productId The product ID of the stock item to load |
||
107 | * @param integer $websiteId The website ID of the stock item to load |
||
108 | * @param integer $stockId The stock ID of the stock item to load |
||
109 | * |
||
110 | * @return array The stock item |
||
111 | */ |
||
112 | public function loadStockItem($productId, $websiteId, $stockId) |
||
116 | } |
||
117 |
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.