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 |
||
25 | abstract class HttpApi |
||
26 | { |
||
27 | /** |
||
28 | * The HTTP client. |
||
29 | * |
||
30 | * @var HttpClient |
||
31 | */ |
||
32 | private $httpClient; |
||
33 | |||
34 | /** |
||
35 | * @var Hydrator |
||
36 | */ |
||
37 | protected $hydrator; |
||
38 | |||
39 | /** |
||
40 | * @var RequestBuilder |
||
41 | */ |
||
42 | protected $requestBuilder; |
||
43 | |||
44 | /** |
||
45 | * @param HttpClient $httpClient |
||
46 | * @param RequestBuilder $requestBuilder |
||
47 | * @param Hydrator $hydrator |
||
48 | */ |
||
49 | 39 | public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator) |
|
57 | |||
58 | /** |
||
59 | * @param ResponseInterface $response |
||
60 | * @param string $class |
||
61 | * |
||
62 | * @return mixed|ResponseInterface |
||
63 | * |
||
64 | * @throws \Exception |
||
65 | */ |
||
66 | 33 | protected function hydrateResponse(ResponseInterface $response, $class) |
|
78 | |||
79 | /** |
||
80 | * Throw the correct exception for this error. |
||
81 | * |
||
82 | * @param ResponseInterface $response |
||
83 | * |
||
84 | * @throws \Exception |
||
85 | */ |
||
86 | 6 | protected function handleErrors(ResponseInterface $response) |
|
106 | |||
107 | /** |
||
108 | * Send a GET request with query parameters. |
||
109 | * |
||
110 | * @param string $path Request path |
||
111 | * @param array $parameters GET parameters |
||
112 | * @param array $requestHeaders Request Headers |
||
113 | * |
||
114 | * @return ResponseInterface |
||
115 | */ |
||
116 | 7 | protected function httpGet($path, array $parameters = [], array $requestHeaders = []) |
|
132 | |||
133 | /** |
||
134 | * Send a POST request with parameters. |
||
135 | * |
||
136 | * @param string $path Request path |
||
137 | * @param array $parameters POST parameters |
||
138 | * @param array $requestHeaders Request headers |
||
139 | * |
||
140 | * @return ResponseInterface |
||
141 | */ |
||
142 | 5 | protected function httpPost($path, array $parameters = [], array $requestHeaders = []) |
|
146 | |||
147 | /** |
||
148 | * Send a POST request with raw data. |
||
149 | * |
||
150 | * @param string $path Request path |
||
151 | * @param array|string $body Request body |
||
152 | * @param array $requestHeaders Request headers |
||
153 | * |
||
154 | * @return ResponseInterface |
||
155 | */ |
||
156 | 5 | View Code Duplication | protected function httpPostRaw($path, $body, array $requestHeaders = []) |
168 | |||
169 | /** |
||
170 | * Send a PUT request. |
||
171 | * |
||
172 | * @param string $path Request path |
||
173 | * @param array $parameters PUT parameters |
||
174 | * @param array $requestHeaders Request headers |
||
175 | * |
||
176 | * @return ResponseInterface |
||
177 | */ |
||
178 | 4 | View Code Duplication | protected function httpPut($path, array $parameters = [], array $requestHeaders = []) |
190 | |||
191 | /** |
||
192 | * Send a DELETE request. |
||
193 | * |
||
194 | * @param string $path Request path |
||
195 | * @param array $parameters DELETE parameters |
||
196 | * @param array $requestHeaders Request headers |
||
197 | * |
||
198 | * @return ResponseInterface |
||
199 | */ |
||
200 | 6 | View Code Duplication | protected function httpDelete($path, array $parameters = [], array $requestHeaders = []) |
212 | |||
213 | /** |
||
214 | * Prepare a set of key-value-pairs to be encoded as multipart/form-data. |
||
215 | * |
||
216 | * @param array $parameters Request parameters |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | 15 | protected function createRequestBody(array $parameters) |
|
237 | } |
||
238 |
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.