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