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 | 'timeout_ms' => 'CURLOPT_TIMEOUT_MS', // milliseconds, libcurl version > 7.36.0 , |
||
| 50 | /** |
||
| 51 | * private properties |
||
| 52 | */ |
||
| 53 | 'expectsMime' => null, //expected mime |
||
| 54 | 'sendMime' => null, //send mime |
||
| 55 | 'ip' => null,//specify ip to send request |
||
| 56 | 'callback' => null,//callback on end |
||
| 57 | |||
| 58 | ); |
||
| 59 | protected static $loggerHandler; |
||
| 60 | public |
||
| 61 | $curlHandle, |
||
| 62 | $uri, |
||
| 63 | $sendMime, |
||
| 64 | $expectedMime; |
||
| 65 | protected $options = array( |
||
| 66 | 'CURLOPT_MAXREDIRS' => 10, |
||
| 67 | 'CURLOPT_SSL_VERIFYPEER' => false,//for https |
||
| 68 | 'CURLOPT_SSL_VERIFYHOST' => 0,//for https |
||
| 69 | 'CURLOPT_IPRESOLVE' => CURL_IPRESOLVE_V4,//ipv4 first |
||
| 70 | 'header' => true, |
||
| 71 | 'method' => self::GET, |
||
| 72 | 'transfer' => true, |
||
| 73 | 'headers' => array(), |
||
| 74 | 'follow_location' => true, |
||
| 75 | 'timeout' => 0); |
||
| 76 | protected $endCallback; |
||
| 77 | protected $withURIQuery; |
||
| 78 | protected $hasInitialized = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Request constructor. |
||
| 82 | */ |
||
| 83 | 2 | protected function __construct() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return Request |
||
| 89 | */ |
||
| 90 | 2 | public static function create() |
|
| 91 | { |
||
| 92 | 2 | return new self; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param callable $handler |
||
| 97 | */ |
||
| 98 | 1 | public static function setLogHandler(callable $handler) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param $parsedComponents |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 2 | private static function combineUrl($parsedComponents) |
|
| 108 | { |
||
| 109 | 2 | $scheme = isset($parsedComponents['scheme']) ? $parsedComponents['scheme'] . '://' : ''; |
|
| 110 | 2 | $host = isset($parsedComponents['host']) ? $parsedComponents['host'] : ''; |
|
| 111 | 2 | $port = isset($parsedComponents['port']) ? ':' . $parsedComponents['port'] : ''; |
|
| 112 | 2 | $user = isset($parsedComponents['user']) ? $parsedComponents['user'] : ''; |
|
| 113 | 2 | $pass = isset($parsedComponents['pass']) ? ':' . $parsedComponents['pass'] : ''; |
|
| 114 | 2 | $pass = ($user || $pass) ? "$pass@" : ''; |
|
| 115 | 2 | $path = isset($parsedComponents['path']) ? $parsedComponents['path'] : ''; |
|
| 116 | 2 | $query = isset($parsedComponents['query']) ? '?' . $parsedComponents['query'] : ''; |
|
| 117 | 2 | $fragment = isset($parsedComponents['fragment']) ? '#' . $parsedComponents['fragment'] : ''; |
|
| 118 | 2 | return "$scheme$user$pass$host$port$path$query$fragment"; |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $mime |
||
| 123 | * @return $this |
||
| 124 | */ |
||
| 125 | 2 | public function expectsMime($mime = 'json') |
|
| 126 | { |
||
| 127 | 2 | $this->expectedMime = $mime; |
|
| 128 | 2 | return $this; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $mime |
||
| 133 | * @return Request |
||
| 134 | */ |
||
| 135 | 2 | public function sendMime($mime = 'json') |
|
| 136 | { |
||
| 137 | 2 | $this->sendMime = $mime; |
|
| 138 | 2 | $this->addHeader('Content-type', Mime::getFullMime($mime)); |
|
| 139 | 2 | return $this; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param $headerName |
||
| 144 | * @param $value , can be rawurlencode |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | 2 | public function addHeader($headerName, $value) |
|
| 148 | { |
||
| 149 | 2 | $this->options['headers'][] = $headerName . ': ' . $value; |
|
| 150 | 2 | return $this; |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param $uri |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function uri($uri) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param $timeout seconds, can be float |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | public function timeout($timeout) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param array $headers |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | 1 | public function addHeaders(array $headers) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | public function endCallback() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 2 | public function hasEndCallback() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @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 | */ |
||
| 216 | 2 | protected static function fullOption($key) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @param $data |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | 1 | public function addQuery($data) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param $uri |
||
| 247 | * @param null $payload |
||
| 248 | * @param array $options |
||
| 249 | * @return Request |
||
| 250 | */ |
||
| 251 | 1 | public function post($uri, $payload = null, array $options = array()) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param $method |
||
| 258 | * @param $url |
||
| 259 | * @param $data |
||
| 260 | * @param array $options |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | 2 | View Code Duplication | protected function ini($method, $url, $data, array $options = array()) |
| 270 | |||
| 271 | /** |
||
| 272 | * @param array $options |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | 2 | public function addOptions(array $options = array()) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @param $uri |
||
| 284 | * @param null $payload |
||
| 285 | * @param array $options |
||
| 286 | * @return Request |
||
| 287 | */ |
||
| 288 | 1 | 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 | 1 | function patch($uri, $payload = null, array $options = array()) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param $uri |
||
| 306 | * @param array $options |
||
| 307 | * @return Request |
||
| 308 | */ |
||
| 309 | 1 | public function get($uri, array $options = array()) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @param $uri |
||
| 316 | * @param array $options |
||
| 317 | * @return Request |
||
| 318 | */ |
||
| 319 | 1 | function options($uri, array $options = array()) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param $uri |
||
| 326 | * @param array $options |
||
| 327 | * @return Request |
||
| 328 | */ |
||
| 329 | 1 | function head($uri, array $options = array()) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param $uri |
||
| 336 | * @param array $options |
||
| 337 | * @return Request |
||
| 338 | */ |
||
| 339 | 1 | function delete($uri, array $options = array()) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param $uri |
||
| 346 | * @param array $options |
||
| 347 | * @return Request |
||
| 348 | */ |
||
| 349 | 2 | function trace($uri, array $options = array()) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @param bool $isMultiCurl |
||
| 356 | * @return Response |
||
| 357 | */ |
||
| 358 | 2 | public function send($isMultiCurl = false) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @return $this |
||
| 383 | */ |
||
| 384 | 2 | public function applyOptions() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @return $this |
||
| 395 | */ |
||
| 396 | 2 | protected function prepare() |
|
| 470 | |||
| 471 | 2 | public function serializeBody() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * @param callable $callback |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | 2 | public function onEnd(callable $callback) |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @param array $options |
||
| 500 | * @return array |
||
| 501 | */ |
||
| 502 | 2 | protected static function filterAndRaw(array &$options) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * @param bool $isMultiCurl |
||
| 520 | * @return Response |
||
| 521 | * @throws \Exception |
||
| 522 | */ |
||
| 523 | 2 | 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.