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 Options 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 Options, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | trait Options |
||
10 | { |
||
11 | /** @var string */ |
||
12 | private static $CONTENT_TYPE_JSON = 'application/json'; |
||
13 | |||
14 | /** @var string */ |
||
15 | private static $CONTENT_TYPE_PLAIN = 'text/plain'; |
||
16 | |||
17 | /** @var string */ |
||
18 | private static $CONTENT_TYPE_MULTIPART = 'multipart/form-data'; |
||
19 | |||
20 | /** @var string */ |
||
21 | private static $CONTENT_TYPE_FORM_PARAMS = 'application/x-www-form-urlencoded'; |
||
22 | |||
23 | /** @var array<String> */ |
||
24 | private $bodyFormats = ['json', 'multipart', 'form_params']; |
||
25 | |||
26 | /** @var string */ |
||
27 | protected static $CUSTOM_DATA_HEADER = 'X-Custom-Data'; |
||
28 | |||
29 | /** |
||
30 | * Set authentication auth bearer token |
||
31 | * |
||
32 | * @param string $token |
||
33 | * |
||
34 | * @return $this |
||
35 | */ |
||
36 | public function authBearer(string $token): self |
||
40 | |||
41 | /** |
||
42 | * Set authentication auth basic. If password is null |
||
43 | * only username will be used |
||
44 | * |
||
45 | * @param string $username |
||
46 | * @param string $password |
||
47 | * |
||
48 | * @return $this |
||
49 | */ |
||
50 | public function authBasic(string $username, string $password = ''): self |
||
60 | |||
61 | /** |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function throwErrors(): self |
||
70 | |||
71 | /** |
||
72 | * @return bool |
||
73 | */ |
||
74 | public function shouldThrowErrors(): bool |
||
78 | |||
79 | /** |
||
80 | * Values for existing header keys will be replaced |
||
81 | * |
||
82 | * @param string $key |
||
83 | * @param string $value |
||
84 | * |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function header(string $key, string $value): self |
||
93 | |||
94 | /** |
||
95 | * Values for existing header keys will be replaced |
||
96 | * |
||
97 | * @param array<String> $headers |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function headers(array $headers): self |
||
109 | |||
110 | /** |
||
111 | * Set the base uri. |
||
112 | * |
||
113 | * @param string $uri |
||
114 | * |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function baseUri(string $uri): self |
||
121 | |||
122 | /** |
||
123 | * Disallows redirects. |
||
124 | * |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function disallowRedirects(): self |
||
131 | |||
132 | /** |
||
133 | * @param int $max |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function allowRedirects(int $max = 0): self |
||
141 | |||
142 | /** |
||
143 | * Float describing the timeout of the request in seconds. |
||
144 | * use 0 to wait indefinitely |
||
145 | * |
||
146 | * @param float $seconds |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function timeout(float $seconds): self |
||
154 | |||
155 | /** |
||
156 | * Pass a string to specify an HTTP proxy, or an array to specify different proxies for different protocols. |
||
157 | * |
||
158 | * @param string|array<String> $proxy |
||
159 | * |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function useProxy($proxy): self |
||
166 | |||
167 | /** |
||
168 | * @param float $seconds |
||
169 | * |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function maxDuration(float $seconds): self |
||
176 | |||
177 | /** |
||
178 | * Does not verify SSL certificates |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function doNotVerifySsl(): self |
||
189 | |||
190 | /** |
||
191 | * Does verify SSL certificates |
||
192 | * |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function verifySsl(): self |
||
202 | |||
203 | /** |
||
204 | * Set the content type |
||
205 | * |
||
206 | * @param string $type |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function contentType(string $type): self |
||
214 | |||
215 | /** |
||
216 | * Set the clients user agent |
||
217 | * |
||
218 | * @param string $agent |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function userAgent(string $agent): self |
||
226 | |||
227 | /** |
||
228 | * Set accept header |
||
229 | * |
||
230 | * @param string $value |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function accept(string $value): self |
||
238 | |||
239 | /** |
||
240 | * Set option according guzzle request options |
||
241 | * |
||
242 | * @param string $key |
||
243 | * @param mixed $value |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function option(string $key, $value): self |
||
253 | |||
254 | /** |
||
255 | * Set param for the query url |
||
256 | * |
||
257 | * @param string $key |
||
258 | * @param string $value |
||
259 | * |
||
260 | * @return $this |
||
261 | */ |
||
262 | public function queryParam(string $key, string $value): self |
||
268 | |||
269 | /** |
||
270 | * Set multiple params for query url |
||
271 | * in form of [<key> => <value>, <key2> => <value2>] |
||
272 | * |
||
273 | * @param array<String> $params |
||
274 | * |
||
275 | * @return $this |
||
276 | */ |
||
277 | public function queryParams(array $params): self |
||
285 | |||
286 | /** |
||
287 | * Any extra data to attach to the response header |
||
288 | * Available in response->customData(). Useful when using asynchronous requests |
||
289 | * to identify the request |
||
290 | * |
||
291 | * @param mixed $data |
||
292 | * |
||
293 | * @return $this |
||
294 | */ |
||
295 | public function customData($data): self |
||
299 | |||
300 | /** |
||
301 | * @param string $format |
||
302 | * |
||
303 | * @return $this |
||
304 | */ |
||
305 | protected function bodyFormat(string $format): self |
||
311 | |||
312 | /** |
||
313 | * @return $this |
||
314 | */ |
||
315 | public function asPlainText() |
||
321 | |||
322 | /** |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function asJson(): self |
||
331 | |||
332 | /** |
||
333 | * @return $this |
||
334 | */ |
||
335 | public function asFormParams(): self |
||
341 | |||
342 | /** |
||
343 | * Set the content type multipart form-data |
||
344 | * |
||
345 | * @return $this |
||
346 | */ |
||
347 | public function asMultipart(): self |
||
353 | |||
354 | /** |
||
355 | * @return array<mixed> |
||
356 | */ |
||
357 | public function getHeaders(): array |
||
361 | |||
362 | /** |
||
363 | * @return null|string |
||
364 | */ |
||
365 | public function getContentType() |
||
371 | |||
372 | /** |
||
373 | * @return string |
||
374 | */ |
||
375 | public function getBodyFormat(): string |
||
379 | |||
380 | /** |
||
381 | * Indicates if the payload must be an array |
||
382 | * depending on body format |
||
383 | * |
||
384 | * @return bool |
||
385 | */ |
||
386 | protected function payloadMustBeArray(): bool |
||
393 | |||
394 | /** |
||
395 | * @param mixed $payload |
||
396 | * |
||
397 | * @return $this |
||
398 | */ |
||
399 | protected function throwExceptionWhenPayloadIsNotArray($payload): self |
||
407 | |||
408 | /** |
||
409 | * Set any payload text, array |
||
410 | * |
||
411 | * @param mixed $payload |
||
412 | * |
||
413 | * @return $this |
||
414 | */ |
||
415 | public function payload($payload): self |
||
419 | |||
420 | /** |
||
421 | * @param string $option |
||
422 | * |
||
423 | * @return mixed |
||
424 | */ |
||
425 | protected function getOption(string $option) |
||
429 | |||
430 | /** |
||
431 | * @return $this |
||
432 | */ |
||
433 | protected function resolvePayload(): self |
||
483 | |||
484 | /** |
||
485 | * remove an option |
||
486 | * |
||
487 | * @param string $option |
||
488 | * |
||
489 | * @return $this |
||
490 | */ |
||
491 | protected function removeOption(string $option): self |
||
497 | |||
498 | /** |
||
499 | * @param array<String> $options |
||
500 | * |
||
501 | * @return $this |
||
502 | */ |
||
503 | protected function removeOptions(array $options): self |
||
515 | |||
516 | /** |
||
517 | * @param mixed $body |
||
518 | * |
||
519 | * @return $this |
||
520 | */ |
||
521 | protected function body($body): self |
||
525 | |||
526 | } |
||
527 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: