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 |
||
18 | class StockbaseStockManagement |
||
19 | { |
||
20 | /** |
||
21 | * @var StockRegistryInterface |
||
22 | */ |
||
23 | private $stockRegistry; |
||
24 | /** |
||
25 | * @var StockbaseConfiguration |
||
26 | */ |
||
27 | private $config; |
||
28 | /** |
||
29 | * @var ProductFactory |
||
30 | */ |
||
31 | private $productFactory; |
||
32 | /** |
||
33 | * @var StockItemResource |
||
34 | */ |
||
35 | private $stockItemResource; |
||
36 | /** |
||
37 | * @var ObjectManagerInterface |
||
38 | */ |
||
39 | private $objectManager; |
||
40 | |||
41 | /** |
||
42 | * StockbaseStockManagement constructor. |
||
43 | * @param StockRegistryInterface $stockRegistry |
||
44 | * @param StockbaseConfiguration $config |
||
45 | * @param ProductFactory $productFactory |
||
46 | * @param StockItemResource $stockItemResource |
||
47 | * @param ObjectManagerInterface $objectManager |
||
48 | */ |
||
49 | 9 | public function __construct( |
|
63 | |||
64 | /** |
||
65 | * Gets the amount of items available in the Stockbase stock. |
||
66 | * |
||
67 | * @param int $productId |
||
68 | * @return float |
||
69 | */ |
||
70 | 1 | public function getStockbaseStockAmount($productId) |
|
81 | |||
82 | /** |
||
83 | * Gets the Stockbase EAN for given product (if any). |
||
84 | * |
||
85 | * @param int $productId |
||
86 | * @return string|null |
||
87 | */ |
||
88 | 4 | public function getStockbaseEan($productId) |
|
106 | |||
107 | /** |
||
108 | * Checks if given product is properly configured to be processed as a Stockbase product. |
||
109 | * |
||
110 | * @param int $productId |
||
111 | * @return bool |
||
112 | */ |
||
113 | 1 | public function isStockbaseProduct($productId) |
|
117 | |||
118 | /** |
||
119 | * Increments/decrements the amount of items in stock. |
||
120 | * |
||
121 | * @param string $ean |
||
122 | * @param float $amount |
||
123 | * @param string $operation |
||
124 | */ |
||
125 | 2 | public function updateStockAmount($ean, $amount, $operation = '-') |
|
129 | |||
130 | /** |
||
131 | * Creates a stock reserve for given item. |
||
132 | * |
||
133 | * @param QuoteItem $quoteItem |
||
134 | * @param float $stockbaseAmount |
||
135 | * @param float $magentoStockAmount |
||
136 | * @return StockItemReserve |
||
137 | */ |
||
138 | 1 | public function createReserve(QuoteItem $quoteItem, $stockbaseAmount, $magentoStockAmount) |
|
156 | |||
157 | /** |
||
158 | * Releases the stock reserve. |
||
159 | * |
||
160 | * @param StockItemReserve $reserve |
||
161 | */ |
||
162 | 1 | public function releaseReserve(StockItemReserve $reserve) |
|
166 | |||
167 | /** |
||
168 | * Gets stock reserve entries for given products. |
||
169 | * |
||
170 | * @param int|int[] $productIds |
||
171 | * @return StockItemReserve[] |
||
172 | */ |
||
173 | 1 | View Code Duplication | public function getReserveForProduct($productIds) |
186 | |||
187 | /** |
||
188 | * Gets stock reserve entries for given quote items. |
||
189 | * |
||
190 | * @param int|int[] $quoteItemIds |
||
191 | * @return StockItemReserve[] |
||
192 | */ |
||
193 | 1 | View Code Duplication | public function getReserveForQuoteItem($quoteItemIds) |
206 | } |
||
207 |
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.