| Conditions | 16 |
| Paths | 56 |
| Total Lines | 103 |
| 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 |
||
| 53 | function authorize( $data = array() ) { |
||
| 54 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
|
|
|||
| 55 | |||
| 56 | $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ); |
||
| 57 | // Checking if site has been active/connected previously before recording unique connection |
||
| 58 | if ( ! $jetpack_unique_connection ) { |
||
| 59 | // jetpack_unique_connection option has never been set |
||
| 60 | $jetpack_unique_connection = array( |
||
| 61 | 'connected' => 0, |
||
| 62 | 'disconnected' => 0, |
||
| 63 | 'version' => '3.6.1', |
||
| 64 | ); |
||
| 65 | |||
| 66 | update_option( 'jetpack_unique_connection', $jetpack_unique_connection ); |
||
| 67 | |||
| 68 | //track unique connection |
||
| 69 | $jetpack = $this->get_jetpack(); |
||
| 70 | |||
| 71 | $jetpack->stat( 'connections', 'unique-connection' ); |
||
| 72 | $jetpack->do_stats( 'server_side' ); |
||
| 73 | } |
||
| 74 | |||
| 75 | // increment number of times connected |
||
| 76 | $jetpack_unique_connection['connected'] += 1; |
||
| 77 | Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); |
||
| 78 | |||
| 79 | $role = Jetpack::translate_current_user_to_role(); |
||
| 80 | |||
| 81 | if ( ! $role ) { |
||
| 82 | return new Jetpack_Error( 'no_role', 'Invalid request.', 400 ); |
||
| 83 | } |
||
| 84 | |||
| 85 | $cap = Jetpack::translate_role_to_cap( $role ); |
||
| 86 | if ( ! $cap ) { |
||
| 87 | return new Jetpack_Error( 'no_cap', 'Invalid request.', 400 ); |
||
| 88 | } |
||
| 89 | |||
| 90 | if ( ! empty( $data['error'] ) ) { |
||
| 91 | return new Jetpack_Error( $data['error'], 'Error included in the request.', 400 ); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ( ! isset( $data['state'] ) ) { |
||
| 95 | return new Jetpack_Error( 'no_state', 'Request must include state.', 400 ); |
||
| 96 | } |
||
| 97 | |||
| 98 | if ( ! ctype_digit( $data['state'] ) ) { |
||
| 99 | return new Jetpack_Error( $data['error'], 'State must be an integer.', 400 ); |
||
| 100 | } |
||
| 101 | |||
| 102 | $current_user_id = get_current_user_id(); |
||
| 103 | if ( $current_user_id != $data['state'] ) { |
||
| 104 | return new Jetpack_Error( 'wrong_state', 'State does not match current user.', 400 ); |
||
| 105 | } |
||
| 106 | |||
| 107 | if ( empty( $data['code'] ) ) { |
||
| 108 | return new Jetpack_Error( 'no_code', 'Request must include an authorization code.', 400 ); |
||
| 109 | } |
||
| 110 | |||
| 111 | $token = $this->get_token( $data ); |
||
| 112 | |||
| 113 | if ( is_wp_error( $token ) ) { |
||
| 114 | $code = $token->get_error_code(); |
||
| 115 | if ( empty( $code ) ) { |
||
| 116 | $code = 'invalid_token'; |
||
| 117 | } |
||
| 118 | return new Jetpack_Error( $code, $token->get_error_message(), 400 ); |
||
| 119 | } |
||
| 120 | |||
| 121 | if ( ! $token ) { |
||
| 122 | return new Jetpack_Error( 'no_token', 'Error generating token.', 400 ); |
||
| 123 | } |
||
| 124 | |||
| 125 | $is_master_user = ! Jetpack::is_active(); |
||
| 126 | |||
| 127 | Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_master_user ); |
||
| 128 | |||
| 129 | if ( ! $is_master_user ) { |
||
| 130 | Jetpack::state( 'message', 'linked' ); |
||
| 131 | // Don't activate anything since we are just connecting a user. |
||
| 132 | return 'linked'; |
||
| 133 | } |
||
| 134 | |||
| 135 | // If this site has been through the Jetpack Onboarding flow, delete the onboarding token |
||
| 136 | Jetpack::invalidate_onboarding_token(); |
||
| 137 | |||
| 138 | // If redirect_uri is SSO, ensure SSO module is enabled |
||
| 139 | parse_str( parse_url( $data['redirect_uri'], PHP_URL_QUERY ), $redirect_options ); |
||
| 140 | |||
| 141 | /** This filter is documented in class.jetpack-cli.php */ |
||
| 142 | $jetpack_start_enable_sso = apply_filters( 'jetpack_start_enable_sso', true ); |
||
| 143 | |||
| 144 | $activate_sso = ( |
||
| 145 | isset( $redirect_options['action'] ) && |
||
| 146 | 'jetpack-sso' === $redirect_options['action'] && |
||
| 147 | $jetpack_start_enable_sso |
||
| 148 | ); |
||
| 149 | |||
| 150 | $do_redirect_on_error = ( 'client' === $data['auth_type'] ); |
||
| 151 | |||
| 152 | Jetpack::handle_post_authorization_actions( $activate_sso, $do_redirect_on_error ); |
||
| 153 | |||
| 154 | return 'authorized'; |
||
| 155 | } |
||
| 156 | |||
| 293 |
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.