| 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 |
||
| 177 | function get_token( $data ) { |
||
| 178 | $role = Jetpack::translate_current_user_to_role(); |
||
| 179 | |||
| 180 | if ( ! $role ) { |
||
| 181 | return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) ); |
||
| 182 | } |
||
| 183 | |||
| 184 | $client_secret = Jetpack_Data::get_access_token(); |
||
| 185 | if ( ! $client_secret ) { |
||
| 186 | return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) ); |
||
| 187 | } |
||
| 188 | |||
| 189 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
| 190 | $redirect_uri = ( 'calypso' === $data['auth_type'] ) |
||
| 191 | ? $data['redirect_uri'] |
||
| 192 | : add_query_arg( array( |
||
| 193 | 'action' => 'authorize', |
||
| 194 | '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
||
| 195 | 'redirect' => $redirect ? urlencode( $redirect ) : false, |
||
| 196 | ), menu_page_url( 'jetpack', false ) ); |
||
| 197 | |||
| 198 | $body = array( |
||
| 199 | 'client_id' => Jetpack_Options::get_option( 'id' ), |
||
| 200 | 'client_secret' => $client_secret->secret, |
||
| 201 | 'grant_type' => 'authorization_code', |
||
| 202 | 'code' => $data['code'], |
||
| 203 | 'redirect_uri' => $redirect_uri, |
||
| 204 | ); |
||
| 205 | |||
| 206 | $args = array( |
||
| 207 | 'method' => 'POST', |
||
| 208 | 'body' => $body, |
||
| 209 | 'headers' => array( |
||
| 210 | 'Accept' => 'application/json', |
||
| 211 | ), |
||
| 212 | ); |
||
| 213 | $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'token' ) ), $args ); |
||
| 214 | |||
| 215 | if ( is_wp_error( $response ) ) { |
||
| 216 | return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() ); |
||
| 217 | } |
||
| 218 | |||
| 219 | $code = wp_remote_retrieve_response_code( $response ); |
||
| 220 | $entity = wp_remote_retrieve_body( $response ); |
||
| 221 | |||
| 222 | if ( $entity ) { |
||
| 223 | $json = json_decode( $entity ); |
||
| 224 | } else { |
||
| 225 | $json = false; |
||
| 226 | } |
||
| 227 | |||
| 228 | if ( 200 != $code || ! empty( $json->error ) ) { |
||
| 229 | if ( empty( $json->error ) ) { |
||
| 230 | return new Jetpack_Error( 'unknown', '', $code ); |
||
| 231 | } |
||
| 232 | |||
| 233 | $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; |
||
| 234 | |||
| 235 | return new Jetpack_Error( (string) $json->error, $error_description, $code ); |
||
| 236 | } |
||
| 237 | |||
| 238 | if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) { |
||
| 239 | return new Jetpack_Error( 'access_token', '', $code ); |
||
| 240 | } |
||
| 241 | |||
| 242 | if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) { |
||
| 243 | return new Jetpack_Error( 'token_type', '', $code ); |
||
| 244 | } |
||
| 245 | |||
| 246 | if ( empty( $json->scope ) ) { |
||
| 247 | return new Jetpack_Error( 'scope', 'No Scope', $code ); |
||
| 248 | } |
||
| 249 | |||
| 250 | @list( $role, $hmac ) = explode( ':', $json->scope ); |
||
| 251 | if ( empty( $role ) || empty( $hmac ) ) { |
||
| 252 | return new Jetpack_Error( 'scope', 'Malformed Scope', $code ); |
||
| 253 | } |
||
| 254 | |||
| 255 | if ( Jetpack::sign_role( $role ) !== $json->scope ) { |
||
| 256 | return new Jetpack_Error( 'scope', 'Invalid Scope', $code ); |
||
| 257 | } |
||
| 258 | |||
| 259 | if ( ! $cap = Jetpack::translate_role_to_cap( $role ) ) { |
||
| 260 | return new Jetpack_Error( 'scope', 'No Cap', $code ); |
||
| 261 | } |
||
| 262 | |||
| 263 | if ( ! current_user_can( $cap ) ) { |
||
| 264 | return new Jetpack_Error( 'scope', 'current_user_cannot', $code ); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Fires after user has successfully received an auth token. |
||
| 269 | * |
||
| 270 | * @since 3.9.0 |
||
| 271 | */ |
||
| 272 | do_action( 'jetpack_user_authorized' ); |
||
| 273 | |||
| 274 | return (string) $json->access_token; |
||
| 275 | } |
||
| 276 | |||
| 285 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.