| Conditions | 22 |
| Paths | 94 |
| Total Lines | 99 |
| Code Lines | 57 |
| 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 |
||
| 175 | function get_token( $data ) { |
||
| 176 | $role = Jetpack::translate_current_user_to_role(); |
||
| 177 | |||
| 178 | if ( ! $role ) { |
||
| 179 | return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) ); |
||
| 180 | } |
||
| 181 | |||
| 182 | $client_secret = Jetpack_Data::get_access_token(); |
||
| 183 | if ( ! $client_secret ) { |
||
| 184 | return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) ); |
||
| 185 | } |
||
| 186 | |||
| 187 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
| 188 | $redirect_uri = ( 'calypso' === $data['auth_type'] ) |
||
| 189 | ? $data['redirect_uri'] |
||
| 190 | : add_query_arg( array( |
||
| 191 | 'action' => 'authorize', |
||
| 192 | '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
||
| 193 | 'redirect' => $redirect ? urlencode( $redirect ) : false, |
||
| 194 | ), menu_page_url( 'jetpack', false ) ); |
||
| 195 | |||
| 196 | $body = array( |
||
| 197 | 'client_id' => Jetpack_Options::get_option( 'id' ), |
||
| 198 | 'client_secret' => $client_secret->secret, |
||
| 199 | 'grant_type' => 'authorization_code', |
||
| 200 | 'code' => $data['code'], |
||
| 201 | 'redirect_uri' => $redirect_uri, |
||
| 202 | ); |
||
| 203 | |||
| 204 | $args = array( |
||
| 205 | 'method' => 'POST', |
||
| 206 | 'body' => $body, |
||
| 207 | 'headers' => array( |
||
| 208 | 'Accept' => 'application/json', |
||
| 209 | ), |
||
| 210 | ); |
||
| 211 | $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'token' ) ), $args ); |
||
| 212 | |||
| 213 | if ( is_wp_error( $response ) ) { |
||
| 214 | return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() ); |
||
| 215 | } |
||
| 216 | |||
| 217 | $code = wp_remote_retrieve_response_code( $response ); |
||
| 218 | $entity = wp_remote_retrieve_body( $response ); |
||
| 219 | |||
| 220 | if ( $entity ) { |
||
| 221 | $json = json_decode( $entity ); |
||
| 222 | } else { |
||
| 223 | $json = false; |
||
| 224 | } |
||
| 225 | |||
| 226 | if ( 200 != $code || ! empty( $json->error ) ) { |
||
| 227 | if ( empty( $json->error ) ) { |
||
| 228 | return new Jetpack_Error( 'unknown', '', $code ); |
||
| 229 | } |
||
| 230 | |||
| 231 | $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; |
||
| 232 | |||
| 233 | return new Jetpack_Error( (string) $json->error, $error_description, $code ); |
||
| 234 | } |
||
| 235 | |||
| 236 | if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) { |
||
| 237 | return new Jetpack_Error( 'access_token', '', $code ); |
||
| 238 | } |
||
| 239 | |||
| 240 | if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) { |
||
| 241 | return new Jetpack_Error( 'token_type', '', $code ); |
||
| 242 | } |
||
| 243 | |||
| 244 | if ( empty( $json->scope ) ) { |
||
| 245 | return new Jetpack_Error( 'scope', 'No Scope', $code ); |
||
| 246 | } |
||
| 247 | |||
| 248 | @list( $role, $hmac ) = explode( ':', $json->scope ); |
||
| 249 | if ( empty( $role ) || empty( $hmac ) ) { |
||
| 250 | return new Jetpack_Error( 'scope', 'Malformed Scope', $code ); |
||
| 251 | } |
||
| 252 | |||
| 253 | if ( Jetpack::sign_role( $role ) !== $json->scope ) { |
||
| 254 | return new Jetpack_Error( 'scope', 'Invalid Scope', $code ); |
||
| 255 | } |
||
| 256 | |||
| 257 | if ( ! $cap = Jetpack::translate_role_to_cap( $role ) ) { |
||
| 258 | return new Jetpack_Error( 'scope', 'No Cap', $code ); |
||
| 259 | } |
||
| 260 | |||
| 261 | if ( ! current_user_can( $cap ) ) { |
||
| 262 | return new Jetpack_Error( 'scope', 'current_user_cannot', $code ); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Fires after user has successfully received an auth token. |
||
| 267 | * |
||
| 268 | * @since 3.9.0 |
||
| 269 | */ |
||
| 270 | do_action( 'jetpack_user_authorized' ); |
||
| 271 | |||
| 272 | return (string) $json->access_token; |
||
| 273 | } |
||
| 274 | |||
| 283 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.