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 | 9 | 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 | 7 | 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 | protected function handleErrors(ResponseInterface $response) |
||
104 | |||
105 | /** |
||
106 | * Send a GET request with query parameters. |
||
107 | * |
||
108 | * @param string $path Request path |
||
109 | * @param array $parameters GET parameters |
||
110 | * @param array $requestHeaders Request Headers |
||
111 | * |
||
112 | * @return ResponseInterface |
||
113 | */ |
||
114 | protected function httpGet($path, array $parameters = [], array $requestHeaders = []) |
||
130 | |||
131 | /** |
||
132 | * Send a POST request with JSON-encoded parameters. |
||
133 | * |
||
134 | * @param string $path Request path |
||
135 | * @param array $parameters POST parameters to be JSON encoded |
||
136 | * @param array $requestHeaders Request headers |
||
137 | * |
||
138 | * @return ResponseInterface |
||
139 | */ |
||
140 | 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 | protected function httpPostRaw($path, $body, array $requestHeaders = []) |
||
168 | |||
169 | /** |
||
170 | * Send a PUT request with JSON-encoded parameters. |
||
171 | * |
||
172 | * @param string $path Request path |
||
173 | * @param array $parameters POST parameters to be JSON encoded |
||
174 | * @param array $requestHeaders Request headers |
||
175 | * |
||
176 | * @return ResponseInterface |
||
177 | */ |
||
178 | View Code Duplication | protected function httpPut($path, array $parameters = [], array $requestHeaders = []) |
|
192 | |||
193 | /** |
||
194 | * Send a DELETE request with JSON-encoded parameters. |
||
195 | * |
||
196 | * @param string $path Request path |
||
197 | * @param array $parameters POST parameters to be JSON encoded |
||
198 | * @param array $requestHeaders Request headers |
||
199 | * |
||
200 | * @return ResponseInterface |
||
201 | */ |
||
202 | View Code Duplication | protected function httpDelete($path, array $parameters = [], array $requestHeaders = []) |
|
216 | |||
217 | /** |
||
218 | * Create a JSON encoded version of an array of parameters. |
||
219 | * |
||
220 | * @param array $parameters Request parameters |
||
221 | * |
||
222 | * @return null|string |
||
223 | */ |
||
224 | protected function createRequestBody(array $parameters) |
||
228 | } |
||
229 |
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.