| Conditions | 16 |
| Paths | 52 |
| Total Lines | 102 |
| Code Lines | 63 |
| 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 |
||
| 8 | function authorize() { |
||
| 9 | $data = stripslashes_deep( $_GET ); |
||
| 10 | $args = array(); |
||
|
|
|||
| 11 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
| 12 | |||
| 13 | do { |
||
| 14 | $jetpack = Jetpack::init(); |
||
| 15 | $role = $jetpack->translate_current_user_to_role(); |
||
| 16 | if ( !$role ) { |
||
| 17 | Jetpack::state( 'error', 'no_role' ); |
||
| 18 | break; |
||
| 19 | } |
||
| 20 | |||
| 21 | $cap = $jetpack->translate_role_to_cap( $role ); |
||
| 22 | if ( !$cap ) { |
||
| 23 | Jetpack::state( 'error', 'no_cap' ); |
||
| 24 | break; |
||
| 25 | } |
||
| 26 | |||
| 27 | check_admin_referer( "jetpack-authorize_{$role}_{$redirect}" ); |
||
| 28 | |||
| 29 | if ( !empty( $data['error'] ) ) { |
||
| 30 | Jetpack::state( 'error', $data['error'] ); |
||
| 31 | break; |
||
| 32 | } |
||
| 33 | |||
| 34 | if ( empty( $data['state'] ) ) { |
||
| 35 | Jetpack::state( 'error', 'no_state' ); |
||
| 36 | break; |
||
| 37 | } |
||
| 38 | |||
| 39 | if ( !ctype_digit( $data['state'] ) ) { |
||
| 40 | Jetpack::state( 'error', 'invalid_state' ); |
||
| 41 | break; |
||
| 42 | } |
||
| 43 | |||
| 44 | $current_user_id = get_current_user_id(); |
||
| 45 | if ( $current_user_id != $data['state'] ) { |
||
| 46 | Jetpack::state( 'error', 'wrong_state' ); |
||
| 47 | break; |
||
| 48 | } |
||
| 49 | |||
| 50 | if ( empty( $data['code'] ) ) { |
||
| 51 | Jetpack::state( 'error', 'no_code' ); |
||
| 52 | break; |
||
| 53 | } |
||
| 54 | |||
| 55 | $token = $this->get_token( $data ); |
||
| 56 | |||
| 57 | if ( is_wp_error( $token ) ) { |
||
| 58 | if ( $error = $token->get_error_code() ) |
||
| 59 | Jetpack::state( 'error', $error ); |
||
| 60 | else |
||
| 61 | Jetpack::state( 'error', 'invalid_token' ); |
||
| 62 | |||
| 63 | Jetpack::state( 'error_description', $token->get_error_message() ); |
||
| 64 | |||
| 65 | break; |
||
| 66 | } |
||
| 67 | |||
| 68 | if ( !$token ) { |
||
| 69 | Jetpack::state( 'error', 'no_token' ); |
||
| 70 | break; |
||
| 71 | } |
||
| 72 | |||
| 73 | $is_master_user = ! Jetpack::is_active(); |
||
| 74 | |||
| 75 | Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_master_user ); |
||
| 76 | |||
| 77 | |||
| 78 | if ( $is_master_user ) { |
||
| 79 | Jetpack::state( 'message', 'authorized' ); |
||
| 80 | } else { |
||
| 81 | Jetpack::state( 'message', 'linked' ); |
||
| 82 | // Don't activate anything since we are just connecting a user. |
||
| 83 | break; |
||
| 84 | } |
||
| 85 | |||
| 86 | if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { |
||
| 87 | Jetpack_Options::delete_option( 'active_modules' ); |
||
| 88 | |||
| 89 | Jetpack::activate_default_modules( 999, 1, $active_modules ); |
||
| 90 | } else { |
||
| 91 | Jetpack::activate_default_modules(); |
||
| 92 | } |
||
| 93 | |||
| 94 | // Sync all registers options and constants |
||
| 95 | do_action( 'jetpack_sync_all_registered_options' ); |
||
| 96 | |||
| 97 | // Start nonce cleaner |
||
| 98 | wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
||
| 99 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
| 100 | } while ( false ); |
||
| 101 | |||
| 102 | if ( wp_validate_redirect( $redirect ) ) { |
||
| 103 | wp_safe_redirect( $redirect ); |
||
| 104 | } else { |
||
| 105 | wp_safe_redirect( Jetpack::admin_url() ); |
||
| 106 | } |
||
| 107 | |||
| 108 | exit; |
||
| 109 | } |
||
| 110 | |||
| 218 |
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.