| Conditions | 30 |
| Paths | > 20000 |
| Total Lines | 124 |
| Lines | 26 |
| Ratio | 20.97 % |
| 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 |
||
| 17 | public static function register_and_build_request_body( $named_args ) { |
||
| 18 | $url_args = array( |
||
| 19 | 'home_url' => 'WP_HOME', |
||
| 20 | 'site_url' => 'WP_SITEURL', |
||
| 21 | ); |
||
| 22 | |||
| 23 | foreach ( $url_args as $url_arg => $constant_name ) { |
||
| 24 | if ( isset( $named_args[ $url_arg ] ) ) { |
||
| 25 | add_filter( |
||
| 26 | $url_arg, |
||
| 27 | function() use ( $url_arg, $named_args ) { |
||
| 28 | return $named_args[ $url_arg ]; |
||
| 29 | }, |
||
| 30 | 11 |
||
| 31 | ); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | // If Jetpack is currently connected, and is not in Safe Mode already, kick off a sync of the current |
||
| 36 | // functions/callables so that we can test if this site is in IDC. |
||
| 37 | if ( Jetpack::is_active() && ! Jetpack::validate_sync_error_idc_option() && Actions::sync_allowed() ) { |
||
| 38 | Actions::do_full_sync( array( 'functions' => true ) ); |
||
| 39 | Actions::$sender->do_full_sync(); |
||
| 40 | } |
||
| 41 | |||
| 42 | if ( Jetpack::validate_sync_error_idc_option() ) { |
||
| 43 | return new WP_Error( |
||
| 44 | 'site_in_safe_mode', |
||
|
|
|||
| 45 | __( 'Can not provision a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' ) |
||
| 46 | ); |
||
| 47 | } |
||
| 48 | |||
| 49 | |||
| 50 | if ( ! Jetpack::connection()->is_registered() || ( isset( $named_args['force_register'] ) && intval( $named_args['force_register'] ) ) ) { |
||
| 51 | // This code mostly copied from Jetpack::admin_page_load. |
||
| 52 | Jetpack::maybe_set_version_option(); |
||
| 53 | $registered = Jetpack::try_registration(); |
||
| 54 | if ( is_wp_error( $registered ) ) { |
||
| 55 | return $registered; |
||
| 56 | } elseif ( ! $registered ) { |
||
| 57 | return new WP_Error( 'registration_error', __( 'There was an unspecified error registering the site', 'jetpack' ) ); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | // If the user isn't specified, but we have a current master user, then set that to current user. |
||
| 62 | $master_user_id = Jetpack_Options::get_option( 'master_user' ); |
||
| 63 | if ( ! get_current_user_id() && $master_user_id ) { |
||
| 64 | wp_set_current_user( $master_user_id ); |
||
| 65 | } |
||
| 66 | |||
| 67 | $site_icon = get_site_icon_url(); |
||
| 68 | |||
| 69 | $auto_enable_sso = ( ! Jetpack::is_active() || Jetpack::is_module_active( 'sso' ) ); |
||
| 70 | |||
| 71 | /** This filter is documented in class.jetpack-cli.php */ |
||
| 72 | View Code Duplication | if ( apply_filters( 'jetpack_start_enable_sso', $auto_enable_sso ) ) { |
|
| 73 | $redirect_uri = add_query_arg( |
||
| 74 | array( |
||
| 75 | 'action' => 'jetpack-sso', |
||
| 76 | 'redirect_to' => rawurlencode( admin_url() ), |
||
| 77 | ), |
||
| 78 | wp_login_url() // TODO: come back to Jetpack dashboard? |
||
| 79 | ); |
||
| 80 | } else { |
||
| 81 | $redirect_uri = admin_url(); |
||
| 82 | } |
||
| 83 | |||
| 84 | $request_body = array( |
||
| 85 | 'jp_version' => JETPACK__VERSION, |
||
| 86 | 'redirect_uri' => $redirect_uri, |
||
| 87 | ); |
||
| 88 | |||
| 89 | if ( $site_icon ) { |
||
| 90 | $request_body['site_icon'] = $site_icon; |
||
| 91 | } |
||
| 92 | |||
| 93 | if ( get_current_user_id() ) { |
||
| 94 | $user = wp_get_current_user(); |
||
| 95 | |||
| 96 | // Role. |
||
| 97 | $roles = new Roles(); |
||
| 98 | $role = $roles->translate_current_user_to_role(); |
||
| 99 | $signed_role = Jetpack::connection()->sign_role( $role ); |
||
| 100 | |||
| 101 | $secrets = Jetpack::init()->generate_secrets( 'authorize' ); |
||
| 102 | |||
| 103 | // Jetpack auth stuff. |
||
| 104 | $request_body['scope'] = $signed_role; |
||
| 105 | $request_body['secret'] = $secrets['secret_1']; |
||
| 106 | |||
| 107 | // User stuff. |
||
| 108 | $request_body['user_id'] = $user->ID; |
||
| 109 | $request_body['user_email'] = $user->user_email; |
||
| 110 | $request_body['user_login'] = $user->user_login; |
||
| 111 | } |
||
| 112 | |||
| 113 | // Optional additional params. |
||
| 114 | View Code Duplication | if ( isset( $named_args['wpcom_user_id'] ) && ! empty( $named_args['wpcom_user_id'] ) ) { |
|
| 115 | $request_body['wpcom_user_id'] = $named_args['wpcom_user_id']; |
||
| 116 | } |
||
| 117 | |||
| 118 | // Override email of selected user. |
||
| 119 | View Code Duplication | if ( isset( $named_args['wpcom_user_email'] ) && ! empty( $named_args['wpcom_user_email'] ) ) { |
|
| 120 | $request_body['user_email'] = $named_args['wpcom_user_email']; |
||
| 121 | } |
||
| 122 | |||
| 123 | View Code Duplication | if ( isset( $named_args['plan'] ) && ! empty( $named_args['plan'] ) ) { |
|
| 124 | $request_body['plan'] = $named_args['plan']; |
||
| 125 | } |
||
| 126 | |||
| 127 | View Code Duplication | if ( isset( $named_args['onboarding'] ) && ! empty( $named_args['onboarding'] ) ) { |
|
| 128 | $request_body['onboarding'] = intval( $named_args['onboarding'] ); |
||
| 129 | } |
||
| 130 | |||
| 131 | View Code Duplication | if ( isset( $named_args['force_connect'] ) && ! empty( $named_args['force_connect'] ) ) { |
|
| 132 | $request_body['force_connect'] = intval( $named_args['force_connect'] ); |
||
| 133 | } |
||
| 134 | |||
| 135 | if ( isset( $request_body['onboarding'] ) && (bool) $request_body['onboarding'] ) { |
||
| 136 | Jetpack::create_onboarding_token(); |
||
| 137 | } |
||
| 138 | |||
| 139 | return $request_body; |
||
| 140 | } |
||
| 141 | |||
| 281 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.