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 |
||
10 | class KeyManager implements KeyManagerInterface |
||
11 | { |
||
12 | use ApiResponse; |
||
13 | |||
14 | /** |
||
15 | * Create a new Client with Api credentials. |
||
16 | * |
||
17 | * @param string $name |
||
18 | * @param string $type |
||
19 | * @param string $status |
||
20 | * @return \Illuminate\Http\JsonResponse |
||
21 | */ |
||
22 | public function createClient(string $name, string $type, string $status = 'active'): JsonResponse |
||
38 | |||
39 | /** |
||
40 | * Get client's private key. |
||
41 | * |
||
42 | * @param int $client_id |
||
43 | * @return \Illuminate\Http\JsonResponse |
||
44 | */ |
||
45 | View Code Duplication | public function getPrivateKey(int $client_id): JsonResponse |
|
55 | |||
56 | /** |
||
57 | * Change Client's public and private keys. |
||
58 | * |
||
59 | * @param int $client_id |
||
60 | * @return \Illuminate\Http\JsonResponse |
||
61 | */ |
||
62 | View Code Duplication | public function changeKeys(int $client_id): JsonResponse |
|
74 | |||
75 | /** |
||
76 | * Suspend Client's Account. |
||
77 | * |
||
78 | * @param int $client_id |
||
79 | * @return \Illuminate\Http\JsonResponse |
||
80 | */ |
||
81 | public function suspendClient(int $client_id): JsonResponse |
||
95 | |||
96 | /** |
||
97 | * Activate Client's Account. |
||
98 | * |
||
99 | * @param int $client_id |
||
100 | * @return \Illuminate\Http\JsonResponse |
||
101 | */ |
||
102 | View Code Duplication | public function activateClient(int $client_id): JsonResponse |
|
116 | |||
117 | /** |
||
118 | * Suspend Client's Api Credential. |
||
119 | * |
||
120 | * @param int $client_id |
||
121 | * @return \Illuminate\Http\JsonResponse |
||
122 | */ |
||
123 | View Code Duplication | public function suspendApiCredential(int $client_id): JsonResponse |
|
137 | |||
138 | /** |
||
139 | * Activate Client's Api Credential. |
||
140 | * |
||
141 | * @param int $client_id |
||
142 | * @return \Illuminate\Http\JsonResponse |
||
143 | */ |
||
144 | View Code Duplication | public function activateApiCredential(int $client_id): JsonResponse |
|
158 | } |
||
159 |
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.