| Conditions | 22 |
| Paths | 94 |
| Total Lines | 107 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 186 | function get_token( $data ) { |
||
| 187 | $roles = new Roles(); |
||
| 188 | $role = $roles->translate_current_user_to_role(); |
||
| 189 | |||
| 190 | if ( ! $role ) { |
||
| 191 | return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) ); |
||
| 192 | } |
||
| 193 | |||
| 194 | $client_secret = Jetpack_Data::get_access_token(); |
||
| 195 | if ( ! $client_secret ) { |
||
| 196 | return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) ); |
||
| 197 | } |
||
| 198 | |||
| 199 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
| 200 | $redirect_uri = ( 'calypso' === $data['auth_type'] ) |
||
| 201 | ? $data['redirect_uri'] |
||
| 202 | : add_query_arg( array( |
||
| 203 | 'action' => 'authorize', |
||
| 204 | '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
||
| 205 | 'redirect' => $redirect ? urlencode( $redirect ) : false, |
||
| 206 | ), menu_page_url( 'jetpack', false ) ); |
||
| 207 | |||
| 208 | // inject identity for analytics |
||
| 209 | $tracks = new Automattic\Jetpack\Tracking(); |
||
| 210 | $tracks_identity = $tracks->tracks_get_identity( get_current_user_id() ); |
||
| 211 | |||
| 212 | $body = array( |
||
| 213 | 'client_id' => Jetpack_Options::get_option( 'id' ), |
||
| 214 | 'client_secret' => $client_secret->secret, |
||
| 215 | 'grant_type' => 'authorization_code', |
||
| 216 | 'code' => $data['code'], |
||
| 217 | 'redirect_uri' => $redirect_uri, |
||
| 218 | '_ui' => $tracks_identity['_ui'], |
||
| 219 | '_ut' => $tracks_identity['_ut'], |
||
| 220 | ); |
||
| 221 | |||
| 222 | $args = array( |
||
| 223 | 'method' => 'POST', |
||
| 224 | 'body' => $body, |
||
| 225 | 'headers' => array( |
||
| 226 | 'Accept' => 'application/json', |
||
| 227 | ), |
||
| 228 | ); |
||
| 229 | $response = Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'token' ) ), $args ); |
||
| 230 | |||
| 231 | if ( is_wp_error( $response ) ) { |
||
| 232 | return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() ); |
||
| 233 | } |
||
| 234 | |||
| 235 | $code = wp_remote_retrieve_response_code( $response ); |
||
| 236 | $entity = wp_remote_retrieve_body( $response ); |
||
| 237 | |||
| 238 | if ( $entity ) { |
||
| 239 | $json = json_decode( $entity ); |
||
| 240 | } else { |
||
| 241 | $json = false; |
||
| 242 | } |
||
| 243 | |||
| 244 | if ( 200 != $code || ! empty( $json->error ) ) { |
||
| 245 | if ( empty( $json->error ) ) { |
||
| 246 | return new Jetpack_Error( 'unknown', '', $code ); |
||
| 247 | } |
||
| 248 | |||
| 249 | $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; |
||
| 250 | |||
| 251 | return new Jetpack_Error( (string) $json->error, $error_description, $code ); |
||
| 252 | } |
||
| 253 | |||
| 254 | if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) { |
||
| 255 | return new Jetpack_Error( 'access_token', '', $code ); |
||
| 256 | } |
||
| 257 | |||
| 258 | if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) { |
||
| 259 | return new Jetpack_Error( 'token_type', '', $code ); |
||
| 260 | } |
||
| 261 | |||
| 262 | if ( empty( $json->scope ) ) { |
||
| 263 | return new Jetpack_Error( 'scope', 'No Scope', $code ); |
||
| 264 | } |
||
| 265 | |||
| 266 | @list( $role, $hmac ) = explode( ':', $json->scope ); |
||
| 267 | if ( empty( $role ) || empty( $hmac ) ) { |
||
| 268 | return new Jetpack_Error( 'scope', 'Malformed Scope', $code ); |
||
| 269 | } |
||
| 270 | |||
| 271 | if ( Jetpack::sign_role( $role ) !== $json->scope ) { |
||
| 272 | return new Jetpack_Error( 'scope', 'Invalid Scope', $code ); |
||
| 273 | } |
||
| 274 | |||
| 275 | $cap = $roles->translate_role_to_cap( $role ); |
||
| 276 | if ( ! $cap ) { |
||
| 277 | return new Jetpack_Error( 'scope', 'No Cap', $code ); |
||
| 278 | } |
||
| 279 | |||
| 280 | if ( ! current_user_can( $cap ) ) { |
||
| 281 | return new Jetpack_Error( 'scope', 'current_user_cannot', $code ); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Fires after user has successfully received an auth token. |
||
| 286 | * |
||
| 287 | * @since 3.9.0 |
||
| 288 | */ |
||
| 289 | do_action( 'jetpack_user_authorized' ); |
||
| 290 | |||
| 291 | return (string) $json->access_token; |
||
| 292 | } |
||
| 293 | |||
| 302 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.