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 Requests 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 Requests, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Requests { |
||
| 22 | /** |
||
| 23 | * POST method |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | const POST = 'POST'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * PUT method |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | const PUT = 'PUT'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * GET method |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | const GET = 'GET'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * HEAD method |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | const HEAD = 'HEAD'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * DELETE method |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | const DELETE = 'DELETE'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * OPTIONS method |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | const OPTIONS = 'OPTIONS'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * TRACE method |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | const TRACE = 'TRACE'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * PATCH method |
||
| 73 | * |
||
| 74 | * @link https://tools.ietf.org/html/rfc5789 |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | const PATCH = 'PATCH'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Default size of buffer size to read streams |
||
| 81 | * |
||
| 82 | * @var integer |
||
| 83 | */ |
||
| 84 | const BUFFER_SIZE = 1160; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Current version of Requests |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | const VERSION = '1.7'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Registered transport classes |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected static $transports = array(); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Selected transport name |
||
| 102 | * |
||
| 103 | * Use {@see get_transport()} instead |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | public static $transport = array(); |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Default certificate path. |
||
| 111 | * |
||
| 112 | * @see Requests::get_certificate_path() |
||
| 113 | * @see Requests::set_certificate_path() |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | protected static $certificate_path; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * This is a static class, do not instantiate it |
||
| 121 | * |
||
| 122 | * @codeCoverageIgnore |
||
| 123 | */ |
||
| 124 | private function __construct() {} |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Autoloader for Requests |
||
| 128 | * |
||
| 129 | * Register this with {@see register_autoloader()} if you'd like to avoid |
||
| 130 | * having to create your own. |
||
| 131 | * |
||
| 132 | * (You can also use `spl_autoload_register` directly if you'd prefer.) |
||
| 133 | * |
||
| 134 | * @codeCoverageIgnore |
||
| 135 | * |
||
| 136 | * @param string $class Class name to load |
||
| 137 | */ |
||
| 138 | public static function autoloader($class) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Register the built-in autoloader |
||
| 152 | * |
||
| 153 | * @codeCoverageIgnore |
||
| 154 | */ |
||
| 155 | public static function register_autoloader() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Register a transport |
||
| 161 | * |
||
| 162 | * @param string $transport Transport class to add, must support the Requests_Transport interface |
||
| 163 | */ |
||
| 164 | public static function add_transport($transport) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get a working transport |
||
| 177 | * |
||
| 178 | * @throws Requests_Exception If no valid transport is found (`notransport`) |
||
| 179 | * @return Requests_Transport |
||
| 180 | */ |
||
| 181 | protected static function get_transport($capabilities = array()) { |
||
| 219 | |||
| 220 | /**#@+ |
||
| 221 | * @see request() |
||
| 222 | * @param string $url |
||
| 223 | * @param array $headers |
||
| 224 | * @param array $options |
||
| 225 | * @return Requests_Response |
||
| 226 | */ |
||
| 227 | /** |
||
| 228 | * Send a GET request |
||
| 229 | */ |
||
| 230 | public static function get($url, $headers = array(), $options = array()) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Send a HEAD request |
||
| 236 | */ |
||
| 237 | public static function head($url, $headers = array(), $options = array()) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Send a DELETE request |
||
| 243 | */ |
||
| 244 | public static function delete($url, $headers = array(), $options = array()) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Send a TRACE request |
||
| 250 | */ |
||
| 251 | public static function trace($url, $headers = array(), $options = array()) { |
||
| 254 | /**#@-*/ |
||
| 255 | |||
| 256 | /**#@+ |
||
| 257 | * @see request() |
||
| 258 | * @param string $url |
||
| 259 | * @param array $headers |
||
| 260 | * @param array $data |
||
| 261 | * @param array $options |
||
| 262 | * @return Requests_Response |
||
| 263 | */ |
||
| 264 | /** |
||
| 265 | * Send a POST request |
||
| 266 | */ |
||
| 267 | public static function post($url, $headers = array(), $data = array(), $options = array()) { |
||
| 270 | /** |
||
| 271 | * Send a PUT request |
||
| 272 | */ |
||
| 273 | public static function put($url, $headers = array(), $data = array(), $options = array()) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Send an OPTIONS request |
||
| 279 | */ |
||
| 280 | public static function options($url, $headers = array(), $data = array(), $options = array()) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Send a PATCH request |
||
| 286 | * |
||
| 287 | * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the |
||
| 288 | * specification recommends that should send an ETag |
||
| 289 | * |
||
| 290 | * @link https://tools.ietf.org/html/rfc5789 |
||
| 291 | */ |
||
| 292 | public static function patch($url, $headers, $data = array(), $options = array()) { |
||
| 295 | /**#@-*/ |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Main interface for HTTP requests |
||
| 299 | * |
||
| 300 | * This method initiates a request and sends it via a transport before |
||
| 301 | * parsing. |
||
| 302 | * |
||
| 303 | * The `$options` parameter takes an associative array with the following |
||
| 304 | * options: |
||
| 305 | * |
||
| 306 | * - `timeout`: How long should we wait for a response? |
||
| 307 | * Note: for cURL, a minimum of 1 second applies, as DNS resolution |
||
| 308 | * operates at second-resolution only. |
||
| 309 | * (float, seconds with a millisecond precision, default: 10, example: 0.01) |
||
| 310 | * - `connect_timeout`: How long should we wait while trying to connect? |
||
| 311 | * (float, seconds with a millisecond precision, default: 10, example: 0.01) |
||
| 312 | * - `useragent`: Useragent to send to the server |
||
| 313 | * (string, default: php-requests/$version) |
||
| 314 | * - `follow_redirects`: Should we follow 3xx redirects? |
||
| 315 | * (boolean, default: true) |
||
| 316 | * - `redirects`: How many times should we redirect before erroring? |
||
| 317 | * (integer, default: 10) |
||
| 318 | * - `blocking`: Should we block processing on this request? |
||
| 319 | * (boolean, default: true) |
||
| 320 | * - `filename`: File to stream the body to instead. |
||
| 321 | * (string|boolean, default: false) |
||
| 322 | * - `auth`: Authentication handler or array of user/password details to use |
||
| 323 | * for Basic authentication |
||
| 324 | * (Requests_Auth|array|boolean, default: false) |
||
| 325 | * - `proxy`: Proxy details to use for proxy by-passing and authentication |
||
| 326 | * (Requests_Proxy|array|string|boolean, default: false) |
||
| 327 | * - `max_bytes`: Limit for the response body size. |
||
| 328 | * (integer|boolean, default: false) |
||
| 329 | * - `idn`: Enable IDN parsing |
||
| 330 | * (boolean, default: true) |
||
| 331 | * - `transport`: Custom transport. Either a class name, or a |
||
| 332 | * transport object. Defaults to the first working transport from |
||
| 333 | * {@see getTransport()} |
||
| 334 | * (string|Requests_Transport, default: {@see getTransport()}) |
||
| 335 | * - `hooks`: Hooks handler. |
||
| 336 | * (Requests_Hooker, default: new Requests_Hooks()) |
||
| 337 | * - `verify`: Should we verify SSL certificates? Allows passing in a custom |
||
| 338 | * certificate file as a string. (Using true uses the system-wide root |
||
| 339 | * certificate store instead, but this may have different behaviour |
||
| 340 | * across transports.) |
||
| 341 | * (string|boolean, default: library/Requests/Transport/cacert.pem) |
||
| 342 | * - `verifyname`: Should we verify the common name in the SSL certificate? |
||
| 343 | * (boolean: default, true) |
||
| 344 | * - `data_format`: How should we send the `$data` parameter? |
||
| 345 | * (string, one of 'query' or 'body', default: 'query' for |
||
| 346 | * HEAD/GET/DELETE, 'body' for POST/PUT/OPTIONS/PATCH) |
||
| 347 | * |
||
| 348 | * @throws Requests_Exception On invalid URLs (`nonhttp`) |
||
| 349 | * |
||
| 350 | * @param string $url URL to request |
||
| 351 | * @param array $headers Extra headers to send with the request |
||
| 352 | * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests |
||
| 353 | * @param string $type HTTP request type (use Requests constants) |
||
| 354 | * @param array $options Options for the request (see description for more information) |
||
| 355 | * @return Requests_Response |
||
| 356 | */ |
||
| 357 | public static function request($url, $headers = array(), $data = array(), $type = self::GET, $options = array()) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Send multiple HTTP requests simultaneously |
||
| 388 | * |
||
| 389 | * The `$requests` parameter takes an associative or indexed array of |
||
| 390 | * request fields. The key of each request can be used to match up the |
||
| 391 | * request with the returned data, or with the request passed into your |
||
| 392 | * `multiple.request.complete` callback. |
||
| 393 | * |
||
| 394 | * The request fields value is an associative array with the following keys: |
||
| 395 | * |
||
| 396 | * - `url`: Request URL Same as the `$url` parameter to |
||
| 397 | * {@see Requests::request} |
||
| 398 | * (string, required) |
||
| 399 | * - `headers`: Associative array of header fields. Same as the `$headers` |
||
| 400 | * parameter to {@see Requests::request} |
||
| 401 | * (array, default: `array()`) |
||
| 402 | * - `data`: Associative array of data fields or a string. Same as the |
||
| 403 | * `$data` parameter to {@see Requests::request} |
||
| 404 | * (array|string, default: `array()`) |
||
| 405 | * - `type`: HTTP request type (use Requests constants). Same as the `$type` |
||
| 406 | * parameter to {@see Requests::request} |
||
| 407 | * (string, default: `Requests::GET`) |
||
| 408 | * - `cookies`: Associative array of cookie name to value, or cookie jar. |
||
| 409 | * (array|Requests_Cookie_Jar) |
||
| 410 | * |
||
| 411 | * If the `$options` parameter is specified, individual requests will |
||
| 412 | * inherit options from it. This can be used to use a single hooking system, |
||
| 413 | * or set all the types to `Requests::POST`, for example. |
||
| 414 | * |
||
| 415 | * In addition, the `$options` parameter takes the following global options: |
||
| 416 | * |
||
| 417 | * - `complete`: A callback for when a request is complete. Takes two |
||
| 418 | * parameters, a Requests_Response/Requests_Exception reference, and the |
||
| 419 | * ID from the request array (Note: this can also be overridden on a |
||
| 420 | * per-request basis, although that's a little silly) |
||
| 421 | * (callback) |
||
| 422 | * |
||
| 423 | * @param array $requests Requests data (see description for more information) |
||
| 424 | * @param array $options Global and default options (see {@see Requests::request}) |
||
| 425 | * @return array Responses (either Requests_Response or a Requests_Exception object) |
||
| 426 | */ |
||
| 427 | public static function request_multiple($requests, $options = array()) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Get the default options |
||
| 497 | * |
||
| 498 | * @see Requests::request() for values returned by this method |
||
| 499 | * @param boolean $multirequest Is this a multirequest? |
||
| 500 | * @return array Default option values |
||
| 501 | */ |
||
| 502 | protected static function get_default_options($multirequest = false) { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Get default certificate path. |
||
| 532 | * |
||
| 533 | * @return string Default certificate path. |
||
| 534 | */ |
||
| 535 | public static function get_certificate_path() { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Set default certificate path. |
||
| 545 | * |
||
| 546 | * @param string $path Certificate path, pointing to a PEM file. |
||
| 547 | */ |
||
| 548 | public static function set_certificate_path( $path ) { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Set the default values |
||
| 554 | * |
||
| 555 | * @param string $url URL to request |
||
| 556 | * @param array $headers Extra headers to send with the request |
||
| 557 | * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests |
||
| 558 | * @param string $type HTTP request type |
||
| 559 | * @param array $options Options for the request |
||
| 560 | * @return array $options |
||
| 561 | */ |
||
| 562 | protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * HTTP response parser |
||
| 616 | * |
||
| 617 | * @throws Requests_Exception On missing head/body separator (`requests.no_crlf_separator`) |
||
| 618 | * @throws Requests_Exception On missing head/body separator (`noversion`) |
||
| 619 | * @throws Requests_Exception On missing head/body separator (`toomanyredirects`) |
||
| 620 | * |
||
| 621 | * @param string $headers Full response text including headers and body |
||
| 622 | * @param string $url Original request URL |
||
| 623 | * @param array $req_headers Original $headers array passed to {@link request()}, in case we need to follow redirects |
||
| 624 | * @param array $req_data Original $data array passed to {@link request()}, in case we need to follow redirects |
||
| 625 | * @param array $options Original $options array passed to {@link request()}, in case we need to follow redirects |
||
| 626 | * @return Requests_Response |
||
| 627 | */ |
||
| 628 | protected static function parse_response($headers, $url, $req_headers, $req_data, $options) { |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Callback for `transport.internal.parse_response` |
||
| 723 | * |
||
| 724 | * Internal use only. Converts a raw HTTP response to a Requests_Response |
||
| 725 | * while still executing a multiple request. |
||
| 726 | * |
||
| 727 | * @param string $response Full response text including headers and body (will be overwritten with Response instance) |
||
| 728 | * @param array $request Request data as passed into {@see Requests::request_multiple()} |
||
| 729 | * @return null `$response` is either set to a Requests_Response instance, or a Requests_Exception object |
||
| 730 | */ |
||
| 731 | public static function parse_multiple(&$response, $request) { |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Decoded a chunked body as per RFC 2616 |
||
| 746 | * |
||
| 747 | * @see https://tools.ietf.org/html/rfc2616#section-3.6.1 |
||
| 748 | * @param string $data Chunked body |
||
| 749 | * @return string Decoded body |
||
| 750 | */ |
||
| 751 | protected static function decode_chunked($data) { |
||
| 786 | // @codeCoverageIgnoreEnd |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Convert a key => value array to a 'key: value' array for headers |
||
| 790 | * |
||
| 791 | * @param array $array Dictionary of header values |
||
| 792 | * @return array List of headers |
||
| 793 | */ |
||
| 794 | public static function flatten($array) { |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Convert a key => value array to a 'key: value' array for headers |
||
| 804 | * |
||
| 805 | * @codeCoverageIgnore |
||
| 806 | * @deprecated Misspelling of {@see Requests::flatten} |
||
| 807 | * @param array $array Dictionary of header values |
||
| 808 | * @return array List of headers |
||
| 809 | */ |
||
| 810 | public static function flattern($array) { |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Decompress an encoded body |
||
| 816 | * |
||
| 817 | * Implements gzip, compress and deflate. Guesses which it is by attempting |
||
| 818 | * to decode. |
||
| 819 | * |
||
| 820 | * @param string $data Compressed data in one of the above formats |
||
| 821 | * @return string Decompressed string |
||
| 822 | */ |
||
| 823 | public static function decompress($data) { |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Decompression of deflated string while staying compatible with the majority of servers. |
||
| 847 | * |
||
| 848 | * Certain Servers will return deflated data with headers which PHP's gzinflate() |
||
| 849 | * function cannot handle out of the box. The following function has been created from |
||
| 850 | * various snippets on the gzinflate() PHP documentation. |
||
| 851 | * |
||
| 852 | * Warning: Magic numbers within. Due to the potential different formats that the compressed |
||
| 853 | * data may be returned in, some "magic offsets" are needed to ensure proper decompression |
||
| 854 | * takes place. For a simple progmatic way to determine the magic offset in use, see: |
||
| 855 | * https://core.trac.wordpress.org/ticket/18273 |
||
| 856 | * |
||
| 857 | * @since 2.8.1 |
||
| 858 | * @link https://core.trac.wordpress.org/ticket/18273 |
||
| 859 | * @link https://secure.php.net/manual/en/function.gzinflate.php#70875 |
||
| 860 | * @link https://secure.php.net/manual/en/function.gzinflate.php#77336 |
||
| 861 | * |
||
| 862 | * @param string $gzData String to decompress. |
||
| 863 | * @return string|bool False on failure. |
||
| 864 | */ |
||
| 865 | public static function compatible_gzinflate($gzData) { |
||
| 959 | |||
| 960 | public static function match_domain($host, $reference) { |
||
| 980 | } |
||
| 981 |