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 |
||
23 | abstract class HttpApi |
||
24 | { |
||
25 | /** |
||
26 | * @var HttpClient |
||
27 | */ |
||
28 | protected $httpClient; |
||
29 | |||
30 | /** |
||
31 | * @var Hydrator |
||
32 | */ |
||
33 | protected $hydrator; |
||
34 | |||
35 | /** |
||
36 | * @var RequestBuilder |
||
37 | */ |
||
38 | protected $requestBuilder; |
||
39 | |||
40 | /** |
||
41 | * @param HttpClient $httpClient |
||
42 | * @param RequestBuilder $requestBuilder |
||
43 | * @param Hydrator $hydrator |
||
44 | */ |
||
45 | public function __construct(HttpClient $httpClient, Hydrator $hydrator, RequestBuilder $requestBuilder) |
||
53 | |||
54 | /** |
||
55 | * Send a GET request with query parameters. |
||
56 | * |
||
57 | * @param string $path Request path |
||
58 | * @param array $params GET parameters |
||
59 | * @param array $requestHeaders Request Headers |
||
60 | * |
||
61 | * @return ResponseInterface |
||
62 | */ |
||
63 | View Code Duplication | protected function httpGet(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
73 | |||
74 | /** |
||
75 | * Send a POST request with JSON-encoded parameters. |
||
76 | * |
||
77 | * @param string $path Request path |
||
78 | * @param array $params POST parameters to be JSON encoded |
||
79 | * @param array $requestHeaders Request headers |
||
80 | * |
||
81 | * @return ResponseInterface |
||
82 | */ |
||
83 | protected function httpPost(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
||
89 | |||
90 | /** |
||
91 | * Send a POST request with raw data. |
||
92 | * |
||
93 | * @param string $path Request path |
||
94 | * @param array|string $body Request body |
||
95 | * @param array $requestHeaders Request headers |
||
96 | * |
||
97 | * @return ResponseInterface |
||
98 | */ |
||
99 | protected function httpPostRaw(string $path, $body, array $requestHeaders = []): ResponseInterface |
||
105 | |||
106 | /** |
||
107 | * Send a PUT request with JSON-encoded parameters. |
||
108 | * |
||
109 | * @param string $path Request path |
||
110 | * @param array $params POST parameters to be JSON encoded |
||
111 | * @param array $requestHeaders Request headers |
||
112 | * |
||
113 | * @return ResponseInterface |
||
114 | */ |
||
115 | View Code Duplication | protected function httpPut(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
123 | |||
124 | /** |
||
125 | * Send a PATCH request with JSON-encoded parameters. |
||
126 | * |
||
127 | * @param string $path Request path |
||
128 | * @param array $params PATCH parameters to be JSON encoded |
||
129 | * @param array $requestHeaders Request headers |
||
130 | * |
||
131 | * @return ResponseInterface |
||
132 | */ |
||
133 | View Code Duplication | protected function httpPatch(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
141 | |||
142 | /** |
||
143 | * Send a DELETE request with JSON-encoded parameters. |
||
144 | * |
||
145 | * @param string $path Request path |
||
146 | * @param array $params POST parameters to be JSON encoded |
||
147 | * @param array $requestHeaders Request headers |
||
148 | * |
||
149 | * @return ResponseInterface |
||
150 | */ |
||
151 | View Code Duplication | protected function httpDelete(string $path, array $params = [], array $requestHeaders = []): ResponseInterface |
|
159 | |||
160 | /** |
||
161 | * Handle HTTP errors. |
||
162 | * |
||
163 | * Call is controlled by the specific API methods. |
||
164 | * |
||
165 | * @param ResponseInterface $response |
||
166 | * |
||
167 | * @throws DomainException |
||
168 | */ |
||
169 | protected function handleErrors(ResponseInterface $response) |
||
195 | } |
||
196 |
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.