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 |
||
8 | class Transactions |
||
9 | { |
||
10 | /** |
||
11 | * Mondo client. |
||
12 | * |
||
13 | * @var ClientInterface |
||
14 | */ |
||
15 | protected $client; |
||
16 | |||
17 | /** |
||
18 | * Accounts constructor. |
||
19 | * |
||
20 | * @param ClientInterface $client |
||
21 | */ |
||
22 | public function __construct(ClientInterface $client) |
||
26 | |||
27 | /** |
||
28 | * Information for a given transaction ID. |
||
29 | * |
||
30 | * @param string $transactionId |
||
31 | * |
||
32 | * @return Response\Transactions\Transaction |
||
33 | */ |
||
34 | public function getTransaction($transactionId) |
||
41 | |||
42 | /** |
||
43 | * Get the transaction history for the account id. |
||
44 | * |
||
45 | * @param string $accountId |
||
46 | * |
||
47 | * @return Response\Transactions |
||
48 | */ |
||
49 | View Code Duplication | public function getTransactionsForAccountId($accountId) |
|
60 | |||
61 | /** |
||
62 | * Add annotation(s) for a transaction. |
||
63 | * |
||
64 | * @param string $transactionId |
||
65 | * @param string|array $key |
||
66 | * @param string $value |
||
67 | * |
||
68 | * @return \Psr\Http\Message\ResponseInterface |
||
69 | */ |
||
70 | View Code Duplication | public function addAnnotationForTransaction($transactionId, $key, $value = null) |
|
82 | |||
83 | /** |
||
84 | * Remove annotation(s) for a transaction. |
||
85 | * |
||
86 | * @param string $transactionId |
||
87 | * @param string $keys |
||
88 | * |
||
89 | * @return \Psr\Http\Message\ResponseInterface |
||
90 | */ |
||
91 | View Code Duplication | public function removeAnnotationForTransaction($transactionId, $keys) |
|
103 | } |
||
104 |
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.