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 |
||
16 | class Request extends Http |
||
17 | { |
||
18 | const MAX_REDIRECTS_DEFAULT = 10; |
||
19 | protected static $curlAlias = array( |
||
20 | 'url' => 'CURLOPT_URL', |
||
21 | 'uri' => 'CURLOPT_URL', |
||
22 | 'debug' => 'CURLOPT_VERBOSE',//for debug verbose |
||
23 | 'method' => 'CURLOPT_CUSTOMREQUEST', |
||
24 | 'data' => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@' |
||
25 | 'ua' => 'CURLOPT_USERAGENT', |
||
26 | 'timeout' => 'CURLOPT_TIMEOUT', // (secs) 0 means indefinitely |
||
27 | 'connect_timeout' => 'CURLOPT_CONNECTTIMEOUT', |
||
28 | 'referer' => 'CURLOPT_REFERER', |
||
29 | 'binary' => 'CURLOPT_BINARYTRANSFER', |
||
30 | 'port' => 'CURLOPT_PORT', |
||
31 | 'header' => 'CURLOPT_HEADER', // TRUE:include header |
||
32 | 'headers' => 'CURLOPT_HTTPHEADER', // array |
||
33 | 'download' => 'CURLOPT_FILE', // writing file stream (using fopen()), default is STDOUT |
||
34 | 'upload' => 'CURLOPT_INFILE', // reading file stream |
||
35 | 'transfer' => 'CURLOPT_RETURNTRANSFER', // TRUE:return string; FALSE:output directly (curl_exec) |
||
36 | 'follow_location' => 'CURLOPT_FOLLOWLOCATION', |
||
37 | 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , |
||
38 | ); |
||
39 | protected static $logger; |
||
40 | public $curlHandle; |
||
41 | public |
||
42 | $uri, |
||
43 | $timeout, |
||
44 | $maxRedirects, |
||
45 | $followRedirects; |
||
46 | public $cert; |
||
47 | public $key; |
||
48 | public $passphrase; |
||
49 | public $encoding; |
||
50 | public $payload; |
||
51 | public $retryTimes; |
||
52 | |||
53 | /** |
||
54 | * @var int seconds |
||
55 | */ |
||
56 | public $retryDuration; |
||
57 | protected $options = array( |
||
58 | 'CURLOPT_MAXREDIRS' => 10, |
||
59 | 'header' => true, |
||
60 | 'method' => self::GET, |
||
61 | 'transfer' => true, |
||
62 | 'follow_location' => true, |
||
63 | 'timeout' => 0, |
||
64 | // 'ip' => null, //host, in string, .e.g: 172.16.1.1:888 |
||
65 | 'retry_times' => 1,//redo task when failed |
||
66 | 'retry_duration' => 0,//in seconds |
||
67 | ); |
||
68 | protected $endCallback; |
||
69 | protected $withURIQuery; |
||
70 | protected $hasInitialized = false; |
||
71 | |||
72 | protected function __construct() |
||
76 | |||
77 | public static function create() |
||
81 | |||
82 | public static function setLogger($logger) |
||
86 | |||
87 | /** |
||
88 | * Specify timeout |
||
89 | * @param float|int $timeout seconds to timeout the HTTP call |
||
90 | 2 | * @return Request |
|
91 | */ |
||
92 | 2 | public function timeout($timeout) |
|
97 | |||
98 | 1 | public function noFollow() |
|
102 | |||
103 | /** |
||
104 | * If the response is a 301 or 302 redirect, automatically |
||
105 | * send off another request to that location |
||
106 | * @param int $follow follow or not to follow or maximal number of redirects |
||
107 | 2 | * @return Request |
|
108 | */ |
||
109 | 2 | public function follow($follow) |
|
115 | 2 | ||
116 | 2 | public function endCallback() |
|
120 | |||
121 | public function hasEndCallback() |
||
125 | 2 | ||
126 | public function uri($uri) |
||
131 | |||
132 | public function hasCert() |
||
136 | |||
137 | 2 | /** |
|
138 | 2 | * Use Client Side Cert Authentication |
|
139 | 2 | * @param string $key file path to client key |
|
140 | * @param string $cert file path to client cert |
||
141 | * @param string $passphrase for client key |
||
142 | * @param string $encoding default PEM |
||
143 | * @return Request |
||
144 | */ |
||
145 | public function cert($cert, $key, $passphrase = null, $encoding = 'PEM') |
||
153 | |||
154 | public function body($payload, $mimeType = null) |
||
177 | 1 | ||
178 | public function addHeaders(array $headers) |
||
199 | /** |
||
200 | * @param $field alias or field name |
||
201 | * @return bool|mixed |
||
202 | */ |
||
203 | public function getIni($field) |
||
208 | 2 | ||
209 | 2 | /** |
|
210 | * @param $key |
||
211 | * @return mixed |
||
212 | */ |
||
213 | protected static function optionAlias($key) |
||
223 | 2 | ||
224 | 2 | public function addQuery($data) |
|
237 | 1 | ||
238 | 1 | public function post($uri, $payload = null, array $options = array()) |
|
242 | 1 | ||
243 | /** |
||
244 | * @param $uri |
||
245 | * @param null $payload |
||
246 | * @param array $options |
||
247 | * @param null $response |
||
248 | * @return string |
||
249 | */ |
||
250 | public function quickPost($uri, $payload = null, array $options = array(), &$response = null) |
||
255 | /* no body */ |
||
256 | |||
257 | View Code Duplication | protected function ini($method, $url, $data , array $options = array()) |
|
264 | |||
265 | 2 | public function addOptions(array $options = array()) |
|
271 | |||
272 | function put($uri, $payload = null, array $options = array()) |
||
276 | |||
277 | 2 | function patch($uri, $payload = null, array $options = array()) |
|
281 | |||
282 | public function get($uri, array $options = array()) |
||
286 | |||
287 | /** |
||
288 | 1 | * @param $uri |
|
289 | * @param array $options |
||
290 | 1 | * @param null $response |
|
291 | * @return string |
||
292 | */ |
||
293 | public function quickGet($uri, array $options = array(), &$response = null) |
||
298 | |||
299 | 1 | function options($uri, array $options = array()) |
|
303 | |||
304 | function head($uri, array $options = array()) |
||
308 | |||
309 | 1 | function delete($uri, array $options = array()) |
|
313 | |||
314 | function trace($uri, array $options = array()) |
||
318 | |||
319 | 1 | /** |
|
320 | * @return Response |
||
321 | 1 | */ |
|
322 | public function send() |
||
333 | |||
334 | public function applyOptions() |
||
342 | |||
343 | protected function prepare() |
||
409 | 1 | ||
410 | public function onEnd(callable $callback) |
||
419 | 2 | ||
420 | 1 | protected static function filterAndRaw(array &$options) |
|
433 | 2 | ||
434 | 2 | ||
435 | 2 | public function makeResponse($isMultiCurl = false) |
|
457 | 2 | ||
458 | 2 | private static function log(Response $response) |
|
467 | } |
||
468 |
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.