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 Jetpack_Client 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 Jetpack_Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class Jetpack_Client { |
||
4 | const WPCOM_JSON_API_VERSION = '1.1'; |
||
5 | |||
6 | /** |
||
7 | * Makes an authorized remote request using Jetpack_Signature |
||
8 | * |
||
9 | * @return array|WP_Error WP HTTP response on success |
||
10 | */ |
||
11 | public static function remote_request( $args, $body = null ) { |
||
12 | $defaults = array( |
||
13 | 'url' => '', |
||
14 | 'user_id' => 0, |
||
15 | 'blog_id' => 0, |
||
16 | 'auth_location' => JETPACK_CLIENT__AUTH_LOCATION, |
||
17 | 'method' => 'POST', |
||
18 | 'timeout' => 10, |
||
19 | 'redirection' => 0, |
||
20 | ); |
||
21 | |||
22 | $args = wp_parse_args( $args, $defaults ); |
||
23 | |||
24 | $args['blog_id'] = (int) $args['blog_id']; |
||
25 | |||
26 | if ( 'header' != $args['auth_location'] ) { |
||
27 | $args['auth_location'] = 'query_string'; |
||
28 | } |
||
29 | |||
30 | $token = Jetpack_Data::get_access_token( $args['user_id'] ); |
||
|
|||
31 | if ( !$token ) { |
||
32 | return new Jetpack_Error( 'missing_token' ); |
||
33 | } |
||
34 | |||
35 | $method = strtoupper( $args['method'] ); |
||
36 | |||
37 | $timeout = intval( $args['timeout'] ); |
||
38 | |||
39 | $redirection = $args['redirection']; |
||
40 | |||
41 | $request = compact( 'method', 'body', 'timeout', 'redirection' ); |
||
42 | |||
43 | @list( $token_key, $secret ) = explode( '.', $token->secret ); |
||
44 | if ( empty( $token ) || empty( $secret ) ) { |
||
45 | return new Jetpack_Error( 'malformed_token' ); |
||
46 | } |
||
47 | |||
48 | $token_key = sprintf( '%s:%d:%d', $token_key, JETPACK__API_VERSION, $token->external_user_id ); |
||
49 | |||
50 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; |
||
51 | |||
52 | $time_diff = (int) Jetpack_Options::get_option( 'time_diff' ); |
||
53 | $jetpack_signature = new Jetpack_Signature( $token->secret, $time_diff ); |
||
54 | |||
55 | $timestamp = time() + $time_diff; |
||
56 | |||
57 | if( function_exists( 'wp_generate_password' ) ) { |
||
58 | $nonce = wp_generate_password( 10, false ); |
||
59 | } else { |
||
60 | $nonce = substr( sha1( rand( 0, 1000000 ) ), 0, 10); |
||
61 | } |
||
62 | |||
63 | // Kind of annoying. Maybe refactor Jetpack_Signature to handle body-hashing |
||
64 | View Code Duplication | if ( is_null( $body ) ) { |
|
65 | $body_hash = ''; |
||
66 | } else { |
||
67 | if ( !is_string( $body ) ) { |
||
68 | return new Jetpack_Error( 'invalid_body', 'Body is malformed.' ); |
||
69 | } |
||
70 | $body_hash = jetpack_sha1_base64( $body ); |
||
71 | } |
||
72 | |||
73 | $auth = array( |
||
74 | 'token' => $token_key, |
||
75 | 'timestamp' => $timestamp, |
||
76 | 'nonce' => $nonce, |
||
77 | 'body-hash' => $body_hash, |
||
78 | ); |
||
79 | |||
80 | if ( false !== strpos( $args['url'], 'xmlrpc.php' ) ) { |
||
81 | $url_args = array( |
||
82 | 'for' => 'jetpack', |
||
83 | 'wpcom_blog_id' => Jetpack_Options::get_option( 'id' ), |
||
84 | ); |
||
85 | } else { |
||
86 | $url_args = array(); |
||
87 | } |
||
88 | |||
89 | if ( 'header' != $args['auth_location'] ) { |
||
90 | $url_args += $auth; |
||
91 | } |
||
92 | |||
93 | $url = add_query_arg( urlencode_deep( $url_args ), $args['url'] ); |
||
94 | $url = Jetpack::fix_url_for_bad_hosts( $url ); |
||
95 | |||
96 | $signature = $jetpack_signature->sign_request( $token_key, $timestamp, $nonce, $body_hash, $method, $url, $body, false ); |
||
97 | |||
98 | if ( !$signature || is_wp_error( $signature ) ) { |
||
99 | return $signature; |
||
100 | } |
||
101 | |||
102 | // Send an Authorization header so various caches/proxies do the right thing |
||
103 | $auth['signature'] = $signature; |
||
104 | $auth['version'] = JETPACK__VERSION; |
||
105 | $header_pieces = array(); |
||
106 | foreach ( $auth as $key => $value ) { |
||
107 | $header_pieces[] = sprintf( '%s="%s"', $key, $value ); |
||
108 | } |
||
109 | $request['headers'] = array( |
||
110 | 'Authorization' => "X_JETPACK " . join( ' ', $header_pieces ), |
||
111 | ); |
||
112 | |||
113 | // Make sure we keep the host when we do JETPACK__WPCOM_JSON_API_HOST requests. |
||
114 | $host = parse_url( $url, PHP_URL_HOST ); |
||
115 | if ( $host === JETPACK__WPCOM_JSON_API_HOST ) { |
||
116 | $request['headers']['Host'] = 'public-api.wordpress.com'; |
||
117 | } |
||
118 | |||
119 | if ( 'header' != $args['auth_location'] ) { |
||
120 | $url = add_query_arg( 'signature', urlencode( $signature ), $url ); |
||
121 | } |
||
122 | |||
123 | return Jetpack_Client::_wp_remote_request( $url, $request ); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Wrapper for wp_remote_request(). Turns off SSL verification for certain SSL errors. |
||
128 | * This is lame, but many, many, many hosts have misconfigured SSL. |
||
129 | * |
||
130 | * When Jetpack is registered, the jetpack_fallback_no_verify_ssl_certs option is set to the current time if: |
||
131 | * 1. a certificate error is found AND |
||
132 | * 2. not verifying the certificate works around the problem. |
||
133 | * |
||
134 | * The option is checked on each request. |
||
135 | * |
||
136 | * @internal |
||
137 | * @see Jetpack::fix_url_for_bad_hosts() |
||
138 | * |
||
139 | * @return array|WP_Error WP HTTP response on success |
||
140 | */ |
||
141 | public static function _wp_remote_request( $url, $args, $set_fallback = false ) { |
||
214 | |||
215 | public static function set_time_diff( &$response, $force_set = false ) { |
||
242 | |||
243 | /** |
||
244 | * Query the WordPress.com REST API using the blog token |
||
245 | * |
||
246 | * @param string $path |
||
247 | * @param string $version |
||
248 | * @param array $args |
||
249 | * @param string $body |
||
250 | * @return array|WP_Error $response Data. |
||
251 | */ |
||
252 | static function wpcom_json_api_request_as_blog( $path, $version = self::WPCOM_JSON_API_VERSION, $args = array(), $body = null ) { |
||
286 | |||
287 | } |
||
288 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: