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 |
||
11 | class CustomerService extends AbstractService |
||
12 | { |
||
13 | /** |
||
14 | * Get the customers. |
||
15 | * |
||
16 | * @param Get\RequestData|null $requestData The request data. |
||
17 | * @return Get\ApiResponse |
||
18 | */ |
||
19 | 12 | public function getCustomers(Get\RequestData $requestData = null) |
|
35 | |||
36 | /** |
||
37 | * Get one customer by ID. |
||
38 | * |
||
39 | * @param integer $customerId The customer ID. |
||
40 | * @return Customer|null |
||
41 | */ |
||
42 | 6 | View Code Duplication | public function getCustomer($customerId) |
51 | |||
52 | /** |
||
53 | * Get once customer by the external ID. |
||
54 | * |
||
55 | * @param string $customerExternalUid The external customer ID. |
||
56 | * @return Customer|null |
||
57 | */ |
||
58 | 3 | View Code Duplication | public function getCustomerByExternalUid($customerExternalUid) |
67 | |||
68 | /** |
||
69 | * Update a customer. |
||
70 | * |
||
71 | * @param Update\RequestData $requestData The data to send. |
||
72 | * @return Update\ApiResponse |
||
73 | */ |
||
74 | 3 | public function updateCustomer(Update\RequestData $requestData) |
|
80 | |||
81 | /** |
||
82 | * Create a customer. |
||
83 | * |
||
84 | * @param Customer $customer The customer data to send. |
||
85 | * @return Create\ApiResponse |
||
86 | */ |
||
87 | 3 | public function createCustomer(Customer $customer) |
|
93 | |||
94 | /** |
||
95 | * Delete a customer. |
||
96 | * |
||
97 | * @param integer $customerId The customer ID to delete. |
||
98 | * @return Delete\ApiResponse |
||
99 | */ |
||
100 | 3 | public function deleteCustomer($customerId) |
|
107 | |||
108 | /** |
||
109 | * Add credits to a customer. |
||
110 | * |
||
111 | * @param integer $customerId The customer ID. |
||
112 | * @param float $amount The amount of credit to add. |
||
113 | * @return AddCredits\ApiResponse |
||
114 | */ |
||
115 | 3 | public function addCredits($customerId, $amount) |
|
122 | |||
123 | /** |
||
124 | * Add credits to a customer. |
||
125 | * |
||
126 | * @param integer $customerId The customer ID. |
||
127 | * @return CreateSecureLink\ApiResponse |
||
128 | */ |
||
129 | 3 | public function createSecureLink($customerId) |
|
136 | } |
||
137 |
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.