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 |
||
18 | abstract class AbstractApi |
||
19 | { |
||
20 | /** |
||
21 | * @var \Http\Client\HttpClient |
||
22 | */ |
||
23 | protected $httpClient; |
||
24 | |||
25 | /** |
||
26 | * @var \IBM\Watson\Common\Hydrator\HydratorInterface |
||
27 | */ |
||
28 | protected $hydrator; |
||
29 | |||
30 | /** |
||
31 | * @var \IBM\Watson\Common\RequestBuilder |
||
32 | */ |
||
33 | protected $requestBuilder; |
||
34 | |||
35 | /** |
||
36 | * AbstractApi constructor. |
||
37 | * |
||
38 | * @param \Http\Client\HttpClient $httpClient |
||
39 | * @param \IBM\Watson\Common\Hydrator\HydratorInterface $hydrator |
||
40 | * @param \IBM\Watson\Common\RequestBuilder $requestBuilder |
||
41 | */ |
||
42 | public function __construct(HttpClient $httpClient, HydratorInterface $hydrator, RequestBuilder $requestBuilder) |
||
52 | |||
53 | /** |
||
54 | * Create and send PSR-7 GET request |
||
55 | * |
||
56 | * @param string $path |
||
57 | * @param array|null $params |
||
58 | * @param array|null $headers |
||
59 | * |
||
60 | * @return \Psr\Http\Message\ResponseInterface |
||
61 | * |
||
62 | * @throws \Http\Client\Exception |
||
63 | * @throws \Exception |
||
64 | */ |
||
65 | protected function get($path, array $params = null, array $headers = null) |
||
83 | |||
84 | /** |
||
85 | * Create and send PSR-7 POST request |
||
86 | * |
||
87 | * @param string $path |
||
88 | * @param array|null $params |
||
89 | * @param array|null $headers |
||
90 | * |
||
91 | * @return \Psr\Http\Message\ResponseInterface |
||
92 | * |
||
93 | * @throws \Http\Client\Exception |
||
94 | * @throws \Exception |
||
95 | */ |
||
96 | protected function post($path, array $params = null, array $headers = null) |
||
108 | |||
109 | /** |
||
110 | * Create and send PSR-7 POST request |
||
111 | * |
||
112 | * @param string $path |
||
113 | * @param resource|string|StreamInterface|null $body |
||
114 | * @param array|null $headers |
||
115 | * |
||
116 | * @return \Psr\Http\Message\ResponseInterface |
||
117 | * |
||
118 | * @throws \Http\Client\Exception |
||
119 | * @throws \Exception |
||
120 | */ |
||
121 | protected function postRaw($path, $body, array $headers = null) |
||
131 | |||
132 | /** |
||
133 | * Create and set PSR-7 PUT request |
||
134 | * |
||
135 | * @param string $path |
||
136 | * @param array|null $params |
||
137 | * @param array|null $headers |
||
138 | * |
||
139 | * @return \Psr\Http\Message\ResponseInterface |
||
140 | * |
||
141 | * @throws \Http\Client\Exception |
||
142 | * @throws \Exception |
||
143 | */ |
||
144 | View Code Duplication | protected function put($path, array $params = null, array $headers = null) |
|
154 | |||
155 | /** |
||
156 | * Create and send PSR-7 PATCH request |
||
157 | * |
||
158 | * @param string $path |
||
159 | * @param array|null $params |
||
160 | * @param array|null $headers |
||
161 | * |
||
162 | * @return \Psr\Http\Message\ResponseInterface |
||
163 | * |
||
164 | * @throws \Http\Client\Exception |
||
165 | * @throws \Exception |
||
166 | */ |
||
167 | View Code Duplication | protected function patch($path, array $params = null, array $headers = null) |
|
177 | |||
178 | /** |
||
179 | * Create and send PSR-7 DELETE request |
||
180 | * |
||
181 | * @param string $path |
||
182 | * @param array|null $params |
||
183 | * @param array|null $headers |
||
184 | * |
||
185 | * @return \Psr\Http\Message\ResponseInterface |
||
186 | * |
||
187 | * @throws \Http\Client\Exception |
||
188 | * @throws \Exception |
||
189 | */ |
||
190 | View Code Duplication | protected function delete($path, array $params = null, array $headers = null) |
|
200 | |||
201 | /** |
||
202 | * Handle API errors |
||
203 | * |
||
204 | * @param \Psr\Http\Message\ResponseInterface $response |
||
205 | * |
||
206 | * @throws \IBM\Watson\Common\Exception\InsufficientPrivilegesException |
||
207 | * @throws \IBM\Watson\Common\Exception\NotFoundException |
||
208 | * @throws \IBM\Watson\Common\Exception\UnknownErrorException |
||
209 | */ |
||
210 | protected function handleErrors(ResponseInterface $response) |
||
232 | } |
||
233 |
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.