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 CertainApiClient |
||
15 | { |
||
16 | /** |
||
17 | * URL for call request |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | private $baseUri = 'https://appeu.certain.com/certainExternal/service/v1/'; |
||
22 | |||
23 | /** |
||
24 | * |
||
25 | * @var Client |
||
26 | */ |
||
27 | private $client; |
||
28 | |||
29 | /** |
||
30 | * AccountCode to put in the URI |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $accountCode; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $builPath; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $username; |
||
47 | |||
48 | /** |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private $password; |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * @param string|null $baseUri |
||
57 | * @param string $username |
||
58 | * @param string $password |
||
59 | * @param string $accountCode |
||
60 | */ |
||
61 | public function __construct($baseUri, $username, $password, |
||
75 | |||
76 | /** |
||
77 | * |
||
78 | * @return Client |
||
79 | */ |
||
80 | public function getClient() |
||
84 | |||
85 | /** |
||
86 | * Define a client |
||
87 | * @param Client $client |
||
88 | * @return \Wabel\CertainAPI\CertainApiClient |
||
89 | */ |
||
90 | public function setClient(Client $client) |
||
95 | |||
96 | /** |
||
97 | * Get Account Code |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getAccountCode() |
||
104 | |||
105 | /** |
||
106 | * Build the URI to request |
||
107 | * @param string|array $ressourceName |
||
108 | * @param string $ressourceId |
||
109 | * @return string |
||
110 | */ |
||
111 | private function builPathToCall($ressourceName,$ressourcePath =null, $ressourceId = null) |
||
130 | |||
131 | /** |
||
132 | * Make "GET" request with the client. |
||
133 | * @param string $ressourceName |
||
134 | * @param string $ressourcePath |
||
135 | * @param string $ressourceId |
||
136 | * @param array $query |
||
137 | * @param boolean $assoc |
||
138 | * @param string $contentType |
||
139 | * @return array |
||
140 | */ |
||
141 | View Code Duplication | public function get($ressourceName, $ressourcePath =null, $ressourceId = null, $query = array(), |
|
158 | |||
159 | /** |
||
160 | * Make "POST" request with the client. |
||
161 | * @param string $ressourceName |
||
162 | * @param string $ressourcePath |
||
163 | * @param string $ressourceId |
||
164 | * @param array $bodyData |
||
165 | * @param array $query |
||
166 | * @param boolean $assoc |
||
167 | * @param string $contentType |
||
168 | * @return array |
||
169 | */ |
||
170 | View Code Duplication | public function post($ressourceName, $ressourcePath =null, $ressourceId = null, |
|
192 | |||
193 | /** |
||
194 | * Make "PUT" request with the client. |
||
195 | * @param string $ressourceName |
||
196 | * @param string $ressourcePath |
||
197 | * @param string $ressourceId |
||
198 | * @param array $bodyData |
||
199 | * @param array $query |
||
200 | * @param boolean $assoc |
||
201 | * @param string $contentType |
||
202 | * @return array |
||
203 | */ |
||
204 | View Code Duplication | public function put($ressourceName, $ressourcePath =null, $ressourceId = null, |
|
226 | |||
227 | /** |
||
228 | * Make "DELETE" request with the client. |
||
229 | * @param string $ressourceName |
||
230 | * @param string $ressourcePath |
||
231 | * @param string $ressourceId |
||
232 | * @param boolean $assoc |
||
233 | * @param string $contentType |
||
234 | * @return array |
||
235 | */ |
||
236 | View Code Duplication | public function delete($ressourceName, $ressourcePath =null, $ressourceId = null, $assoc = false, |
|
252 | |||
253 | /** |
||
254 | * Make the Certain Api repsonse. |
||
255 | * @param ResponseInterface|null $response |
||
256 | * @param string $contentType |
||
257 | * @param boolean $assoc |
||
258 | * @return array |
||
259 | */ |
||
260 | private function makeCertainApiResponse($response, |
||
267 | |||
268 | public function getBuilPath() |
||
272 | |||
273 | |||
274 | } |
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.