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