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:
Complex classes like RestClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RestClient, and based on these observations, apply Extract Interface, too.
1 | <?PHP |
||
27 | class RestClient |
||
28 | { |
||
29 | /** |
||
30 | * Your API key. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $apiKey; |
||
35 | |||
36 | /** |
||
37 | * @var HttpClient |
||
38 | */ |
||
39 | protected $httpClient; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $apiHost; |
||
45 | |||
46 | /** |
||
47 | * The version of the API to use. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $apiVersion = 'v2'; |
||
52 | |||
53 | /** |
||
54 | * If we should use SSL or not. |
||
55 | * |
||
56 | * @var bool |
||
57 | * |
||
58 | * @deprecated To be removed in 3.0 |
||
59 | */ |
||
60 | protected $sslEnabled = true; |
||
61 | |||
62 | /** |
||
63 | * @param string $apiKey |
||
64 | * @param string $apiHost |
||
65 | * @param HttpClient $httpClient |
||
66 | */ |
||
67 | 7 | public function __construct($apiKey, $apiHost, HttpClient $httpClient = null) |
|
73 | |||
74 | /** |
||
75 | * @param string $method |
||
76 | * @param string $uri |
||
77 | * @param mixed $body |
||
78 | * @param array $files |
||
79 | * @param array $headers |
||
80 | * |
||
81 | * @throws GenericHTTPError |
||
82 | * @throws InvalidCredentials |
||
83 | * @throws MissingEndpoint |
||
84 | * @throws MissingRequiredParameters |
||
85 | * |
||
86 | * @return \stdClass |
||
87 | */ |
||
88 | protected function send($method, $uri, $body = null, $files = [], array $headers = []) |
||
110 | |||
111 | /** |
||
112 | * @param string $url |
||
113 | * |
||
114 | * @throws GenericHTTPError |
||
115 | * @throws InvalidCredentials |
||
116 | * @throws MissingEndpoint |
||
117 | * @throws MissingRequiredParameters |
||
118 | * |
||
119 | * @return \stdClass |
||
120 | */ |
||
121 | 2 | public function getAttachment($url) |
|
130 | |||
131 | /** |
||
132 | * @param string $endpointUrl |
||
133 | * @param array $postData |
||
134 | * @param array $files |
||
135 | * |
||
136 | * @throws GenericHTTPError |
||
137 | * @throws InvalidCredentials |
||
138 | * @throws MissingEndpoint |
||
139 | * @throws MissingRequiredParameters |
||
140 | * |
||
141 | * @return \stdClass |
||
142 | */ |
||
143 | 5 | public function post($endpointUrl, array $postData = [], $files = []) |
|
182 | |||
183 | /** |
||
184 | * @param string $endpointUrl |
||
185 | * @param array $queryString |
||
186 | * |
||
187 | * @throws GenericHTTPError |
||
188 | * @throws InvalidCredentials |
||
189 | * @throws MissingEndpoint |
||
190 | * @throws MissingRequiredParameters |
||
191 | * |
||
192 | * @return \stdClass |
||
193 | */ |
||
194 | public function get($endpointUrl, $queryString = []) |
||
198 | |||
199 | /** |
||
200 | * @param string $endpointUrl |
||
201 | * |
||
202 | * @throws GenericHTTPError |
||
203 | * @throws InvalidCredentials |
||
204 | * @throws MissingEndpoint |
||
205 | * @throws MissingRequiredParameters |
||
206 | * |
||
207 | * @return \stdClass |
||
208 | */ |
||
209 | public function delete($endpointUrl) |
||
213 | |||
214 | /** |
||
215 | * @param string $endpointUrl |
||
216 | * @param mixed $putData |
||
217 | * |
||
218 | * @throws GenericHTTPError |
||
219 | * @throws InvalidCredentials |
||
220 | * @throws MissingEndpoint |
||
221 | * @throws MissingRequiredParameters |
||
222 | * |
||
223 | * @return \stdClass |
||
224 | */ |
||
225 | public function put($endpointUrl, $putData) |
||
229 | |||
230 | /** |
||
231 | * @param ResponseInterface $responseObj |
||
232 | * |
||
233 | * @throws GenericHTTPError |
||
234 | * @throws InvalidCredentials |
||
235 | * @throws MissingEndpoint |
||
236 | * @throws MissingRequiredParameters |
||
237 | * |
||
238 | * @return \stdClass |
||
239 | */ |
||
240 | 2 | public function responseHandler(ResponseInterface $responseObj) |
|
264 | |||
265 | /** |
||
266 | * @param ResponseInterface $responseObj |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | protected function getResponseExceptionMessage(ResponseInterface $responseObj) |
||
280 | |||
281 | /** |
||
282 | * Prepare a file for the postBody. |
||
283 | * |
||
284 | * @param string $fieldName |
||
285 | * @param string|array $filePath |
||
286 | * @param int $fileIndex |
||
287 | * |
||
288 | * @return array |
||
289 | */ |
||
290 | 4 | protected function prepareFile($fieldName, $filePath, $fileIndex = 0) |
|
324 | |||
325 | /** |
||
326 | * @return HttpClient |
||
327 | */ |
||
328 | protected function getHttpClient() |
||
336 | |||
337 | /** |
||
338 | * @param string $uri |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | private function getApiUrl($uri) |
||
346 | |||
347 | /** |
||
348 | * @param string $apiEndpoint |
||
349 | * @param string $apiVersion |
||
350 | * @param bool $ssl |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) |
||
358 | |||
359 | /** |
||
360 | * @param string $apiVersion |
||
361 | * |
||
362 | * @return RestClient |
||
363 | */ |
||
364 | public function setApiVersion($apiVersion) |
||
370 | |||
371 | /** |
||
372 | * @param bool $sslEnabled |
||
373 | * |
||
374 | * @return RestClient |
||
375 | * |
||
376 | * @deprecated To be removed in 3.0 |
||
377 | */ |
||
378 | public function setSslEnabled($sslEnabled) |
||
384 | } |
||
385 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.