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() { |
||
| 42 | |||
| 43 | function authorize( $data = array() ) { |
||
| 44 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
|
|
|||
| 45 | |||
| 46 | $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ); |
||
| 47 | // Checking if site has been active/connected previously before recording unique connection |
||
| 48 | if ( ! $jetpack_unique_connection ) { |
||
| 49 | // jetpack_unique_connection option has never been set |
||
| 50 | $jetpack_unique_connection = array( |
||
| 51 | 'connected' => 0, |
||
| 52 | 'disconnected' => 0, |
||
| 53 | 'version' => '3.6.1', |
||
| 54 | ); |
||
| 55 | |||
| 56 | update_option( 'jetpack_unique_connection', $jetpack_unique_connection ); |
||
| 57 | |||
| 58 | //track unique connection |
||
| 59 | $jetpack = $this->get_jetpack();; |
||
| 60 | |||
| 61 | $jetpack->stat( 'connections', 'unique-connection' ); |
||
| 62 | $jetpack->do_stats( 'server_side' ); |
||
| 63 | } |
||
| 64 | |||
| 65 | // increment number of times connected |
||
| 66 | $jetpack_unique_connection['connected'] += 1; |
||
| 67 | Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); |
||
| 68 | |||
| 69 | $role = Jetpack::translate_current_user_to_role(); |
||
| 70 | |||
| 71 | if ( ! $role ) { |
||
| 72 | return new Jetpack_Error( 'no_role', 'Invalid request.', 400 ); |
||
| 73 | } |
||
| 74 | |||
| 75 | $cap = Jetpack::translate_role_to_cap( $role ); |
||
| 76 | if ( ! $cap ) { |
||
| 77 | return new Jetpack_Error( 'no_cap', 'Invalid request.', 400 ); |
||
| 78 | } |
||
| 79 | |||
| 80 | if ( ! empty( $data['error'] ) ) { |
||
| 81 | return new Jetpack_Error( $data['error'], 'Error included in the request.', 400 ); |
||
| 82 | } |
||
| 83 | |||
| 84 | if ( ! isset( $data['state'] ) ) { |
||
| 85 | return new Jetpack_Error( 'no_state', 'Request must include state.', 400 ); |
||
| 86 | } |
||
| 87 | |||
| 88 | if ( ! ctype_digit( $data['state'] ) ) { |
||
| 89 | return new Jetpack_Error( $data['error'], 'State must be an integer.', 400 ); |
||
| 90 | } |
||
| 91 | |||
| 92 | $current_user_id = get_current_user_id(); |
||
| 93 | if ( $current_user_id != $data['state'] ) { |
||
| 94 | return new Jetpack_Error( 'wrong_state', 'State does not match current user.', 400 ); |
||
| 95 | } |
||
| 96 | |||
| 97 | if ( empty( $data['code'] ) ) { |
||
| 98 | return new Jetpack_Error( 'no_code', 'Request must include an authorization code.', 400 ); |
||
| 99 | } |
||
| 100 | |||
| 101 | $token = $this->get_token( $data ); |
||
| 102 | |||
| 103 | if ( is_wp_error( $token ) ) { |
||
| 104 | $code = $token->get_error_code(); |
||
| 105 | if ( empty( $code ) ) { |
||
| 106 | $code = 'invalid_token'; |
||
| 107 | } |
||
| 108 | return new Jetpack_Error( $code, $token->get_error_message(), 400 ); |
||
| 109 | } |
||
| 110 | |||
| 111 | if ( ! $token ) { |
||
| 112 | return new Jetpack_Error( 'no_token', 'Error generating token.', 400 ); |
||
| 113 | } |
||
| 114 | |||
| 115 | $is_master_user = ! Jetpack::is_active(); |
||
| 116 | |||
| 117 | Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_master_user ); |
||
| 118 | |||
| 119 | if ( ! $is_master_user ) { |
||
| 120 | Jetpack::state( 'message', 'linked' ); |
||
| 121 | // Don't activate anything since we are just connecting a user. |
||
| 122 | return 'linked'; |
||
| 123 | } |
||
| 124 | |||
| 125 | $redirect_on_activation_error = ( 'client' === $data['auth_type'] ) ? true : false; |
||
| 126 | if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { |
||
| 127 | Jetpack_Options::delete_option( 'active_modules' ); |
||
| 128 | |||
| 129 | Jetpack::activate_default_modules( 999, 1, $active_modules, $redirect_on_activation_error ); |
||
| 130 | } else { |
||
| 131 | Jetpack::activate_default_modules( false, false, array(), $redirect_on_activation_error ); |
||
| 132 | } |
||
| 133 | |||
| 134 | // Start nonce cleaner |
||
| 135 | wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
||
| 136 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
| 137 | |||
| 138 | Jetpack::state( 'message', 'authorized' ); |
||
| 139 | return 'authorized'; |
||
| 140 | } |
||
| 141 | |||
| 142 | public static function deactivate_plugin( $probable_file, $probable_title ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return object|WP_Error |
||
| 164 | */ |
||
| 165 | function get_token( $data ) { |
||
| 264 | |||
| 265 | public function get_jetpack() { |
||
| 268 | |||
| 269 | public function check_admin_referer( $action ) { |
||
| 272 | |||
| 273 | public function wp_safe_redirect( $redirect ) { |
||
| 276 | |||
| 277 | public function do_exit() { |
||
| 280 | } |
||
| 281 |
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.