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 |
||
9 | class Product extends Endpoint |
||
10 | { |
||
11 | /** |
||
12 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/GetProduct |
||
13 | * |
||
14 | * @param string $productNumber |
||
15 | * @param int $siteId |
||
16 | * @return array |
||
17 | */ |
||
18 | View Code Duplication | public function getProduct(string $productNumber, int $siteId) : array |
|
28 | |||
29 | /** |
||
30 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/GetProductsInCategory |
||
31 | * |
||
32 | * @param int $categoryId |
||
33 | * @param int $siteId |
||
34 | * @return array |
||
35 | */ |
||
36 | View Code Duplication | public function getProductsInCategory(int $categoryId, int $siteId) : array |
|
46 | |||
47 | /** |
||
48 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/GetCategories |
||
49 | * |
||
50 | * @param int $siteId |
||
51 | * @return array |
||
52 | */ |
||
53 | public function getCategories(int $siteId) : array |
||
62 | |||
63 | /** |
||
64 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/GetSubCategories |
||
65 | * |
||
66 | * @param int $categoryId |
||
67 | * @param int $siteId |
||
68 | * @return array |
||
69 | */ |
||
70 | View Code Duplication | public function getSubCategories(int $categoryId, int $siteId) : array |
|
80 | |||
81 | /** |
||
82 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/GetProductByMetadata |
||
83 | * |
||
84 | * @param string $productNumber |
||
85 | * @param array $context |
||
86 | * @return array |
||
87 | */ |
||
88 | public function getProductByMetadata(string $productNumber, array $context) : array |
||
99 | |||
100 | /** |
||
101 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/GetProductsInCategoryByMetadata |
||
102 | * |
||
103 | * @param int $categoryId |
||
104 | * @param array $context |
||
105 | * @return array |
||
106 | */ |
||
107 | public function getProductsInCategoryByMetadata(int $categoryId, array $context) : array |
||
118 | |||
119 | /** |
||
120 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/GetCategory |
||
121 | * |
||
122 | * @param int $categoryId |
||
123 | * @param int $siteId |
||
124 | * @return array |
||
125 | */ |
||
126 | View Code Duplication | public function getCategory(int $categoryId, int $siteId) : array |
|
136 | |||
137 | /** |
||
138 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/FindProductNumbersByKeyword |
||
139 | * |
||
140 | * @param string $keyword |
||
141 | * @param int $siteId |
||
142 | * @return array |
||
143 | */ |
||
144 | View Code Duplication | public function findProductNumbersByKeyword(string $keyword, int $siteId) : array |
|
158 | |||
159 | /** |
||
160 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/GetProductsByBarcode |
||
161 | * |
||
162 | * @param string $barCode |
||
163 | * @param int $siteId |
||
164 | * @return array |
||
165 | */ |
||
166 | View Code Duplication | public function getProductsByBarcode(string $barCode, int $siteId) : array |
|
176 | |||
177 | /** |
||
178 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/GetProductsByModificationDate |
||
179 | * |
||
180 | * @param \DateTimeInterface $modificationDate |
||
181 | * @param int $siteId |
||
182 | * @return array |
||
183 | */ |
||
184 | public function getProductsByModificationDate(\DateTimeInterface $modificationDate, int $siteId) : array |
||
193 | |||
194 | /** |
||
195 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/FindProductsByProductNumbers |
||
196 | * |
||
197 | * @param int $siteId |
||
198 | * @param array $productNumbers |
||
199 | * @param int $page |
||
200 | * @param int $pageSize |
||
201 | * @return array |
||
202 | */ |
||
203 | public function findProductsByProductNumbers(int $siteId, array $productNumbers, int $page = 1, int $pageSize = 100) : array |
||
223 | |||
224 | /** |
||
225 | * @see http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductService/help/operations/GetProductsInModifiedInterval |
||
226 | * |
||
227 | * @param int $siteId |
||
228 | * @param \DateTimeInterface $dateStart |
||
229 | * @param \DateTimeInterface $dateEnd |
||
230 | * @return array |
||
231 | */ |
||
232 | View Code Duplication | public function getProductsInModifiedInterval(int $siteId, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd) : array |
|
247 | } |
||
248 |
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.