| Conditions | 15 |
| Paths | 296 |
| Total Lines | 115 |
| Code Lines | 70 |
| Lines | 8 |
| Ratio | 6.96 % |
| Changes | 0 | ||
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 | 'headers' => array(), |
||
| 21 | ); |
||
| 22 | |||
| 23 | $args = wp_parse_args( $args, $defaults ); |
||
| 24 | |||
| 25 | $args['blog_id'] = (int) $args['blog_id']; |
||
| 26 | |||
| 27 | if ( 'header' != $args['auth_location'] ) { |
||
| 28 | $args['auth_location'] = 'query_string'; |
||
| 29 | } |
||
| 30 | |||
| 31 | $token = Jetpack_Data::get_access_token( $args['user_id'] ); |
||
|
|
|||
| 32 | if ( !$token ) { |
||
| 33 | return new Jetpack_Error( 'missing_token' ); |
||
| 34 | } |
||
| 35 | |||
| 36 | $method = strtoupper( $args['method'] ); |
||
| 37 | |||
| 38 | $timeout = intval( $args['timeout'] ); |
||
| 39 | |||
| 40 | $redirection = $args['redirection']; |
||
| 41 | |||
| 42 | $request = compact( 'method', 'body', 'timeout', 'redirection' ); |
||
| 43 | |||
| 44 | @list( $token_key, $secret ) = explode( '.', $token->secret ); |
||
| 45 | if ( empty( $token ) || empty( $secret ) ) { |
||
| 46 | return new Jetpack_Error( 'malformed_token' ); |
||
| 47 | } |
||
| 48 | |||
| 49 | $token_key = sprintf( '%s:%d:%d', $token_key, JETPACK__API_VERSION, $token->external_user_id ); |
||
| 50 | |||
| 51 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; |
||
| 52 | |||
| 53 | $time_diff = (int) Jetpack_Options::get_option( 'time_diff' ); |
||
| 54 | $jetpack_signature = new Jetpack_Signature( $token->secret, $time_diff ); |
||
| 55 | |||
| 56 | $timestamp = time() + $time_diff; |
||
| 57 | |||
| 58 | if( function_exists( 'wp_generate_password' ) ) { |
||
| 59 | $nonce = wp_generate_password( 10, false ); |
||
| 60 | } else { |
||
| 61 | $nonce = substr( sha1( rand( 0, 1000000 ) ), 0, 10); |
||
| 62 | } |
||
| 63 | |||
| 64 | // Kind of annoying. Maybe refactor Jetpack_Signature to handle body-hashing |
||
| 65 | View Code Duplication | if ( is_null( $body ) ) { |
|
| 66 | $body_hash = ''; |
||
| 67 | } else { |
||
| 68 | if ( !is_string( $body ) ) { |
||
| 69 | return new Jetpack_Error( 'invalid_body', 'Body is malformed.' ); |
||
| 70 | } |
||
| 71 | $body_hash = jetpack_sha1_base64( $body ); |
||
| 72 | } |
||
| 73 | |||
| 74 | $auth = array( |
||
| 75 | 'token' => $token_key, |
||
| 76 | 'timestamp' => $timestamp, |
||
| 77 | 'nonce' => $nonce, |
||
| 78 | 'body-hash' => $body_hash, |
||
| 79 | ); |
||
| 80 | |||
| 81 | if ( false !== strpos( $args['url'], 'xmlrpc.php' ) ) { |
||
| 82 | $url_args = array( |
||
| 83 | 'for' => 'jetpack', |
||
| 84 | 'wpcom_blog_id' => Jetpack_Options::get_option( 'id' ), |
||
| 85 | ); |
||
| 86 | } else { |
||
| 87 | $url_args = array(); |
||
| 88 | } |
||
| 89 | |||
| 90 | if ( 'header' != $args['auth_location'] ) { |
||
| 91 | $url_args += $auth; |
||
| 92 | } |
||
| 93 | |||
| 94 | $url = add_query_arg( urlencode_deep( $url_args ), $args['url'] ); |
||
| 95 | $url = Jetpack::fix_url_for_bad_hosts( $url ); |
||
| 96 | |||
| 97 | $signature = $jetpack_signature->sign_request( $token_key, $timestamp, $nonce, $body_hash, $method, $url, $body, false ); |
||
| 98 | |||
| 99 | if ( !$signature || is_wp_error( $signature ) ) { |
||
| 100 | return $signature; |
||
| 101 | } |
||
| 102 | |||
| 103 | // Send an Authorization header so various caches/proxies do the right thing |
||
| 104 | $auth['signature'] = $signature; |
||
| 105 | $auth['version'] = JETPACK__VERSION; |
||
| 106 | $header_pieces = array(); |
||
| 107 | foreach ( $auth as $key => $value ) { |
||
| 108 | $header_pieces[] = sprintf( '%s="%s"', $key, $value ); |
||
| 109 | } |
||
| 110 | $request['headers'] = array_merge( $args['headers'], array( |
||
| 111 | 'Authorization' => "X_JETPACK " . join( ' ', $header_pieces ), |
||
| 112 | ) ); |
||
| 113 | |||
| 114 | // Make sure we keep the host when we do JETPACK__WPCOM_JSON_API_HOST requests. |
||
| 115 | $host = parse_url( $url, PHP_URL_HOST ); |
||
| 116 | if ( $host === JETPACK__WPCOM_JSON_API_HOST ) { |
||
| 117 | $request['headers']['Host'] = 'public-api.wordpress.com'; |
||
| 118 | } |
||
| 119 | |||
| 120 | if ( 'header' != $args['auth_location'] ) { |
||
| 121 | $url = add_query_arg( 'signature', urlencode( $signature ), $url ); |
||
| 122 | } |
||
| 123 | |||
| 124 | return Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 125 | } |
||
| 126 | |||
| 289 |
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: