| Conditions | 21 |
| Paths | 48 |
| Total Lines | 90 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 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 |
||
| 159 | function get_token( $data ) { |
||
| 160 | $jetpack = $this->get_jetpack(); |
||
| 161 | $role = $jetpack->translate_current_user_to_role(); |
||
| 162 | |||
| 163 | if ( !$role ) { |
||
| 164 | return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) ); |
||
| 165 | } |
||
| 166 | |||
| 167 | $client_secret = Jetpack_Data::get_access_token(); |
||
| 168 | if ( !$client_secret ) { |
||
| 169 | return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) ); |
||
| 170 | } |
||
| 171 | |||
| 172 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
| 173 | |||
| 174 | $body = array( |
||
| 175 | 'client_id' => Jetpack_Options::get_option( 'id' ), |
||
| 176 | 'client_secret' => $client_secret->secret, |
||
| 177 | 'grant_type' => 'authorization_code', |
||
| 178 | 'code' => $data['code'], |
||
| 179 | 'redirect_uri' => add_query_arg( array( |
||
| 180 | 'action' => 'authorize', |
||
| 181 | '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
||
| 182 | 'redirect' => $redirect ? urlencode( $redirect ) : false, |
||
| 183 | ), menu_page_url( 'jetpack', false ) ), |
||
| 184 | ); |
||
| 185 | |||
| 186 | $args = array( |
||
| 187 | 'method' => 'POST', |
||
| 188 | 'body' => $body, |
||
| 189 | 'headers' => array( |
||
| 190 | 'Accept' => 'application/json', |
||
| 191 | ), |
||
| 192 | ); |
||
| 193 | $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'token' ) ), $args ); |
||
| 194 | |||
| 195 | if ( is_wp_error( $response ) ) { |
||
| 196 | return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() ); |
||
| 197 | } |
||
| 198 | |||
| 199 | $code = wp_remote_retrieve_response_code( $response ); |
||
| 200 | $entity = wp_remote_retrieve_body( $response ); |
||
| 201 | |||
| 202 | if ( $entity ) |
||
| 203 | $json = json_decode( $entity ); |
||
| 204 | else |
||
| 205 | $json = false; |
||
| 206 | |||
| 207 | if ( 200 != $code || !empty( $json->error ) ) { |
||
| 208 | if ( empty( $json->error ) ) |
||
| 209 | return new Jetpack_Error( 'unknown', '', $code ); |
||
| 210 | |||
| 211 | $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; |
||
| 212 | |||
| 213 | return new Jetpack_Error( (string) $json->error, $error_description, $code ); |
||
| 214 | } |
||
| 215 | |||
| 216 | if ( empty( $json->access_token ) || !is_scalar( $json->access_token ) ) { |
||
| 217 | return new Jetpack_Error( 'access_token', '', $code ); |
||
| 218 | } |
||
| 219 | |||
| 220 | if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) { |
||
| 221 | return new Jetpack_Error( 'token_type', '', $code ); |
||
| 222 | } |
||
| 223 | |||
| 224 | if ( empty( $json->scope ) ) { |
||
| 225 | return new Jetpack_Error( 'scope', 'No Scope', $code ); |
||
| 226 | } |
||
| 227 | @list( $role, $hmac ) = explode( ':', $json->scope ); |
||
| 228 | if ( empty( $role ) || empty( $hmac ) ) { |
||
| 229 | return new Jetpack_Error( 'scope', 'Malformed Scope', $code ); |
||
| 230 | } |
||
| 231 | if ( $jetpack->sign_role( $role ) !== $json->scope ) { |
||
| 232 | return new Jetpack_Error( 'scope', 'Invalid Scope', $code ); |
||
| 233 | } |
||
| 234 | |||
| 235 | if ( !$cap = $jetpack->translate_role_to_cap( $role ) ) |
||
| 236 | return new Jetpack_Error( 'scope', 'No Cap', $code ); |
||
| 237 | if ( ! current_user_can( $cap ) ) |
||
| 238 | return new Jetpack_Error( 'scope', 'current_user_cannot', $code ); |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Fires after user has successfully received an auth token. |
||
| 242 | * |
||
| 243 | * @since 3.9.0 |
||
| 244 | */ |
||
| 245 | do_action( 'jetpack_user_authorized' ); |
||
| 246 | |||
| 247 | return (string) $json->access_token; |
||
| 248 | } |
||
| 249 | |||
| 267 |
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: