Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | View Code Duplication | if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { |
|
127 | Jetpack::delete_active_modules(); |
||
128 | |||
129 | Jetpack::activate_default_modules( 999, 1, $active_modules, $redirect_on_activation_error, false ); |
||
130 | } else { |
||
131 | Jetpack::activate_default_modules( false, false, array(), $redirect_on_activation_error, false ); |
||
132 | } |
||
133 | |||
134 | // If redirect_uri is SSO, ensure SSO module is enabled |
||
135 | parse_str( parse_url( $data['redirect_uri'], PHP_URL_QUERY ), $redirect_options ); |
||
136 | /** This filter is documented in class.jetpack-cli.php */ |
||
137 | if ( isset( $redirect_options['action'] ) && 'jetpack-sso' === $redirect_options['action'] && apply_filters( 'jetpack_start_enable_sso', true ) ) { |
||
138 | Jetpack::activate_module( 'sso', false, false ); |
||
139 | } |
||
140 | |||
141 | // Since this is a fresh connection, be sure to clear out IDC options |
||
142 | Jetpack_IDC::clear_all_idc_options(); |
||
143 | |||
144 | // Start nonce cleaner |
||
145 | wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
||
146 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
147 | |||
148 | Jetpack::state( 'message', 'authorized' ); |
||
149 | return 'authorized'; |
||
150 | } |
||
151 | |||
152 | public static function deactivate_plugin( $probable_file, $probable_title ) { |
||
171 | |||
172 | /** |
||
173 | * @return object|WP_Error |
||
174 | */ |
||
175 | function get_token( $data ) { |
||
274 | |||
275 | public function get_jetpack() { |
||
278 | |||
279 | public function do_exit() { |
||
282 | } |
||
283 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.