| Conditions | 31 |
| Paths | > 20000 |
| Total Lines | 138 |
| Lines | 26 |
| Ratio | 18.84 % |
| 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 //phpcs:ignore |
||
| 13 | public static function register_and_build_request_body( $named_args ) { |
||
| 14 | $url_args = array( |
||
| 15 | 'home_url' => 'WP_HOME', |
||
| 16 | 'site_url' => 'WP_SITEURL', |
||
| 17 | ); |
||
| 18 | |||
| 19 | foreach ( $url_args as $url_arg => $constant_name ) { |
||
| 20 | // Anonymous functions were introduced in 5.3.0. So, if we're running on |
||
| 21 | // >= 5.3.0, use an anonymous function to set the home/siteurl value%s. |
||
| 22 | // |
||
| 23 | // Otherwise, fallback to setting the home/siteurl value via the WP_HOME and |
||
| 24 | // WP_SITEURL constants if the constant hasn't already been defined. |
||
| 25 | if ( isset( $named_args[ $url_arg ] ) ) { |
||
| 26 | if ( version_compare( phpversion(), '5.3.0', '>=' ) ) { |
||
| 27 | add_filter( $url_arg, function() use ( $url_arg, $named_args ) { // phpcs:ignore PHPCompatibility.PHP.NewClosure.Found |
||
| 28 | return $named_args[ $url_arg ]; |
||
| 29 | }, 11 ); |
||
| 30 | } elseif ( ! defined( $constant_name ) ) { |
||
| 31 | define( $constant_name, $named_args[ $url_arg ] ); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | /* |
||
| 37 | // If Jetpack is currently connected, and is not in Safe Mode already, kick off a sync of the current |
||
| 38 | // functions/callables so that we can test if this site is in IDC. |
||
| 39 | if ( Jetpack::is_active() && ! Jetpack::validate_sync_error_idc_option() && Jetpack_Sync_Actions::sync_allowed() ) { |
||
| 40 | Jetpack_Sync_Actions::do_full_sync( array( 'functions' => true ) ); |
||
| 41 | Jetpack_Sync_Actions::$sender->do_full_sync(); |
||
| 42 | } |
||
| 43 | |||
| 44 | |||
| 45 | if ( Jetpack::validate_sync_error_idc_option() ) { |
||
| 46 | return new WP_Error( |
||
| 47 | 'site_in_safe_mode', |
||
| 48 | __( 'Can not provision a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' ) |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | */ |
||
| 52 | |||
| 53 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 54 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 55 | |||
| 56 | if ( ! $blog_id || ! $blog_token || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) { |
||
| 57 | // This code mostly copied from Jetpack::admin_page_load. |
||
| 58 | Jetpack::maybe_set_version_option(); |
||
| 59 | $registered = Jetpack::try_registration(); |
||
| 60 | if ( is_wp_error( $registered ) ) { |
||
| 61 | return $registered; |
||
| 62 | } elseif ( ! $registered ) { |
||
| 63 | return new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) ); |
||
| 64 | } |
||
| 65 | |||
| 66 | $blog_id = Jetpack_Options::get_option( 'id' ); |
||
|
|
|||
| 67 | $blog_token = Jetpack_Options::get_option( 'blog_token' ); |
||
| 68 | } |
||
| 69 | |||
| 70 | // If the user isn't specified, but we have a current master user, then set that to current user. |
||
| 71 | $master_user_id = Jetpack_Options::get_option( 'master_user' ); |
||
| 72 | if ( ! get_current_user_id() && $master_user_id ) { |
||
| 73 | wp_set_current_user( $master_user_id ); |
||
| 74 | } |
||
| 75 | |||
| 76 | $site_icon = ( function_exists( 'has_site_icon' ) && has_site_icon() ) |
||
| 77 | ? get_site_icon_url() |
||
| 78 | : false; |
||
| 79 | |||
| 80 | $auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) ); |
||
| 81 | |||
| 82 | /** This filter is documented in class.jetpack-cli.php */ |
||
| 83 | View Code Duplication | if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) { |
|
| 84 | $redirect_uri = add_query_arg( |
||
| 85 | array( |
||
| 86 | 'action' => 'jetpack-sso', |
||
| 87 | 'redirect_to' => rawurlencode( admin_url() ), |
||
| 88 | ), |
||
| 89 | wp_login_url() // TODO: come back to Jetpack dashboard? |
||
| 90 | ); |
||
| 91 | } else { |
||
| 92 | $redirect_uri = admin_url(); |
||
| 93 | } |
||
| 94 | |||
| 95 | $request_body = array( |
||
| 96 | 'jp_version' => JETPACK__VERSION, |
||
| 97 | 'redirect_uri' => $redirect_uri, |
||
| 98 | ); |
||
| 99 | |||
| 100 | if ( $site_icon ) { |
||
| 101 | $request_body['site_icon'] = $site_icon; |
||
| 102 | } |
||
| 103 | |||
| 104 | if ( get_current_user_id() ) { |
||
| 105 | $user = wp_get_current_user(); |
||
| 106 | |||
| 107 | // Role. |
||
| 108 | $role = Jetpack::translate_current_user_to_role(); |
||
| 109 | $signed_role = Jetpack::sign_role( $role ); |
||
| 110 | |||
| 111 | $secrets = Jetpack::init()->generate_secrets( 'authorize' ); |
||
| 112 | |||
| 113 | // Jetpack auth stuff. |
||
| 114 | $request_body['scope'] = $signed_role; |
||
| 115 | $request_body['secret'] = $secrets['secret_1']; |
||
| 116 | |||
| 117 | // User stuff. |
||
| 118 | $request_body['user_id'] = $user->ID; |
||
| 119 | $request_body['user_email'] = $user->user_email; |
||
| 120 | $request_body['user_login'] = $user->user_login; |
||
| 121 | } |
||
| 122 | |||
| 123 | // Optional additional params. |
||
| 124 | View Code Duplication | if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) { |
|
| 125 | $request_body['wpcom_user_id'] = $named_args['wpcom_user_id']; |
||
| 126 | } |
||
| 127 | |||
| 128 | // Override email of selected user. |
||
| 129 | View Code Duplication | if ( isset( $named_args['wpcom_user_email'] ) && ! empty( $named_args['wpcom_user_email'] ) ) { |
|
| 130 | $request_body['user_email'] = $named_args['wpcom_user_email']; |
||
| 131 | } |
||
| 132 | |||
| 133 | View Code Duplication | if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) { |
|
| 134 | $request_body['plan'] = $named_args['plan']; |
||
| 135 | } |
||
| 136 | |||
| 137 | View Code Duplication | if ( isset( $named_args['onboarding'] ) && ! empty( $named_args['onboarding'] ) ) { |
|
| 138 | $request_body['onboarding'] = intval( $named_args['onboarding'] ); |
||
| 139 | } |
||
| 140 | |||
| 141 | View Code Duplication | if ( isset( $named_args['force_connect'] ) && ! empty( $named_args['force_connect'] ) ) { |
|
| 142 | $request_body['force_connect'] = intval( $named_args['force_connect'] ); |
||
| 143 | } |
||
| 144 | |||
| 145 | if ( isset( $request_body['onboarding'] ) && (bool) $request_body['onboarding'] ) { |
||
| 146 | Jetpack::create_onboarding_token(); |
||
| 147 | } |
||
| 148 | |||
| 149 | return $request_body; |
||
| 150 | } |
||
| 151 | |||
| 290 |
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.