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 |
||
21 | class WP_Http { |
||
22 | |||
23 | // Aliases for HTTP response codes. |
||
24 | const HTTP_CONTINUE = 100; |
||
25 | const SWITCHING_PROTOCOLS = 101; |
||
26 | const PROCESSING = 102; |
||
27 | |||
28 | const OK = 200; |
||
29 | const CREATED = 201; |
||
30 | const ACCEPTED = 202; |
||
31 | const NON_AUTHORITATIVE_INFORMATION = 203; |
||
32 | const NO_CONTENT = 204; |
||
33 | const RESET_CONTENT = 205; |
||
34 | const PARTIAL_CONTENT = 206; |
||
35 | const MULTI_STATUS = 207; |
||
36 | const IM_USED = 226; |
||
37 | |||
38 | const MULTIPLE_CHOICES = 300; |
||
39 | const MOVED_PERMANENTLY = 301; |
||
40 | const FOUND = 302; |
||
41 | const SEE_OTHER = 303; |
||
42 | const NOT_MODIFIED = 304; |
||
43 | const USE_PROXY = 305; |
||
44 | const RESERVED = 306; |
||
45 | const TEMPORARY_REDIRECT = 307; |
||
46 | const PERMANENT_REDIRECT = 308; |
||
47 | |||
48 | const BAD_REQUEST = 400; |
||
49 | const UNAUTHORIZED = 401; |
||
50 | const PAYMENT_REQUIRED = 402; |
||
51 | const FORBIDDEN = 403; |
||
52 | const NOT_FOUND = 404; |
||
53 | const METHOD_NOT_ALLOWED = 405; |
||
54 | const NOT_ACCEPTABLE = 406; |
||
55 | const PROXY_AUTHENTICATION_REQUIRED = 407; |
||
56 | const REQUEST_TIMEOUT = 408; |
||
57 | const CONFLICT = 409; |
||
58 | const GONE = 410; |
||
59 | const LENGTH_REQUIRED = 411; |
||
60 | const PRECONDITION_FAILED = 412; |
||
61 | const REQUEST_ENTITY_TOO_LARGE = 413; |
||
62 | const REQUEST_URI_TOO_LONG = 414; |
||
63 | const UNSUPPORTED_MEDIA_TYPE = 415; |
||
64 | const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
||
65 | const EXPECTATION_FAILED = 417; |
||
66 | const IM_A_TEAPOT = 418; |
||
67 | const MISDIRECTED_REQUEST = 421; |
||
68 | const UNPROCESSABLE_ENTITY = 422; |
||
69 | const LOCKED = 423; |
||
70 | const FAILED_DEPENDENCY = 424; |
||
71 | const UPGRADE_REQUIRED = 426; |
||
72 | const PRECONDITION_REQUIRED = 428; |
||
73 | const TOO_MANY_REQUESTS = 429; |
||
74 | const REQUEST_HEADER_FIELDS_TOO_LARGE = 431; |
||
75 | const UNAVAILABLE_FOR_LEGAL_REASONS = 451; |
||
76 | |||
77 | const INTERNAL_SERVER_ERROR = 500; |
||
78 | const NOT_IMPLEMENTED = 501; |
||
79 | const BAD_GATEWAY = 502; |
||
80 | const SERVICE_UNAVAILABLE = 503; |
||
81 | const GATEWAY_TIMEOUT = 504; |
||
82 | const HTTP_VERSION_NOT_SUPPORTED = 505; |
||
83 | const VARIANT_ALSO_NEGOTIATES = 506; |
||
84 | const INSUFFICIENT_STORAGE = 507; |
||
85 | const NOT_EXTENDED = 510; |
||
86 | const NETWORK_AUTHENTICATION_REQUIRED = 511; |
||
87 | |||
88 | /** |
||
89 | * Send an HTTP request to a URI. |
||
90 | * |
||
91 | * Please note: The only URI that are supported in the HTTP Transport implementation |
||
92 | * are the HTTP and HTTPS protocols. |
||
93 | * |
||
94 | * @access public |
||
95 | * @since 2.7.0 |
||
96 | * |
||
97 | * @global string $wp_version |
||
98 | * |
||
99 | * @param string $url The request URL. |
||
100 | * @param string|array $args { |
||
101 | * Optional. Array or string of HTTP request arguments. |
||
102 | * |
||
103 | * @type string $method Request method. Accepts 'GET', 'POST', 'HEAD', or 'PUT'. |
||
104 | * Some transports technically allow others, but should not be |
||
105 | * assumed. Default 'GET'. |
||
106 | * @type int $timeout How long the connection should stay open in seconds. Default 5. |
||
107 | * @type int $redirection Number of allowed redirects. Not supported by all transports |
||
108 | * Default 5. |
||
109 | * @type string $httpversion Version of the HTTP protocol to use. Accepts '1.0' and '1.1'. |
||
110 | * Default '1.0'. |
||
111 | * @type string $user-agent User-agent value sent. |
||
112 | * Default WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ). |
||
113 | * @type bool $reject_unsafe_urls Whether to pass URLs through {@see wp_http_validate_url()}. |
||
114 | * Default false. |
||
115 | * @type bool $blocking Whether the calling code requires the result of the request. |
||
116 | * If set to false, the request will be sent to the remote server, |
||
117 | * and processing returned to the calling code immediately, the caller |
||
118 | * will know if the request succeeded or failed, but will not receive |
||
119 | * any response from the remote server. Default true. |
||
120 | * @type string|array $headers Array or string of headers to send with the request. |
||
121 | * Default empty array. |
||
122 | * @type array $cookies List of cookies to send with the request. Default empty array. |
||
123 | * @type string|array $body Body to send with the request. Default null. |
||
124 | * @type bool $compress Whether to compress the $body when sending the request. |
||
125 | * Default false. |
||
126 | * @type bool $decompress Whether to decompress a compressed response. If set to false and |
||
127 | * compressed content is returned in the response anyway, it will |
||
128 | * need to be separately decompressed. Default true. |
||
129 | * @type bool $sslverify Whether to verify SSL for the request. Default true. |
||
130 | * @type string sslcertificates Absolute path to an SSL certificate .crt file. |
||
131 | * Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'. |
||
132 | * @type bool $stream Whether to stream to a file. If set to true and no filename was |
||
133 | * given, it will be droped it in the WP temp dir and its name will |
||
134 | * be set using the basename of the URL. Default false. |
||
135 | * @type string $filename Filename of the file to write to when streaming. $stream must be |
||
136 | * set to true. Default null. |
||
137 | * @type int $limit_response_size Size in bytes to limit the response to. Default null. |
||
138 | * |
||
139 | * } |
||
140 | * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. |
||
141 | * A WP_Error instance upon error. |
||
142 | */ |
||
143 | public function request( $url, $args = array() ) { |
||
361 | |||
362 | /** |
||
363 | * Tests which transports are capable of supporting the request. |
||
364 | * |
||
365 | * @since 3.2.0 |
||
366 | * @access public |
||
367 | * |
||
368 | * @param array $args Request arguments |
||
369 | * @param string $url URL to Request |
||
370 | * |
||
371 | * @return string|false Class name for the first transport that claims to support the request. False if no transport claims to support the request. |
||
372 | */ |
||
373 | public function _get_first_available_transport( $args, $url = null ) { |
||
404 | |||
405 | /** |
||
406 | * Dispatches a HTTP request to a supporting transport. |
||
407 | * |
||
408 | * Tests each transport in order to find a transport which matches the request arguments. |
||
409 | * Also caches the transport instance to be used later. |
||
410 | * |
||
411 | * The order for requests is cURL, and then PHP Streams. |
||
412 | * |
||
413 | * @since 3.2.0 |
||
414 | * |
||
415 | * @static |
||
416 | * @access private |
||
417 | * |
||
418 | * @param string $url URL to Request |
||
419 | * @param array $args Request arguments |
||
420 | * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
||
421 | */ |
||
422 | private function _dispatch_request( $url, $args ) { |
||
462 | |||
463 | /** |
||
464 | * Uses the POST HTTP method. |
||
465 | * |
||
466 | * Used for sending data that is expected to be in the body. |
||
467 | * |
||
468 | * @access public |
||
469 | * @since 2.7.0 |
||
470 | * |
||
471 | * @param string $url The request URL. |
||
472 | * @param string|array $args Optional. Override the defaults. |
||
473 | * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
||
474 | */ |
||
475 | public function post($url, $args = array()) { |
||
480 | |||
481 | /** |
||
482 | * Uses the GET HTTP method. |
||
483 | * |
||
484 | * Used for sending data that is expected to be in the body. |
||
485 | * |
||
486 | * @access public |
||
487 | * @since 2.7.0 |
||
488 | * |
||
489 | * @param string $url The request URL. |
||
490 | * @param string|array $args Optional. Override the defaults. |
||
491 | * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
||
492 | */ |
||
493 | public function get($url, $args = array()) { |
||
498 | |||
499 | /** |
||
500 | * Uses the HEAD HTTP method. |
||
501 | * |
||
502 | * Used for sending data that is expected to be in the body. |
||
503 | * |
||
504 | * @access public |
||
505 | * @since 2.7.0 |
||
506 | * |
||
507 | * @param string $url The request URL. |
||
508 | * @param string|array $args Optional. Override the defaults. |
||
509 | * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error |
||
510 | */ |
||
511 | public function head($url, $args = array()) { |
||
516 | |||
517 | /** |
||
518 | * Parses the responses and splits the parts into headers and body. |
||
519 | * |
||
520 | * @access public |
||
521 | * @static |
||
522 | * @since 2.7.0 |
||
523 | * |
||
524 | * @param string $strResponse The full response string |
||
525 | * @return array Array with 'headers' and 'body' keys. |
||
526 | */ |
||
527 | public static function processResponse($strResponse) { |
||
532 | |||
533 | /** |
||
534 | * Transform header string into an array. |
||
535 | * |
||
536 | * If an array is given then it is assumed to be raw header data with numeric keys with the |
||
537 | * headers as the values. No headers must be passed that were already processed. |
||
538 | * |
||
539 | * @access public |
||
540 | * @static |
||
541 | * @since 2.7.0 |
||
542 | * |
||
543 | * @param string|array $headers |
||
544 | * @param string $url The URL that was requested |
||
545 | * @return array Processed string headers. If duplicate headers are encountered, |
||
546 | * Then a numbered array is returned as the value of that header-key. |
||
547 | */ |
||
548 | public static function processHeaders( $headers, $url = '' ) { |
||
609 | |||
610 | /** |
||
611 | * Takes the arguments for a ::request() and checks for the cookie array. |
||
612 | * |
||
613 | * If it's found, then it upgrades any basic name => value pairs to WP_Http_Cookie instances, |
||
614 | * which are each parsed into strings and added to the Cookie: header (within the arguments array). |
||
615 | * Edits the array by reference. |
||
616 | * |
||
617 | * @access public |
||
618 | * @version 2.8.0 |
||
619 | * @static |
||
620 | * |
||
621 | * @param array $r Full array of args passed into ::request() |
||
622 | */ |
||
623 | public static function buildCookieHeader( &$r ) { |
||
640 | |||
641 | /** |
||
642 | * Decodes chunk transfer-encoding, based off the HTTP 1.1 specification. |
||
643 | * |
||
644 | * Based off the HTTP http_encoding_dechunk function. |
||
645 | * |
||
646 | * @link http://tools.ietf.org/html/rfc2616#section-19.4.6 Process for chunked decoding. |
||
647 | * |
||
648 | * @access public |
||
649 | * @since 2.7.0 |
||
650 | * @static |
||
651 | * |
||
652 | * @param string $body Body content |
||
653 | * @return string Chunked decoded body on success or raw body on failure. |
||
654 | */ |
||
655 | public static function chunkTransferDecode( $body ) { |
||
684 | |||
685 | /** |
||
686 | * Block requests through the proxy. |
||
687 | * |
||
688 | * Those who are behind a proxy and want to prevent access to certain hosts may do so. This will |
||
689 | * prevent plugins from working and core functionality, if you don't include api.wordpress.org. |
||
690 | * |
||
691 | * You block external URL requests by defining WP_HTTP_BLOCK_EXTERNAL as true in your wp-config.php |
||
692 | * file and this will only allow localhost and your site to make requests. The constant |
||
693 | * WP_ACCESSIBLE_HOSTS will allow additional hosts to go through for requests. The format of the |
||
694 | * WP_ACCESSIBLE_HOSTS constant is a comma separated list of hostnames to allow, wildcard domains |
||
695 | * are supported, eg *.wordpress.org will allow for all subdomains of wordpress.org to be contacted. |
||
696 | * |
||
697 | * @since 2.8.0 |
||
698 | * @link https://core.trac.wordpress.org/ticket/8927 Allow preventing external requests. |
||
699 | * @link https://core.trac.wordpress.org/ticket/14636 Allow wildcard domains in WP_ACCESSIBLE_HOSTS |
||
700 | * |
||
701 | * @staticvar array|null $accessible_hosts |
||
702 | * @staticvar array $wildcard_regex |
||
703 | * |
||
704 | * @param string $uri URI of url. |
||
705 | * @return bool True to block, false to allow. |
||
706 | */ |
||
707 | public function block_request($uri) { |
||
753 | |||
754 | /** |
||
755 | * Used as a wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7. |
||
756 | * |
||
757 | * @access protected |
||
758 | * @deprecated 4.4.0 Use wp_parse_url() |
||
759 | * @see wp_parse_url() |
||
760 | * |
||
761 | * @param string $url The URL to parse. |
||
762 | * @return bool|array False on failure; Array of URL components on success; |
||
763 | * See parse_url()'s return values. |
||
764 | */ |
||
765 | protected static function parse_url( $url ) { |
||
769 | |||
770 | /** |
||
771 | * Converts a relative URL to an absolute URL relative to a given URL. |
||
772 | * |
||
773 | * If an Absolute URL is provided, no processing of that URL is done. |
||
774 | * |
||
775 | * @since 3.4.0 |
||
776 | * |
||
777 | * @static |
||
778 | * @access public |
||
779 | * |
||
780 | * @param string $maybe_relative_path The URL which might be relative |
||
781 | * @param string $url The URL which $maybe_relative_path is relative to |
||
782 | * @return string An Absolute URL, in a failure condition where the URL cannot be parsed, the relative URL will be returned. |
||
783 | */ |
||
784 | public static function make_absolute_url( $maybe_relative_path, $url ) { |
||
844 | |||
845 | /** |
||
846 | * Handles HTTP Redirects and follows them if appropriate. |
||
847 | * |
||
848 | * @since 3.7.0 |
||
849 | * |
||
850 | * @static |
||
851 | * |
||
852 | * @param string $url The URL which was requested. |
||
853 | * @param array $args The Arguments which were used to make the request. |
||
854 | * @param array $response The Response of the HTTP request. |
||
855 | * @return false|object False if no redirect is present, a WP_HTTP or WP_Error result otherwise. |
||
856 | */ |
||
857 | public static function handle_redirects( $url, $args, $response ) { |
||
894 | |||
895 | /** |
||
896 | * Determines if a specified string represents an IP address or not. |
||
897 | * |
||
898 | * This function also detects the type of the IP address, returning either |
||
899 | * '4' or '6' to represent a IPv4 and IPv6 address respectively. |
||
900 | * This does not verify if the IP is a valid IP, only that it appears to be |
||
901 | * an IP address. |
||
902 | * |
||
903 | * @link http://home.deds.nl/~aeron/regex/ for IPv6 regex |
||
904 | * |
||
905 | * @since 3.7.0 |
||
906 | * @static |
||
907 | * |
||
908 | * @param string $maybe_ip A suspected IP address |
||
909 | * @return integer|bool Upon success, '4' or '6' to represent a IPv4 or IPv6 address, false upon failure |
||
910 | */ |
||
911 | public static function is_ip_address( $maybe_ip ) { |
||
920 | |||
921 | } |
||
922 |
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: