| Conditions | 41 |
| Paths | > 20000 |
| Total Lines | 169 |
| Code Lines | 92 |
| Lines | 25 |
| Ratio | 14.79 % |
| 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 |
||
| 3 | static function partner_provision( $access_token, $named_args ) { |
||
| 4 | // first, verify the token |
||
| 5 | $verify_response = self::verify_token( $access_token ); |
||
| 6 | |||
| 7 | if ( is_wp_error( $verify_response ) ) { |
||
| 8 | return $verify_response; |
||
| 9 | } |
||
| 10 | |||
| 11 | $url_args = array( |
||
| 12 | 'home_url' => 'WP_HOME', |
||
| 13 | 'site_url' => 'WP_SITEURL', |
||
| 14 | ); |
||
| 15 | |||
| 16 | foreach ( $url_args as $url_arg => $constant_name ) { |
||
| 17 | // Anonymous functions were introduced in 5.3.0. So, if we're running on |
||
| 18 | // >= 5.3.0, use an anonymous function to set the home/siteurl value%s. |
||
| 19 | // |
||
| 20 | // Otherwise, fallback to setting the home/siteurl value via the WP_HOME and |
||
| 21 | // WP_SITEURL constants if the constant hasn't already been defined. |
||
| 22 | if ( isset( $named_args[ $url_arg ] ) ) { |
||
| 23 | if ( version_compare( phpversion(), '5.3.0', '>=') ) { |
||
| 24 | add_filter( $url_arg, function( $url ) use ( $url_arg, $named_args ) { |
||
| 25 | return $named_args[ $url_arg ]; |
||
| 26 | }, 11 ); |
||
| 27 | } else if ( ! defined( $constant_name ) ) { |
||
| 28 | define( $constant_name, $named_args[ $url_arg ] ); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 34 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 35 | |||
| 36 | if ( ! $blog_id || ! $blog_token || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) { |
||
| 37 | // this code mostly copied from Jetpack::admin_page_load |
||
| 38 | Jetpack::maybe_set_version_option(); |
||
| 39 | $registered = Jetpack::try_registration(); |
||
| 40 | if ( is_wp_error( $registered ) ) { |
||
| 41 | return $registered; |
||
| 42 | } elseif ( ! $registered ) { |
||
| 43 | return new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) ); |
||
| 44 | } |
||
| 45 | |||
| 46 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 47 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
|
|
|||
| 48 | } |
||
| 49 | |||
| 50 | // if the user isn't specified, but we have a current master user, then set that to current user |
||
| 51 | if ( ! get_current_user_id() && $master_user_id = Jetpack_Options::get_option( 'master_user' ) ) { |
||
| 52 | wp_set_current_user( $master_user_id ); |
||
| 53 | } |
||
| 54 | |||
| 55 | $site_icon = ( function_exists( 'has_site_icon') && has_site_icon() ) |
||
| 56 | ? get_site_icon_url() |
||
| 57 | : false; |
||
| 58 | |||
| 59 | $auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) ); |
||
| 60 | |||
| 61 | /** This filter is documented in class.jetpack-cli.php */ |
||
| 62 | if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) { |
||
| 63 | $redirect_uri = add_query_arg( |
||
| 64 | array( 'action' => 'jetpack-sso', 'redirect_to' => urlencode( admin_url() ) ), |
||
| 65 | wp_login_url() // TODO: come back to Jetpack dashboard? |
||
| 66 | ); |
||
| 67 | } else { |
||
| 68 | $redirect_uri = admin_url(); |
||
| 69 | } |
||
| 70 | |||
| 71 | $request_body = array( |
||
| 72 | 'jp_version' => JETPACK__VERSION, |
||
| 73 | 'redirect_uri' => $redirect_uri |
||
| 74 | ); |
||
| 75 | |||
| 76 | if ( $site_icon ) { |
||
| 77 | $request_body['site_icon'] = $site_icon; |
||
| 78 | } |
||
| 79 | |||
| 80 | if ( get_current_user_id() ) { |
||
| 81 | $user = wp_get_current_user(); |
||
| 82 | |||
| 83 | // role |
||
| 84 | $role = Jetpack::translate_current_user_to_role(); |
||
| 85 | $signed_role = Jetpack::sign_role( $role ); |
||
| 86 | |||
| 87 | $secrets = Jetpack::init()->generate_secrets( 'authorize' ); |
||
| 88 | |||
| 89 | // Jetpack auth stuff |
||
| 90 | $request_body['scope'] = $signed_role; |
||
| 91 | $request_body['secret'] = $secrets['secret_1']; |
||
| 92 | |||
| 93 | // User stuff |
||
| 94 | $request_body['user_id'] = $user->ID; |
||
| 95 | $request_body['user_email'] = $user->user_email; |
||
| 96 | $request_body['user_login'] = $user->user_login; |
||
| 97 | } |
||
| 98 | |||
| 99 | // optional additional params |
||
| 100 | View Code Duplication | if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) { |
|
| 101 | $request_body['wpcom_user_id'] = $named_args['wpcom_user_id']; |
||
| 102 | } |
||
| 103 | |||
| 104 | // override email of selected user |
||
| 105 | View Code Duplication | if ( isset( $named_args['wpcom_user_email'] ) && ! empty( $named_args['wpcom_user_email'] ) ) { |
|
| 106 | $request_body['user_email'] = $named_args['wpcom_user_email']; |
||
| 107 | } |
||
| 108 | |||
| 109 | View Code Duplication | if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) { |
|
| 110 | $request_body['plan'] = $named_args['plan']; |
||
| 111 | } |
||
| 112 | |||
| 113 | View Code Duplication | if ( isset( $named_args['onboarding'] ) && ! empty( $named_args['onboarding'] ) ) { |
|
| 114 | $request_body['onboarding'] = intval( $named_args['onboarding'] ); |
||
| 115 | } |
||
| 116 | |||
| 117 | View Code Duplication | if ( isset( $named_args['force_connect'] ) && ! empty( $named_args['force_connect'] ) ) { |
|
| 118 | $request_body['force_connect'] = intval( $named_args['force_connect'] ); |
||
| 119 | } |
||
| 120 | |||
| 121 | if ( isset( $request_body['onboarding'] ) && (bool) $request_body['onboarding'] ) { |
||
| 122 | Jetpack::create_onboarding_token(); |
||
| 123 | } |
||
| 124 | |||
| 125 | $request = array( |
||
| 126 | 'headers' => array( |
||
| 127 | 'Authorization' => "Bearer " . $access_token, |
||
| 128 | 'Host' => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com', |
||
| 129 | ), |
||
| 130 | 'timeout' => 60, |
||
| 131 | 'method' => 'POST', |
||
| 132 | 'body' => json_encode( $request_body ) |
||
| 133 | ); |
||
| 134 | |||
| 135 | $url = sprintf( 'https://%s/rest/v1.3/jpphp/%d/partner-provision', self::get_api_host(), $blog_id ); |
||
| 136 | View Code Duplication | if ( ! empty( $named_args['partner-tracking-id'] ) ) { |
|
| 137 | $url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner-tracking-id'], $url ) ); |
||
| 138 | } |
||
| 139 | |||
| 140 | // add calypso env if set |
||
| 141 | if ( getenv( 'CALYPSO_ENV' ) ) { |
||
| 142 | $url = add_query_arg( array( 'calypso_env' => getenv( 'CALYPSO_ENV' ) ), $url ); |
||
| 143 | } |
||
| 144 | |||
| 145 | $result = Jetpack_Client::_wp_remote_request( $url, $request ); |
||
| 146 | |||
| 147 | if ( is_wp_error( $result ) ) { |
||
| 148 | return $result; |
||
| 149 | } |
||
| 150 | |||
| 151 | $response_code = wp_remote_retrieve_response_code( $result ); |
||
| 152 | $body_json = json_decode( wp_remote_retrieve_body( $result ) ); |
||
| 153 | |||
| 154 | View Code Duplication | if( 200 !== $response_code ) { |
|
| 155 | if ( isset( $body_json->error ) ) { |
||
| 156 | return new WP_Error( $body_json->error, $body_json->message ); |
||
| 157 | } else { |
||
| 158 | return new WP_Error( 'server_error', sprintf( __( "Request failed with code %s" ), $response_code ) ); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | if ( isset( $body_json->access_token ) ) { |
||
| 163 | // check if this matches the existing token before replacing |
||
| 164 | $existing_token = Jetpack_Data::get_access_token( $user->ID ); |
||
| 165 | if ( empty( $existing_token ) || $existing_token->secret !== $body_json->access_token ) { |
||
| 166 | self::authorize_user( $user->ID, $body_json->access_token ); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | return $body_json; |
||
| 171 | } |
||
| 172 | |||
| 233 |
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.