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 | * |
||
29 | */ |
||
30 | const MAX_REDIRECTS_DEFAULT = 10; |
||
31 | protected static $curlAlias = array( |
||
32 | 'url' => 'CURLOPT_URL', |
||
33 | 'uri' => 'CURLOPT_URL', |
||
34 | 'debug' => 'CURLOPT_VERBOSE',//for debug verbose |
||
35 | 'method' => 'CURLOPT_CUSTOMREQUEST', |
||
36 | 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' |
||
37 | 'ua' => 'CURLOPT_USERAGENT', |
||
38 | 'timeout' => 'CURLOPT_TIMEOUT', // (secs) 0 means indefinitely |
||
39 | 'connect_timeout' => 'CURLOPT_CONNECTTIMEOUT', |
||
40 | 'referer' => 'CURLOPT_REFERER', |
||
41 | 'binary' => 'CURLOPT_BINARYTRANSFER', |
||
42 | 'port' => 'CURLOPT_PORT', |
||
43 | 'header' => 'CURLOPT_HEADER', // TRUE:include header |
||
44 | 'headers' => 'CURLOPT_HTTPHEADER', // array |
||
45 | 'download' => 'CURLOPT_FILE', // writing file stream (using fopen()), default is STDOUT |
||
46 | 'upload' => 'CURLOPT_INFILE', // reading file stream |
||
47 | 'transfer' => 'CURLOPT_RETURNTRANSFER', // TRUE:return string; FALSE:output directly (curl_exec) |
||
48 | 'follow_location' => 'CURLOPT_FOLLOWLOCATION', |
||
49 | 2 | 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , |
|
50 | /** |
||
51 | 2 | * private properties |
|
52 | */ |
||
53 | 2 | 'expectsMime' => null, //expected mime |
|
54 | 2 | 'sendMime' => null, //send mime |
|
55 | 'ip' => null,//specify ip to send request |
||
56 | 'callback' => null,//callback on end |
||
57 | 1 | ||
58 | 1 | ); |
|
59 | protected static $loggerHandler; |
||
60 | public |
||
61 | 2 | $curlHandle, |
|
62 | 2 | $uri, |
|
63 | $sendMime, |
||
64 | $expectedMime; |
||
65 | 2 | protected $options = array( |
|
66 | 2 | 'CURLOPT_MAXREDIRS' => 10, |
|
67 | 'CURLOPT_SSL_VERIFYPEER' => false,//for https |
||
68 | 'CURLOPT_SSL_VERIFYHOST' => 0,//for https |
||
69 | 2 | 'CURLOPT_IPRESOLVE' => CURL_IPRESOLVE_V4,//ipv4 first |
|
70 | 2 | 'header' => true, |
|
71 | 'method' => self::GET, |
||
72 | 'transfer' => true, |
||
73 | 2 | 'headers' => array(), |
|
74 | 2 | 'follow_location' => true, |
|
75 | 'timeout' => 0); |
||
76 | protected $endCallback; |
||
77 | protected $withURIQuery; |
||
78 | protected $hasInitialized = false; |
||
79 | |||
80 | /** |
||
81 | 2 | * Request constructor. |
|
82 | */ |
||
83 | protected function __construct() |
||
86 | |||
87 | /** |
||
88 | 2 | * @return Request |
|
89 | */ |
||
90 | public static function create() |
||
94 | |||
95 | 1 | /** |
|
96 | 1 | * @param callable $handler |
|
97 | 1 | */ |
|
98 | 1 | public static function setLogHandler(callable $handler) |
|
102 | |||
103 | /** |
||
104 | * @param $parsedComponents |
||
105 | 1 | * @return string |
|
106 | */ |
||
107 | private static function combineUrl($parsedComponents) |
||
120 | 2 | ||
121 | 2 | /** |
|
122 | * @param string $mime |
||
123 | * @return $this |
||
124 | 2 | */ |
|
125 | 2 | public function expectsMime($mime = 'json') |
|
130 | |||
131 | 2 | /** |
|
132 | 2 | * @param string $mime |
|
133 | 2 | * @return Request |
|
134 | */ |
||
135 | public function sendMime($mime = 'json') |
||
141 | |||
142 | /** |
||
143 | * @param $headerName |
||
144 | * @param $value , can be rawurlencode |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function addHeader($headerName, $value) |
||
152 | |||
153 | 1 | /** |
|
154 | 1 | * @param $uri |
|
155 | * @return $this |
||
156 | */ |
||
157 | public function uri($uri) |
||
162 | |||
163 | /** |
||
164 | * @param $timeout seconds, can be float |
||
165 | 2 | * @return $this |
|
166 | 2 | */ |
|
167 | public function timeout($timeout) |
||
172 | 1 | ||
173 | 1 | /** |
|
174 | 1 | * @param array $headers |
|
175 | 1 | * @return $this |
|
176 | 1 | */ |
|
177 | 1 | public function addHeaders(array $headers) |
|
184 | 2 | ||
185 | 2 | /** |
|
186 | 2 | * @return mixed |
|
187 | */ |
||
188 | public function endCallback() |
||
192 | 1 | ||
193 | 1 | /** |
|
194 | 1 | * @return bool |
|
195 | 1 | */ |
|
196 | 1 | public function hasEndCallback() |
|
200 | 1 | ||
201 | 1 | /** |
|
202 | 1 | * @param $field alias or field name |
|
203 | * @return bool|mixed |
||
204 | */ |
||
205 | 2 | public function getIni($field = null) |
|
211 | |||
212 | /** |
||
213 | * @param $key |
||
214 | * @return mixed |
||
215 | 2 | */ |
|
216 | 2 | protected static function fullOption($key) |
|
226 | 2 | ||
227 | /** |
||
228 | * @param $data |
||
229 | 2 | * @return $this |
|
230 | */ |
||
231 | public function addQuery($data) |
||
244 | |||
245 | /** |
||
246 | * @param $uri |
||
247 | 2 | * @param null $payload |
|
248 | 2 | * @param array $options |
|
249 | 2 | * @return Request |
|
250 | 2 | */ |
|
251 | 2 | public function post($uri, $payload = null, array $options = array()) |
|
255 | |||
256 | 2 | /** |
|
257 | 2 | * @param $method |
|
258 | 2 | * @param $url |
|
259 | 2 | * @param $data |
|
260 | 2 | * @param array $options |
|
261 | 2 | * @return $this |
|
262 | 2 | */ |
|
263 | View Code Duplication | protected function ini($method, $url, $data, array $options = array()) |
|
270 | |||
271 | /** |
||
272 | * @param array $options |
||
273 | * @return $this |
||
274 | */ |
||
275 | public function addOptions(array $options = array()) |
||
281 | |||
282 | /** |
||
283 | * @param $uri |
||
284 | * @param null $payload |
||
285 | * @param array $options |
||
286 | * @return Request |
||
287 | */ |
||
288 | function put($uri, $payload = null, array $options = array()) |
||
292 | |||
293 | /** |
||
294 | * @param $uri |
||
295 | * @param null $payload |
||
296 | * @param array $options |
||
297 | * @return Request |
||
298 | */ |
||
299 | function patch($uri, $payload = null, array $options = array()) |
||
303 | |||
304 | /** |
||
305 | * @param $uri |
||
306 | * @param array $options |
||
307 | * @return Request |
||
308 | */ |
||
309 | public function get($uri, array $options = array()) |
||
313 | |||
314 | /** |
||
315 | * @param $uri |
||
316 | * @param array $options |
||
317 | * @return Request |
||
318 | */ |
||
319 | function options($uri, array $options = array()) |
||
323 | |||
324 | /** |
||
325 | * @param $uri |
||
326 | * @param array $options |
||
327 | * @return Request |
||
328 | */ |
||
329 | function head($uri, array $options = array()) |
||
333 | |||
334 | /** |
||
335 | * @param $uri |
||
336 | * @param array $options |
||
337 | * @return Request |
||
338 | */ |
||
339 | function delete($uri, array $options = array()) |
||
343 | |||
344 | /** |
||
345 | * @param $uri |
||
346 | * @param array $options |
||
347 | * @return Request |
||
348 | */ |
||
349 | function trace($uri, array $options = array()) |
||
353 | |||
354 | /** |
||
355 | * @param bool $isMultiCurl |
||
356 | * @return Response |
||
357 | */ |
||
358 | public function send($isMultiCurl = false) |
||
380 | |||
381 | /** |
||
382 | * @return $this |
||
383 | */ |
||
384 | public function applyOptions() |
||
392 | |||
393 | /** |
||
394 | * @return $this |
||
395 | */ |
||
396 | protected function prepare() |
||
470 | |||
471 | public function serializeBody() |
||
483 | |||
484 | /** |
||
485 | * @param callable $callback |
||
486 | * @return $this |
||
487 | */ |
||
488 | public function onEnd(callable $callback) |
||
497 | |||
498 | /** |
||
499 | * @param array $options |
||
500 | * @return array |
||
501 | */ |
||
502 | protected static function filterAndRaw(array &$options) |
||
517 | |||
518 | /** |
||
519 | * @param bool $isMultiCurl |
||
520 | * @return Response |
||
521 | * @throws \Exception |
||
522 | */ |
||
523 | public function makeResponse($isMultiCurl = false) |
||
532 | |||
533 | |||
534 | } |
||
535 |
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.