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 |
||
24 | abstract class HttpApi |
||
25 | { |
||
26 | /** |
||
27 | * @var HttpClient |
||
28 | */ |
||
29 | protected $httpClient; |
||
30 | |||
31 | /** |
||
32 | * @var Hydrator |
||
33 | */ |
||
34 | protected $hydrator; |
||
35 | |||
36 | /** |
||
37 | * @var RequestBuilder |
||
38 | */ |
||
39 | protected $requestBuilder; |
||
40 | |||
41 | /** |
||
42 | * @param HttpClient $httpClient |
||
43 | * @param RequestBuilder $requestBuilder |
||
44 | * @param Hydrator $hydrator |
||
45 | */ |
||
46 | public function __construct(HttpClient $httpClient, Hydrator $hydrator, RequestBuilder $requestBuilder) |
||
54 | |||
55 | /** |
||
56 | * Send a GET request with query parameters. |
||
57 | * |
||
58 | * @param string $path Request path |
||
59 | * @param array $params GET parameters |
||
60 | * @param array $requestHeaders Request Headers |
||
61 | * |
||
62 | * @throws ClientExceptionInterface |
||
63 | * |
||
64 | * @return ResponseInterface |
||
65 | */ |
||
66 | protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
||
76 | |||
77 | /** |
||
78 | * Send a POST request with JSON-encoded parameters. |
||
79 | * |
||
80 | * @param string $path Request path |
||
81 | * @param array $params POST parameters to be JSON encoded |
||
82 | * @param array $requestHeaders Request headers |
||
83 | * |
||
84 | * @throws ClientExceptionInterface |
||
85 | * |
||
86 | * @return ResponseInterface |
||
87 | */ |
||
88 | protected function httpPost(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
||
94 | |||
95 | /** |
||
96 | * Send a POST request with raw data. |
||
97 | * |
||
98 | * @param string $path Request path |
||
99 | * @param array|string $body Request body |
||
100 | * @param array $requestHeaders Request headers |
||
101 | * |
||
102 | * @throws ClientExceptionInterface |
||
103 | * |
||
104 | * @return ResponseInterface |
||
105 | */ |
||
106 | protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface |
||
112 | |||
113 | /** |
||
114 | * Send a PUT request with JSON-encoded parameters. |
||
115 | * |
||
116 | * @param string $path Request path |
||
117 | * @param array $params POST parameters to be JSON encoded |
||
118 | * @param array $requestHeaders Request headers |
||
119 | * |
||
120 | * @throws ClientExceptionInterface |
||
121 | * |
||
122 | * @return ResponseInterface |
||
123 | */ |
||
124 | View Code Duplication | protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
132 | |||
133 | /** |
||
134 | * Send a DELETE request with JSON-encoded parameters. |
||
135 | * |
||
136 | * @param string $path Request path |
||
137 | * @param array $params POST parameters to be JSON encoded |
||
138 | * @param array $requestHeaders Request headers |
||
139 | * |
||
140 | * @throws ClientExceptionInterface |
||
141 | * |
||
142 | * @return ResponseInterface |
||
143 | */ |
||
144 | View Code Duplication | protected function httpDelete(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
152 | |||
153 | /** |
||
154 | * Send a PATCH request with json encoded data. |
||
155 | * |
||
156 | * @param string $path Request path |
||
157 | * @param array|string $body Request body |
||
158 | * @param array $requestHeaders Request headers |
||
159 | * |
||
160 | * @throws ClientExceptionInterface |
||
161 | * |
||
162 | * @return ResponseInterface |
||
163 | */ |
||
164 | View Code Duplication | protected function httpPatch(string $path, $body, array $requestHeaders = []): ResponseInterface |
|
172 | |||
173 | /** |
||
174 | * Handle HTTP errors. |
||
175 | * |
||
176 | * Call is controlled by the specific API methods. |
||
177 | * |
||
178 | * @param ResponseInterface $response |
||
179 | * |
||
180 | * @throws DomainException |
||
181 | */ |
||
182 | protected function handleErrors(ResponseInterface $response) |
||
203 | } |
||
204 |
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.