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 |
||
6 | class Customer extends Endpoint |
||
7 | { |
||
8 | |||
9 | /** |
||
10 | * @see https://shoppartner.dandomain.dk/dokumentation/api-documentation/customer/#GetCustomer_GET |
||
11 | * |
||
12 | * @param int $customerId |
||
13 | * @return array |
||
14 | */ |
||
15 | 1 | public function getCustomer(int $customerId) : array |
|
16 | { |
||
17 | 1 | Assert::that($customerId)->greaterThan(0, 'The $customerId has to be positive'); |
|
18 | |||
19 | return (array)$this->master->doRequest( |
||
20 | 'GET', |
||
21 | sprintf('/admin/WEBAPI/Endpoints/v1_0/CustomerService/{KEY}/%d', $customerId) |
||
22 | ); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * @see https://shoppartner.dandomain.dk/dokumentation/api-documentation/customer/#GetCustomerByEmail_GET |
||
27 | * |
||
28 | * @param string $email |
||
29 | * @return array |
||
30 | */ |
||
31 | View Code Duplication | public function getCustomerByEmail(string $email) : array |
|
40 | |||
41 | /** |
||
42 | * @see https://shoppartner.dandomain.dk/dokumentation/api-documentation/customer/#CreateCustomer_POST |
||
43 | * |
||
44 | * @param array|\stdClass $customer |
||
45 | * @return array |
||
46 | */ |
||
47 | public function createCustomer($customer) : array |
||
51 | |||
52 | /** |
||
53 | * @see https://shoppartner.dandomain.dk/dokumentation/api-documentation/customer/#UpdateCustomer_PUT |
||
54 | * |
||
55 | * @param int $customerId |
||
56 | * @param array|\stdClass $customer |
||
57 | * @return array |
||
58 | */ |
||
59 | public function updateCustomer(int $customerId, $customer) : array |
||
69 | |||
70 | /** |
||
71 | * @see https://shoppartner.dandomain.dk/dokumentation/api-documentation/customer/#DeleteCustomer_DELETE |
||
72 | * |
||
73 | * @param int $customerId |
||
74 | * @return boolean |
||
75 | */ |
||
76 | public function deleteCustomer(int $customerId) : bool |
||
88 | |||
89 | /** |
||
90 | * @see https://shoppartner.dandomain.dk/dokumentation/api-documentation/customer/#GetCustomerGroups_GET |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | public function getCustomerGroups() : array |
||
101 | |||
102 | /** |
||
103 | * @see https://shoppartner.dandomain.dk/dokumentation/api-documentation/customer/#UpdateCustomerDiscountPOST |
||
104 | * |
||
105 | * @param int $customerId |
||
106 | * @param array|\stdClass $customerDiscount |
||
107 | * @return array |
||
108 | */ |
||
109 | public function updateCustomerDiscount(int $customerId, $customerDiscount) : array |
||
119 | |||
120 | /** |
||
121 | * @see https://shoppartner.dandomain.dk/dokumentation/api-documentation/customer/#GetCustomerDiscountGET |
||
122 | * |
||
123 | * @param int $customerId |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getCustomerDiscount(int $customerId) : array |
||
138 | } |
||
139 |
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.