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 | 2 | public $encoding; |
|
| 50 | public $payload; |
||
| 51 | 2 | protected $options = array( |
|
| 52 | 'CURLOPT_MAXREDIRS' => 10, |
||
| 53 | 2 | 'header' => true, |
|
| 54 | 2 | 'method' => self::GET, |
|
| 55 | 'transfer' => true, |
||
| 56 | 'follow_location' => true, |
||
| 57 | 1 | 'timeout' => 0); |
|
| 58 | 1 | protected $endCallback; |
|
| 59 | protected $withURIQuery; |
||
| 60 | protected $hasInitialized = false; |
||
| 61 | 2 | ||
| 62 | 2 | protected function __construct() |
|
| 66 | 2 | ||
| 67 | public static function create() |
||
| 71 | |||
| 72 | public static function setLogger($logger) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Specify timeout |
||
| 79 | * @param float|int $timeout seconds to timeout the HTTP call |
||
| 80 | * @return Request |
||
| 81 | 2 | */ |
|
| 82 | public function timeout($timeout) |
||
| 87 | |||
| 88 | 2 | public function noFollow() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * If the response is a 301 or 302 redirect, automatically |
||
| 95 | 1 | * send off another request to that location |
|
| 96 | 1 | * @param int $follow follow or not to follow or maximal number of redirects |
|
| 97 | 1 | * @return Request |
|
| 98 | 1 | */ |
|
| 99 | 1 | public function follow(int $follow = self::MAX_REDIRECTS_DEFAULT) |
|
| 105 | 1 | ||
| 106 | public function endCallback() |
||
| 110 | |||
| 111 | public function hasEndCallback() |
||
| 115 | |||
| 116 | 2 | public function uri($uri) |
|
| 121 | 2 | ||
| 122 | public function hasCert() |
||
| 126 | |||
| 127 | 2 | /** |
|
| 128 | 1 | * Use Client Side Cert Authentication |
|
| 129 | 1 | * @param string $key file path to client key |
|
| 130 | * @param string $cert file path to client cert |
||
| 131 | 2 | * @param string $passphrase for client key |
|
| 132 | 2 | * @param string $encoding default PEM |
|
| 133 | 2 | * @return Request |
|
| 134 | */ |
||
| 135 | public function cert($cert, $key, $passphrase = null, $encoding = 'PEM') |
||
| 143 | |||
| 144 | public function body($payload, $mimeType = null) |
||
| 167 | |||
| 168 | public function addHeaders(array $headers) |
||
| 189 | 2 | /** |
|
| 190 | * @param $field alias or field name |
||
| 191 | 2 | * @return bool|mixed |
|
| 192 | 1 | */ |
|
| 193 | 1 | public function getIni($field) |
|
| 198 | |||
| 199 | /** |
||
| 200 | 1 | * @param $key |
|
| 201 | 1 | * @return mixed |
|
| 202 | 1 | */ |
|
| 203 | protected static function optionAlias($key) |
||
| 213 | |||
| 214 | public function addQuery($data) |
||
| 227 | |||
| 228 | public function post($uri, $payload = null, array $options = array()) |
||
| 232 | 2 | ||
| 233 | 2 | /* no body */ |
|
| 234 | 2 | ||
| 235 | 2 | View Code Duplication | protected function ini($method, $url, $data , array $options = array()) |
| 242 | |||
| 243 | public function addOptions(array $options = array()) |
||
| 249 | 2 | ||
| 250 | 2 | function put($uri, $payload = null, array $options = array()) |
|
| 254 | 2 | ||
| 255 | function patch($uri, $payload = null, array $options = array()) |
||
| 259 | 2 | ||
| 260 | 2 | public function get($uri, array $options = array()) |
|
| 264 | |||
| 265 | function options($uri, array $options = array()) |
||
| 269 | |||
| 270 | function head($uri, array $options = array()) |
||
| 274 | |||
| 275 | function delete($uri, array $options = array()) |
||
| 279 | |||
| 280 | function trace($uri, array $options = array()) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return Response |
||
| 287 | */ |
||
| 288 | public function send() |
||
| 299 | |||
| 300 | public function applyOptions() |
||
| 308 | |||
| 309 | protected function prepare() |
||
| 368 | |||
| 369 | public function onEnd(callable $callback) |
||
| 378 | |||
| 379 | protected static function filterAndRaw(array &$options) |
||
| 392 | |||
| 393 | public function makeResponse($isMultiCurl = false) |
||
| 406 | |||
| 407 | private static function log(Response $response) |
||
| 416 | } |
||
| 417 |
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.