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 |
||
14 | class Invoice extends Client |
||
15 | { |
||
16 | /** |
||
17 | * @param string $customerId The customer's id |
||
18 | * |
||
19 | * @throws \Exception |
||
20 | * |
||
21 | * @return array |
||
22 | */ |
||
23 | View Code Duplication | public function listInvoicesByCustomer(string $customerId): array |
|
42 | |||
43 | /** |
||
44 | * @param string $invoiceId The invoice's id |
||
45 | * |
||
46 | * @throws \Exception |
||
47 | * |
||
48 | * @return array |
||
49 | */ |
||
50 | View Code Duplication | public function getInvoice(string $invoiceId) |
|
69 | |||
70 | /** |
||
71 | * @param string $invoiceId The invoice's id |
||
72 | * |
||
73 | * @throws \Exception |
||
74 | * |
||
75 | * @return array |
||
76 | */ |
||
77 | public function getInvoicePdf(string $invoiceId) |
||
85 | } |
||
86 |
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.