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 Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Request extends Http |
||
22 | { |
||
23 | /** |
||
24 | * you can implement more traits |
||
25 | */ |
||
26 | use JsonTrait; |
||
27 | |||
28 | protected static $curlAlias = array( |
||
29 | 'url' => 'CURLOPT_URL', |
||
30 | 'uri' => 'CURLOPT_URL', |
||
31 | 'debug' => 'CURLOPT_VERBOSE',//for debug verbose |
||
32 | 'method' => 'CURLOPT_CUSTOMREQUEST', |
||
33 | 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' |
||
34 | 'ua' => 'CURLOPT_USERAGENT', |
||
35 | 'timeout' => 'CURLOPT_TIMEOUT', // (secs) 0 means indefinitely |
||
36 | 'connect_timeout' => 'CURLOPT_CONNECTTIMEOUT', |
||
37 | 'referer' => 'CURLOPT_REFERER', |
||
38 | 'binary' => 'CURLOPT_BINARYTRANSFER', |
||
39 | 'port' => 'CURLOPT_PORT', |
||
40 | 'header' => 'CURLOPT_HEADER', // TRUE:include header |
||
41 | 'headers' => 'CURLOPT_HTTPHEADER', // array |
||
42 | 'download' => 'CURLOPT_FILE', // writing file stream (using fopen()), default is STDOUT |
||
43 | 'upload' => 'CURLOPT_INFILE', // reading file stream |
||
44 | 'transfer' => 'CURLOPT_RETURNTRANSFER', // TRUE:return string; FALSE:output directly (curl_exec) |
||
45 | 'follow_location' => 'CURLOPT_FOLLOWLOCATION', |
||
46 | 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , |
||
47 | 'expects_mime' => null, //expected mime |
||
48 | 'send_mime' => null, //send mime |
||
49 | 'ip' => null,//specify ip to send request |
||
50 | 'callback' => null,//callback on end |
||
51 | |||
52 | ); |
||
53 | protected static $loggerHandler; |
||
54 | public |
||
55 | $curlHandle, |
||
56 | $uri, |
||
57 | $sendMime, |
||
58 | $expectedMime, |
||
59 | $timeout, |
||
60 | $maxRedirects, |
||
61 | $encoding, |
||
62 | $payload, |
||
63 | $retryTimes, |
||
64 | /** |
||
65 | * @var int seconds |
||
66 | */ |
||
67 | $retryDuration, |
||
68 | $followRedirects; |
||
69 | |||
70 | protected |
||
71 | $body, |
||
72 | $endCallback, |
||
73 | $withURIQuery, |
||
74 | $hasInitialized = false, |
||
75 | /** |
||
76 | * @var array |
||
77 | */ |
||
78 | $options = array( |
||
79 | 'CURLOPT_MAXREDIRS' => 10, |
||
80 | 'CURLOPT_SSL_VERIFYPEER' => false,//for https |
||
81 | 'CURLOPT_SSL_VERIFYHOST' => 0,//for https |
||
82 | 'CURLOPT_IPRESOLVE' => CURL_IPRESOLVE_V4,//ipv4 first |
||
83 | // 'CURLOPT_SAFE_UPLOAD' => false,// compatible with PHP 5.6.0 |
||
84 | 'header' => true, |
||
85 | 'method' => self::GET, |
||
86 | 'transfer' => true, |
||
87 | 'headers' => array(), |
||
88 | 'follow_location' => true, |
||
89 | 'timeout' => 0, |
||
90 | // 'ip' => null, //host, in string, .e.g: 172.16.1.1:888 |
||
91 | 'retry_times' => 1,//redo task when failed |
||
92 | 'retry_duration' => 0,//in seconds |
||
93 | ); |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Request constructor. |
||
98 | */ |
||
99 | 6 | protected function __construct() |
|
102 | |||
103 | /** |
||
104 | * @return Request |
||
105 | */ |
||
106 | 6 | public static function create() |
|
110 | |||
111 | /** |
||
112 | * @param callable $handler |
||
113 | */ |
||
114 | 1 | public static function setLogHandler(callable $handler) |
|
118 | /** |
||
119 | * Specify timeout |
||
120 | * @param float|int $timeout seconds to timeout the HTTP call |
||
121 | * @return Request |
||
122 | */ |
||
123 | public function timeout($timeout) |
||
128 | |||
129 | /** |
||
130 | * @return Request |
||
131 | */ |
||
132 | public function noFollow() |
||
136 | |||
137 | /** |
||
138 | * If the response is a 301 or 302 redirect, automatically |
||
139 | * send off another request to that location |
||
140 | * @param int $follow follow or not to follow or maximal number of redirects |
||
141 | * @return Request |
||
142 | */ |
||
143 | public function follow($follow) |
||
149 | |||
150 | /** |
||
151 | * @param $parsedComponents |
||
152 | * @return string |
||
153 | */ |
||
154 | 6 | private static function combineUrl($parsedComponents) |
|
167 | |||
168 | /** |
||
169 | * @param string $mime |
||
170 | * @return $this |
||
171 | */ |
||
172 | 2 | public function expectsMime($mime = 'json') |
|
177 | |||
178 | /** |
||
179 | * @param string $mime |
||
180 | * @return Request |
||
181 | */ |
||
182 | 2 | public function sendMime($mime = 'json') |
|
188 | |||
189 | /** |
||
190 | * @param $headerName |
||
191 | * @param $value , can be rawurlencode |
||
192 | * @return $this |
||
193 | */ |
||
194 | 1 | public function addHeader($headerName, $value) |
|
199 | |||
200 | /** |
||
201 | * @param $uri |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function uri($uri) |
||
209 | |||
210 | |||
211 | |||
212 | /** |
||
213 | * @param array $headers |
||
214 | * @return $this |
||
215 | */ |
||
216 | 1 | public function addHeaders(array $headers) |
|
237 | |||
238 | /** |
||
239 | * @return mixed |
||
240 | */ |
||
241 | public function endCallback() |
||
245 | |||
246 | /** |
||
247 | * @return bool |
||
248 | */ |
||
249 | 2 | public function hasEndCallback() |
|
253 | |||
254 | /** |
||
255 | * @param $field alias or field name |
||
256 | * @return bool|mixed |
||
257 | */ |
||
258 | 3 | public function getIni($field = null) |
|
264 | |||
265 | /** |
||
266 | * @param $key |
||
267 | * @return mixed |
||
268 | */ |
||
269 | 6 | protected static function fullOption($key) |
|
279 | |||
280 | /** |
||
281 | * @param $queryData |
||
282 | * @return $this |
||
283 | */ |
||
284 | 1 | public function addQuery($queryData) |
|
297 | /** |
||
298 | * @param $uri |
||
299 | * @param null $payload |
||
300 | * @param array $options |
||
301 | * @return Request |
||
302 | */ |
||
303 | 3 | public function post($uri, $payload = null, array $options = array()) |
|
307 | |||
308 | /** |
||
309 | * @param $uri |
||
310 | * @param null $payload |
||
311 | * @param array $options |
||
312 | * @param null $response |
||
313 | * @return string |
||
314 | */ |
||
315 | 2 | public function quickPost($uri, $payload = null, array $options = array(), &$response = null) |
|
320 | |||
321 | |||
322 | /** |
||
323 | * @param $method |
||
324 | * @param $url |
||
325 | * @param $data |
||
326 | * @param array $options |
||
327 | * @return $this |
||
328 | */ |
||
329 | 5 | View Code Duplication | protected function ini($method, $url, $data, array $options = array()) |
336 | |||
337 | /** |
||
338 | * @param array $options |
||
339 | * @return $this |
||
340 | */ |
||
341 | 6 | public function addOptions(array $options = array()) |
|
347 | |||
348 | /** |
||
349 | * @param $uri |
||
350 | * @param null $payload |
||
351 | * @param array $options |
||
352 | * @return Request |
||
353 | */ |
||
354 | 1 | function put($uri, $payload = null, array $options = array()) |
|
358 | |||
359 | /** |
||
360 | * @param $uri |
||
361 | * @param null $payload |
||
362 | * @param array $options |
||
363 | * @return Request |
||
364 | */ |
||
365 | 1 | function patch($uri, $payload = null, array $options = array()) |
|
369 | |||
370 | /** |
||
371 | * @param $uri |
||
372 | * @param array $options |
||
373 | * @return Request |
||
374 | */ |
||
375 | 3 | public function get($uri, array $options = array()) |
|
379 | |||
380 | |||
381 | /** |
||
382 | * @param $uri |
||
383 | * @param array $options |
||
384 | * @param null $response |
||
385 | * @return string |
||
386 | */ |
||
387 | 2 | public function quickGet($uri, array $options = array(), &$response = null) |
|
392 | |||
393 | /** |
||
394 | * @param $uri |
||
395 | * @param array $options |
||
396 | * @return Request |
||
397 | */ |
||
398 | 1 | function options($uri, array $options = array()) |
|
402 | |||
403 | /** |
||
404 | * @param $uri |
||
405 | * @param array $options |
||
406 | * @return Request |
||
407 | */ |
||
408 | 1 | function head($uri, array $options = array()) |
|
412 | |||
413 | /** |
||
414 | * @param $uri |
||
415 | * @param array $options |
||
416 | * @return Request |
||
417 | */ |
||
418 | 1 | function delete($uri, array $options = array()) |
|
422 | |||
423 | /** |
||
424 | * @param $uri |
||
425 | * @param array $options |
||
426 | * @return Request |
||
427 | */ |
||
428 | 2 | function trace($uri, array $options = array()) |
|
432 | |||
433 | /** |
||
434 | * @param bool $isMultiCurl |
||
435 | * @return Response |
||
436 | */ |
||
437 | 6 | public function send($isMultiCurl = false) |
|
459 | |||
460 | /** |
||
461 | * @return $this |
||
462 | */ |
||
463 | 6 | public function applyOptions() |
|
471 | |||
472 | /** |
||
473 | * @return $this |
||
474 | */ |
||
475 | 6 | protected function prepare() |
|
561 | |||
562 | 6 | public function serializeBody() |
|
575 | |||
576 | /** |
||
577 | * @param callable $callback |
||
578 | * @return $this |
||
579 | */ |
||
580 | 5 | public function onEnd(callable $callback) |
|
589 | |||
590 | /** |
||
591 | * @param array $options |
||
592 | * @return array |
||
593 | */ |
||
594 | 6 | protected static function filterAndRaw(array &$options) |
|
609 | |||
610 | /** |
||
611 | * @param bool $isMultiCurl |
||
612 | * @return Response |
||
613 | * @throws \Exception |
||
614 | */ |
||
615 | 6 | public function makeResponse($isMultiCurl = false) |
|
631 | |||
632 | |||
633 | } |
||
634 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.