Complex classes like Jetpack_Client_Server often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Client_Server, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Jetpack_Client_Server { |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Authorizations |
||
| 11 | */ |
||
| 12 | function client_authorize() { |
||
| 13 | $data = stripslashes_deep( $_GET ); |
||
| 14 | $data['auth_type'] = 'client'; |
||
| 15 | $role = Jetpack::translate_current_user_to_role(); |
||
| 16 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
| 17 | |||
| 18 | $this->check_admin_referer( "jetpack-authorize_{$role}_{$redirect}" ); |
||
| 19 | |||
| 20 | $result = $this->authorize( $data ); |
||
| 21 | if ( is_wp_error( $result ) ) { |
||
| 22 | Jetpack::state( 'error', $result->get_error_code() ); |
||
| 23 | } |
||
| 24 | |||
| 25 | if ( wp_validate_redirect( $redirect ) ) { |
||
| 26 | $this->wp_safe_redirect( $redirect ); |
||
| 27 | } else { |
||
| 28 | $this->wp_safe_redirect( Jetpack::admin_url() ); |
||
| 29 | } |
||
| 30 | |||
| 31 | $this->do_exit(); |
||
| 32 | } |
||
| 33 | |||
| 34 | function authorize( $data = array() ) { |
||
| 35 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
|
|
|||
| 36 | |||
| 37 | $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ); |
||
| 38 | // Checking if site has been active/connected previously before recording unique connection |
||
| 39 | if ( ! $jetpack_unique_connection ) { |
||
| 40 | // jetpack_unique_connection option has never been set |
||
| 41 | $jetpack_unique_connection = array( |
||
| 42 | 'connected' => 0, |
||
| 43 | 'disconnected' => 0, |
||
| 44 | 'version' => '3.6.1', |
||
| 45 | ); |
||
| 46 | |||
| 47 | update_option( 'jetpack_unique_connection', $jetpack_unique_connection ); |
||
| 48 | |||
| 49 | //track unique connection |
||
| 50 | $jetpack = $this->get_jetpack();; |
||
| 51 | |||
| 52 | $jetpack->stat( 'connections', 'unique-connection' ); |
||
| 53 | $jetpack->do_stats( 'server_side' ); |
||
| 54 | } |
||
| 55 | |||
| 56 | // increment number of times connected |
||
| 57 | $jetpack_unique_connection['connected'] += 1; |
||
| 58 | Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); |
||
| 59 | |||
| 60 | $role = Jetpack::translate_current_user_to_role(); |
||
| 61 | |||
| 62 | if ( ! $role ) { |
||
| 63 | return new Jetpack_Error( 'no_role', 'Invalid request.', 400 ); |
||
| 64 | } |
||
| 65 | |||
| 66 | $cap = Jetpack::translate_role_to_cap( $role ); |
||
| 67 | if ( ! $cap ) { |
||
| 68 | return new Jetpack_Error( 'no_cap', 'Invalid request.', 400 ); |
||
| 69 | } |
||
| 70 | |||
| 71 | if ( ! empty( $data['error'] ) ) { |
||
| 72 | return new Jetpack_Error( $data['error'], 'Error included in the request.', 400 ); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ( ! isset( $data['state'] ) ) { |
||
| 76 | return new Jetpack_Error( 'no_state', 'Request must include state.', 400 ); |
||
| 77 | } |
||
| 78 | |||
| 79 | if ( ! ctype_digit( $data['state'] ) ) { |
||
| 80 | return new Jetpack_Error( $data['error'], 'State must be an integer.', 400 ); |
||
| 81 | } |
||
| 82 | |||
| 83 | $current_user_id = get_current_user_id(); |
||
| 84 | if ( $current_user_id != $data['state'] ) { |
||
| 85 | return new Jetpack_Error( 'wrong_state', 'State does not match current user.', 400 ); |
||
| 86 | } |
||
| 87 | |||
| 88 | if ( empty( $data['code'] ) ) { |
||
| 89 | return new Jetpack_Error( 'no_code', 'Request must include an authorization code.', 400 ); |
||
| 90 | } |
||
| 91 | |||
| 92 | $token = $this->get_token( $data ); |
||
| 93 | |||
| 94 | if ( is_wp_error( $token ) ) { |
||
| 95 | $code = $token->get_error_code(); |
||
| 96 | if ( empty( $code ) ) { |
||
| 97 | $code = 'invalid_token'; |
||
| 98 | } |
||
| 99 | return new Jetpack_Error( $code, $token->get_error_message(), 400 ); |
||
| 100 | } |
||
| 101 | |||
| 102 | if ( ! $token ) { |
||
| 103 | return new Jetpack_Error( 'no_token', 'Error generating token.', 400 ); |
||
| 104 | } |
||
| 105 | |||
| 106 | $is_master_user = ! Jetpack::is_active(); |
||
| 107 | |||
| 108 | Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_master_user ); |
||
| 109 | |||
| 110 | if ( ! $is_master_user ) { |
||
| 111 | Jetpack::state( 'message', 'linked' ); |
||
| 112 | // Don't activate anything since we are just connecting a user. |
||
| 113 | return 'linked'; |
||
| 114 | } |
||
| 115 | |||
| 116 | $redirect_on_activation_error = ( 'client' === $data['auth_type'] ) ? true : false; |
||
| 117 | if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { |
||
| 118 | Jetpack_Options::delete_option( 'active_modules' ); |
||
| 119 | |||
| 120 | Jetpack::activate_default_modules( 999, 1, $active_modules, $redirect_on_activation_error ); |
||
| 121 | } else { |
||
| 122 | Jetpack::activate_default_modules( false, false, array(), $redirect_on_activation_error ); |
||
| 123 | } |
||
| 124 | |||
| 125 | // Start nonce cleaner |
||
| 126 | wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
||
| 127 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
| 128 | |||
| 129 | Jetpack::state( 'message', 'authorized' ); |
||
| 130 | return 'authorized'; |
||
| 131 | } |
||
| 132 | |||
| 133 | public static function deactivate_plugin( $probable_file, $probable_title ) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return object|WP_Error |
||
| 155 | */ |
||
| 156 | function get_token( $data ) { |
||
| 255 | |||
| 256 | public function get_jetpack() { |
||
| 259 | |||
| 260 | public function check_admin_referer( $action ) { |
||
| 263 | |||
| 264 | public function wp_safe_redirect( $redirect ) { |
||
| 267 | |||
| 268 | public function do_exit() { |
||
| 271 | } |
||
| 272 |
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.