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 WP_Http 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 WP_Http, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class WP_Http { |
||
29 | |||
30 | // Aliases for HTTP response codes. |
||
31 | const HTTP_CONTINUE = 100; |
||
32 | const SWITCHING_PROTOCOLS = 101; |
||
33 | const PROCESSING = 102; |
||
34 | |||
35 | const OK = 200; |
||
36 | const CREATED = 201; |
||
37 | const ACCEPTED = 202; |
||
38 | const NON_AUTHORITATIVE_INFORMATION = 203; |
||
39 | const NO_CONTENT = 204; |
||
40 | const RESET_CONTENT = 205; |
||
41 | const PARTIAL_CONTENT = 206; |
||
42 | const MULTI_STATUS = 207; |
||
43 | const IM_USED = 226; |
||
44 | |||
45 | const MULTIPLE_CHOICES = 300; |
||
46 | const MOVED_PERMANENTLY = 301; |
||
47 | const FOUND = 302; |
||
48 | const SEE_OTHER = 303; |
||
49 | const NOT_MODIFIED = 304; |
||
50 | const USE_PROXY = 305; |
||
51 | const RESERVED = 306; |
||
52 | const TEMPORARY_REDIRECT = 307; |
||
53 | const PERMANENT_REDIRECT = 308; |
||
54 | |||
55 | const BAD_REQUEST = 400; |
||
56 | const UNAUTHORIZED = 401; |
||
57 | const PAYMENT_REQUIRED = 402; |
||
58 | const FORBIDDEN = 403; |
||
59 | const NOT_FOUND = 404; |
||
60 | const METHOD_NOT_ALLOWED = 405; |
||
61 | const NOT_ACCEPTABLE = 406; |
||
62 | const PROXY_AUTHENTICATION_REQUIRED = 407; |
||
63 | const REQUEST_TIMEOUT = 408; |
||
64 | const CONFLICT = 409; |
||
65 | const GONE = 410; |
||
66 | const LENGTH_REQUIRED = 411; |
||
67 | const PRECONDITION_FAILED = 412; |
||
68 | const REQUEST_ENTITY_TOO_LARGE = 413; |
||
69 | const REQUEST_URI_TOO_LONG = 414; |
||
70 | const UNSUPPORTED_MEDIA_TYPE = 415; |
||
71 | const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
||
72 | const EXPECTATION_FAILED = 417; |
||
73 | const IM_A_TEAPOT = 418; |
||
74 | const MISDIRECTED_REQUEST = 421; |
||
75 | const UNPROCESSABLE_ENTITY = 422; |
||
76 | const LOCKED = 423; |
||
77 | const FAILED_DEPENDENCY = 424; |
||
78 | const UPGRADE_REQUIRED = 426; |
||
79 | const PRECONDITION_REQUIRED = 428; |
||
80 | const TOO_MANY_REQUESTS = 429; |
||
81 | const REQUEST_HEADER_FIELDS_TOO_LARGE = 431; |
||
82 | const UNAVAILABLE_FOR_LEGAL_REASONS = 451; |
||
83 | |||
84 | const INTERNAL_SERVER_ERROR = 500; |
||
85 | const NOT_IMPLEMENTED = 501; |
||
86 | const BAD_GATEWAY = 502; |
||
87 | const SERVICE_UNAVAILABLE = 503; |
||
88 | const GATEWAY_TIMEOUT = 504; |
||
89 | const HTTP_VERSION_NOT_SUPPORTED = 505; |
||
90 | const VARIANT_ALSO_NEGOTIATES = 506; |
||
91 | const INSUFFICIENT_STORAGE = 507; |
||
92 | const NOT_EXTENDED = 510; |
||
93 | const NETWORK_AUTHENTICATION_REQUIRED = 511; |
||
94 | |||
95 | /** |
||
96 | * Send an HTTP request to a URI. |
||
97 | * |
||
98 | * Please note: The only URI that are supported in the HTTP Transport implementation |
||
99 | * are the HTTP and HTTPS protocols. |
||
100 | * |
||
101 | * @access public |
||
102 | * @since 2.7.0 |
||
103 | * |
||
104 | * @param string $url The request URL. |
||
105 | * @param string|array $args { |
||
106 | * Optional. Array or string of HTTP request arguments. |
||
107 | * |
||
108 | * @type string $method Request method. Accepts 'GET', 'POST', 'HEAD', or 'PUT'. |
||
109 | * Some transports technically allow others, but should not be |
||
110 | * assumed. Default 'GET'. |
||
111 | * @type int $timeout How long the connection should stay open in seconds. Default 5. |
||
112 | * @type int $redirection Number of allowed redirects. Not supported by all transports |
||
113 | * Default 5. |
||
114 | * @type string $httpversion Version of the HTTP protocol to use. Accepts '1.0' and '1.1'. |
||
115 | * Default '1.0'. |
||
116 | * @type string $user-agent User-agent value sent. |
||
117 | * Default WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ). |
||
118 | * @type bool $reject_unsafe_urls Whether to pass URLs through wp_http_validate_url(). |
||
119 | * Default false. |
||
120 | * @type bool $blocking Whether the calling code requires the result of the request. |
||
121 | * If set to false, the request will be sent to the remote server, |
||
122 | * and processing returned to the calling code immediately, the caller |
||
123 | * will know if the request succeeded or failed, but will not receive |
||
124 | * any response from the remote server. Default true. |
||
125 | * @type string|array $headers Array or string of headers to send with the request. |
||
126 | * Default empty array. |
||
127 | * @type array $cookies List of cookies to send with the request. Default empty array. |
||
128 | * @type string|array $body Body to send with the request. Default null. |
||
129 | * @type bool $compress Whether to compress the $body when sending the request. |
||
130 | * Default false. |
||
131 | * @type bool $decompress Whether to decompress a compressed response. If set to false and |
||
132 | * compressed content is returned in the response anyway, it will |
||
133 | * need to be separately decompressed. Default true. |
||
134 | * @type bool $sslverify Whether to verify SSL for the request. Default true. |
||
135 | * @type string sslcertificates Absolute path to an SSL certificate .crt file. |
||
136 | * Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'. |
||
137 | * @type bool $stream Whether to stream to a file. If set to true and no filename was |
||
138 | * given, it will be droped it in the WP temp dir and its name will |
||
139 | * be set using the basename of the URL. Default false. |
||
140 | * @type string $filename Filename of the file to write to when streaming. $stream must be |
||
141 | * set to true. Default null. |
||
142 | * @type int $limit_response_size Size in bytes to limit the response to. Default null. |
||
143 | * |
||
144 | * } |
||
145 | * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. |
||
146 | * A WP_Error instance upon error. |
||
147 | */ |
||
148 | public function request( $url, $args = array() ) { |
||
426 | |||
427 | /** |
||
428 | * Normalizes cookies for using in Requests. |
||
429 | * |
||
430 | * @since 4.6.0 |
||
431 | * @access public |
||
432 | * @static |
||
433 | * |
||
434 | * @param array $cookies List of cookies to send with the request. |
||
435 | * @return Requests_Cookie_Jar Cookie holder object. |
||
436 | */ |
||
437 | public static function normalize_cookies( $cookies ) { |
||
450 | |||
451 | /** |
||
452 | * Match redirect behaviour to browser handling. |
||
453 | * |
||
454 | * Changes 302 redirects from POST to GET to match browser handling. Per |
||
455 | * RFC 7231, user agents can deviate from the strict reading of the |
||
456 | * specification for compatibility purposes. |
||
457 | * |
||
458 | * @since 4.6.0 |
||
459 | * @access public |
||
460 | * @static |
||
461 | * |
||
462 | * @param string $location URL to redirect to. |
||
463 | * @param array $headers Headers for the redirect. |
||
464 | * @param array $options Redirect request options. |
||
465 | * @param Requests_Response $original Response object. |
||
466 | */ |
||
467 | public static function browser_redirect_compatibility( $location, $headers, $data, &$options, $original ) { |
||
468 | // Browser compat |
||
469 | if ( $original->status_code === 302 ) { |
||
470 | $options['type'] = Requests::GET; |
||
471 | } |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Validate redirected URLs. |
||
476 | * |
||
477 | * @since 4.7.5 |
||
478 | * |
||
479 | * @throws Requests_Exception On unsuccessful URL validation |
||
480 | * @param string $location URL to redirect to. |
||
481 | */ |
||
482 | public static function validate_redirects( $location ) { |
||
487 | |||
488 | /** |
||
489 | * Tests which transports are capable of supporting the request. |
||
490 | * |
||
491 | * @since 3.2.0 |
||
492 | * @access public |
||
493 | * |
||
494 | * @param array $args Request arguments |
||
495 | * @param string $url URL to Request |
||
496 | * |
||
497 | * @return string|false Class name for the first transport that claims to support the request. False if no transport claims to support the request. |
||
498 | */ |
||
499 | public function _get_first_available_transport( $args, $url = null ) { |
||
530 | |||
531 | /** |
||
532 | * Dispatches a HTTP request to a supporting transport. |
||
533 | * |
||
534 | * Tests each transport in order to find a transport which matches the request arguments. |
||
535 | * Also caches the transport instance to be used later. |
||
536 | * |
||
537 | * The order for requests is cURL, and then PHP Streams. |
||
538 | * |
||
539 | * @since 3.2.0 |
||
540 | * |
||
541 | * @static |
||
542 | * @access private |
||
543 | * |
||
544 | * @param string $url URL to Request |
||
545 | * @param array $args Request arguments |
||
546 | * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
||
547 | */ |
||
548 | private function _dispatch_request( $url, $args ) { |
||
578 | |||
579 | /** |
||
580 | * Uses the POST HTTP method. |
||
581 | * |
||
582 | * Used for sending data that is expected to be in the body. |
||
583 | * |
||
584 | * @access public |
||
585 | * @since 2.7.0 |
||
586 | * |
||
587 | * @param string $url The request URL. |
||
588 | * @param string|array $args Optional. Override the defaults. |
||
589 | * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
||
590 | */ |
||
591 | public function post($url, $args = array()) { |
||
596 | |||
597 | /** |
||
598 | * Uses the GET HTTP method. |
||
599 | * |
||
600 | * Used for sending data that is expected to be in the body. |
||
601 | * |
||
602 | * @access public |
||
603 | * @since 2.7.0 |
||
604 | * |
||
605 | * @param string $url The request URL. |
||
606 | * @param string|array $args Optional. Override the defaults. |
||
607 | * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
||
608 | */ |
||
609 | public function get($url, $args = array()) { |
||
614 | |||
615 | /** |
||
616 | * Uses the HEAD HTTP method. |
||
617 | * |
||
618 | * Used for sending data that is expected to be in the body. |
||
619 | * |
||
620 | * @access public |
||
621 | * @since 2.7.0 |
||
622 | * |
||
623 | * @param string $url The request URL. |
||
624 | * @param string|array $args Optional. Override the defaults. |
||
625 | * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
||
626 | */ |
||
627 | public function head($url, $args = array()) { |
||
632 | |||
633 | /** |
||
634 | * Parses the responses and splits the parts into headers and body. |
||
635 | * |
||
636 | * @access public |
||
637 | * @static |
||
638 | * @since 2.7.0 |
||
639 | * |
||
640 | * @param string $strResponse The full response string |
||
641 | * @return array Array with 'headers' and 'body' keys. |
||
642 | */ |
||
643 | public static function processResponse($strResponse) { |
||
648 | |||
649 | /** |
||
650 | * Transform header string into an array. |
||
651 | * |
||
652 | * If an array is given then it is assumed to be raw header data with numeric keys with the |
||
653 | * headers as the values. No headers must be passed that were already processed. |
||
654 | * |
||
655 | * @access public |
||
656 | * @static |
||
657 | * @since 2.7.0 |
||
658 | * |
||
659 | * @param string|array $headers |
||
660 | * @param string $url The URL that was requested |
||
661 | * @return array Processed string headers. If duplicate headers are encountered, |
||
662 | * Then a numbered array is returned as the value of that header-key. |
||
663 | */ |
||
664 | public static function processHeaders( $headers, $url = '' ) { |
||
725 | |||
726 | /** |
||
727 | * Takes the arguments for a ::request() and checks for the cookie array. |
||
728 | * |
||
729 | * If it's found, then it upgrades any basic name => value pairs to WP_Http_Cookie instances, |
||
730 | * which are each parsed into strings and added to the Cookie: header (within the arguments array). |
||
731 | * Edits the array by reference. |
||
732 | * |
||
733 | * @access public |
||
734 | * @version 2.8.0 |
||
735 | * @static |
||
736 | * |
||
737 | * @param array $r Full array of args passed into ::request() |
||
738 | */ |
||
739 | public static function buildCookieHeader( &$r ) { |
||
756 | |||
757 | /** |
||
758 | * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification. |
||
759 | * |
||
760 | * Based off the HTTP http_encoding_dechunk function. |
||
761 | * |
||
762 | * @link https://tools.ietf.org/html/rfc2616#section-19.4.6 Process for chunked decoding. |
||
763 | * |
||
764 | * @access public |
||
765 | * @since 2.7.0 |
||
766 | * @static |
||
767 | * |
||
768 | * @param string $body Body content |
||
769 | * @return string Chunked decoded body on success or raw body on failure. |
||
770 | */ |
||
771 | public static function chunkTransferDecode( $body ) { |
||
800 | |||
801 | /** |
||
802 | * Block requests through the proxy. |
||
803 | * |
||
804 | * Those who are behind a proxy and want to prevent access to certain hosts may do so. This will |
||
805 | * prevent plugins from working and core functionality, if you don't include api.wordpress.org. |
||
806 | * |
||
807 | * You block external URL requests by defining WP_HTTP_BLOCK_EXTERNAL as true in your wp-config.php |
||
808 | * file and this will only allow localhost and your site to make requests. The constant |
||
809 | * WP_ACCESSIBLE_HOSTS will allow additional hosts to go through for requests. The format of the |
||
810 | * WP_ACCESSIBLE_HOSTS constant is a comma separated list of hostnames to allow, wildcard domains |
||
811 | * are supported, eg *.wordpress.org will allow for all subdomains of wordpress.org to be contacted. |
||
812 | * |
||
813 | * @since 2.8.0 |
||
814 | * @link https://core.trac.wordpress.org/ticket/8927 Allow preventing external requests. |
||
815 | * @link https://core.trac.wordpress.org/ticket/14636 Allow wildcard domains in WP_ACCESSIBLE_HOSTS |
||
816 | * |
||
817 | * @staticvar array|null $accessible_hosts |
||
818 | * @staticvar array $wildcard_regex |
||
819 | * |
||
820 | * @param string $uri URI of url. |
||
821 | * @return bool True to block, false to allow. |
||
822 | */ |
||
823 | public function block_request($uri) { |
||
869 | |||
870 | /** |
||
871 | * Used as a wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7. |
||
872 | * |
||
873 | * @access protected |
||
874 | * @deprecated 4.4.0 Use wp_parse_url() |
||
875 | * @see wp_parse_url() |
||
876 | * |
||
877 | * @param string $url The URL to parse. |
||
878 | * @return bool|array False on failure; Array of URL components on success; |
||
879 | * See parse_url()'s return values. |
||
880 | */ |
||
881 | protected static function parse_url( $url ) { |
||
885 | |||
886 | /** |
||
887 | * Converts a relative URL to an absolute URL relative to a given URL. |
||
888 | * |
||
889 | * If an Absolute URL is provided, no processing of that URL is done. |
||
890 | * |
||
891 | * @since 3.4.0 |
||
892 | * |
||
893 | * @static |
||
894 | * @access public |
||
895 | * |
||
896 | * @param string $maybe_relative_path The URL which might be relative |
||
897 | * @param string $url The URL which $maybe_relative_path is relative to |
||
898 | * @return string An Absolute URL, in a failure condition where the URL cannot be parsed, the relative URL will be returned. |
||
899 | */ |
||
900 | public static function make_absolute_url( $maybe_relative_path, $url ) { |
||
960 | |||
961 | /** |
||
962 | * Handles HTTP Redirects and follows them if appropriate. |
||
963 | * |
||
964 | * @since 3.7.0 |
||
965 | * |
||
966 | * @static |
||
967 | * |
||
968 | * @param string $url The URL which was requested. |
||
969 | * @param array $args The Arguments which were used to make the request. |
||
970 | * @param array $response The Response of the HTTP request. |
||
971 | * @return false|object False if no redirect is present, a WP_HTTP or WP_Error result otherwise. |
||
972 | */ |
||
973 | public static function handle_redirects( $url, $args, $response ) { |
||
1010 | |||
1011 | /** |
||
1012 | * Determines if a specified string represents an IP address or not. |
||
1013 | * |
||
1014 | * This function also detects the type of the IP address, returning either |
||
1015 | * '4' or '6' to represent a IPv4 and IPv6 address respectively. |
||
1016 | * This does not verify if the IP is a valid IP, only that it appears to be |
||
1017 | * an IP address. |
||
1018 | * |
||
1019 | * @link http://home.deds.nl/~aeron/regex/ for IPv6 regex |
||
1020 | * |
||
1021 | * @since 3.7.0 |
||
1022 | * @static |
||
1023 | * |
||
1024 | * @param string $maybe_ip A suspected IP address |
||
1025 | * @return integer|bool Upon success, '4' or '6' to represent a IPv4 or IPv6 address, false upon failure |
||
1026 | */ |
||
1027 | public static function is_ip_address( $maybe_ip ) { |
||
1036 | |||
1037 | } |
||
1038 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: