Conditions | 15 |
Paths | 296 |
Total Lines | 114 |
Code Lines | 69 |
Lines | 8 |
Ratio | 7.02 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
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: