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 |
||
7 | class ProductData extends Endpoint |
||
8 | { |
||
9 | /** |
||
10 | * @param string $productNumber |
||
11 | * @return array |
||
12 | */ |
||
13 | public function getDataProduct(string $productNumber) : array |
||
22 | |||
23 | /** |
||
24 | * @param int $categoryId |
||
25 | * @return array |
||
26 | */ |
||
27 | public function getDataProductsInCategory(int $categoryId) : array |
||
36 | |||
37 | /** |
||
38 | * @param string $barCode |
||
39 | * @return array |
||
40 | */ |
||
41 | public function getDataProductsByBarcode(string $barCode) : array |
||
50 | |||
51 | /** |
||
52 | * @param \DateTimeInterface $date |
||
53 | * @return array |
||
54 | */ |
||
55 | public function getDataProductsByModificationDate(\DateTimeInterface $date) : array |
||
62 | |||
63 | /** |
||
64 | * @param \DateTimeInterface $dateStart |
||
65 | * @param \DateTimeInterface $dateEnd |
||
66 | * @param int $page |
||
67 | * @param int $pageSize |
||
68 | * @return array |
||
69 | */ |
||
70 | public function getDataProductsInModifiedInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, int $page = 1, int $pageSize = 100) : array |
||
87 | |||
88 | /** |
||
89 | * @param \DateTimeInterface $dateStart |
||
90 | * @param \DateTimeInterface $dateEnd |
||
91 | * @return int |
||
92 | */ |
||
93 | public function countByModifiedInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd) : int |
||
106 | |||
107 | /** |
||
108 | * @param array|\stdClass $product |
||
109 | * @return array |
||
110 | */ |
||
111 | public function createProduct($product) : array |
||
115 | |||
116 | /** |
||
117 | * @param string $productNumber |
||
118 | * @param int $stockCount |
||
119 | * @return bool |
||
120 | */ |
||
121 | public function setStockCount(string $productNumber, int $stockCount) : bool |
||
130 | |||
131 | /** |
||
132 | * Will return the stock count for the specified product number |
||
133 | * |
||
134 | * @param string $productNumber |
||
135 | * @return int |
||
136 | */ |
||
137 | public function getStockCount(string $productNumber) : int |
||
144 | |||
145 | /** |
||
146 | * Will increment or decrement the stock count for the given product number |
||
147 | * |
||
148 | * @param string $productNumber |
||
149 | * @param int $amount |
||
150 | * @return array |
||
151 | */ |
||
152 | public function incrementOrDecrementStockCount(string $productNumber, int $amount) : array |
||
166 | |||
167 | /** |
||
168 | * Will increment the stock count for the given product number |
||
169 | * |
||
170 | * @param string $productNumber |
||
171 | * @param int $amount |
||
172 | * @return array |
||
173 | */ |
||
174 | public function incrementStockCount(string $productNumber, int $amount) : array |
||
180 | |||
181 | /** |
||
182 | * Will decrement the stock count for the given product number |
||
183 | * |
||
184 | * @param string $productNumber |
||
185 | * @param int $amount |
||
186 | * @return array |
||
187 | */ |
||
188 | public function decrementStockCount(string $productNumber, int $amount) : array |
||
194 | |||
195 | /** |
||
196 | * @return array |
||
197 | */ |
||
198 | public function getDataCategories() : array |
||
202 | |||
203 | /** |
||
204 | * @param int $categoryId |
||
205 | * @return array |
||
206 | */ |
||
207 | public function getDataSubCategories(int $categoryId) : array |
||
216 | |||
217 | /** |
||
218 | * @return int |
||
219 | */ |
||
220 | public function getProductCount() : int |
||
224 | |||
225 | /** |
||
226 | * @param int $page |
||
227 | * @param int $pageSize |
||
228 | * @return array |
||
229 | */ |
||
230 | View Code Duplication | public function getProductPage(int $page = 1, int $pageSize = 100) : array |
|
240 | |||
241 | /** |
||
242 | * This method will return the number of pages you need to iterate to get the whole catalog using a page size of $pageSize |
||
243 | * If a shop has 10,000 products, a call with $pageSize = 100 will return 10,000 / 100 = 100 |
||
244 | * |
||
245 | * @param int $pageSize |
||
246 | * @return int |
||
247 | */ |
||
248 | public function getProductPageCount(int $pageSize = 100) : int |
||
257 | |||
258 | /** |
||
259 | * @param string $productNumber |
||
260 | * @return boolean |
||
261 | */ |
||
262 | public function deleteProduct(string $productNumber) : bool |
||
271 | |||
272 | /** |
||
273 | * @param array|\stdClass $category |
||
274 | * @return array |
||
275 | */ |
||
276 | public function createCategory($category) : array |
||
280 | |||
281 | /** |
||
282 | * @param int $categoryId |
||
283 | * @return boolean |
||
284 | */ |
||
285 | public function deleteCategory(int $categoryId) : bool |
||
294 | |||
295 | /** |
||
296 | * @param int $categoryId |
||
297 | * @return array |
||
298 | */ |
||
299 | public function getDataCategory(int $categoryId) : array |
||
308 | |||
309 | /** |
||
310 | * @param string $productNumber |
||
311 | * @param array|\stdClass $product |
||
312 | * @return array |
||
313 | */ |
||
314 | public function updateProduct(string $productNumber, $product) : array |
||
324 | |||
325 | /** |
||
326 | * @param string $productNumber |
||
327 | * @param KeyValueCollection $keyValueCollection |
||
328 | * @return array |
||
329 | */ |
||
330 | View Code Duplication | public function patchProduct(string $productNumber, KeyValueCollection $keyValueCollection) : array |
|
341 | |||
342 | /** |
||
343 | * @param string $productNumber |
||
344 | * @param array|\stdClass $price |
||
345 | * @return array |
||
346 | */ |
||
347 | public function createPrice(string $productNumber, $price) : array |
||
357 | |||
358 | /** |
||
359 | * @param string $productNumber |
||
360 | * @param array|\stdClass $price |
||
361 | * @return bool |
||
362 | */ |
||
363 | public function deletePrice(string $productNumber, $price) : bool |
||
373 | |||
374 | /** |
||
375 | * @param string $productNumber |
||
376 | * @return array |
||
377 | */ |
||
378 | public function getPricesForProduct(string $productNumber) : array |
||
387 | |||
388 | /** |
||
389 | * @param int $siteId |
||
390 | * @param string $productNumber |
||
391 | * @param KeyValueCollection $keyValueCollection |
||
392 | * @return array |
||
393 | */ |
||
394 | View Code Duplication | public function patchProductSettings(int $siteId, string $productNumber, KeyValueCollection $keyValueCollection) : array |
|
410 | } |
||
411 |
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.