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 |
||
21 | class ProductGateway extends AbstractGateway |
||
22 | { |
||
23 | /** |
||
24 | * @param string $extId |
||
25 | * @param string $token |
||
26 | * @return GroupEntity |
||
27 | */ |
||
28 | 2 | public function getProductGroupsWithProducts($extId, $token) |
|
29 | { |
||
30 | 2 | $response = $this->fetchDocument( |
|
31 | 2 | '/v4/installations/' . $extId . '/product-groups?with=products', |
|
32 | 2 | $token, |
|
33 | 'listGroupsWithProducts' |
||
34 | 2 | ); |
|
35 | |||
36 | 2 | foreach($response as &$group) { |
|
37 | foreach($group['products'] as &$product) { |
||
38 | $product = ProductEntity::make($product); |
||
39 | } |
||
40 | $group = GroupEntity::make($group); |
||
41 | 2 | } |
|
42 | |||
43 | 2 | return $response; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * @author WN |
||
48 | * @param string $installation |
||
49 | * @param string $product |
||
50 | * @param string $token |
||
51 | * @param array $params |
||
52 | * @return array |
||
53 | */ |
||
54 | public function getCreditInfo($installation, $product, $token, array $params) |
||
63 | |||
64 | /** |
||
65 | * @param string $extId |
||
66 | * @param string $productGroup |
||
67 | * @param string $token |
||
68 | * @return array |
||
69 | * @author SL |
||
70 | */ |
||
71 | View Code Duplication | public function getProductsInGroup($extId, $productGroup, $token) |
|
87 | |||
88 | /** |
||
89 | * author EA |
||
90 | * @param string $installation |
||
91 | * @param string $token |
||
92 | * @param array $params |
||
93 | * @return array |
||
94 | */ |
||
95 | public function orderProducts($installation, $token, array $params) |
||
104 | |||
105 | /** |
||
106 | * author EA |
||
107 | * @param string $installation |
||
108 | * @param string $token |
||
109 | * @return array |
||
110 | */ |
||
111 | View Code Duplication | public function getProducts($installation, $token) |
|
127 | } |
||
128 |
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.