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 | function authorize() { |
||
135 | |||
136 | public static function deactivate_plugin( $probable_file, $probable_title ) { |
||
155 | |||
156 | /** |
||
157 | * @return object|WP_Error |
||
158 | */ |
||
159 | function get_token( $data ) { |
||
160 | $jetpack = $this->get_jetpack(); |
||
161 | $role = $jetpack->translate_current_user_to_role(); |
||
162 | |||
163 | if ( !$role ) { |
||
164 | return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) ); |
||
165 | } |
||
166 | |||
167 | $client_secret = Jetpack_Data::get_access_token(); |
||
168 | if ( !$client_secret ) { |
||
169 | return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) ); |
||
170 | } |
||
171 | |||
172 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
173 | |||
174 | $body = array( |
||
175 | 'client_id' => Jetpack_Options::get_option( 'id' ), |
||
176 | 'client_secret' => $client_secret->secret, |
||
177 | 'grant_type' => 'authorization_code', |
||
178 | 'code' => $data['code'], |
||
179 | 'redirect_uri' => add_query_arg( array( |
||
180 | 'action' => 'authorize', |
||
181 | '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ), |
||
182 | 'redirect' => $redirect ? urlencode( $redirect ) : false, |
||
183 | ), menu_page_url( 'jetpack', false ) ), |
||
184 | ); |
||
185 | |||
186 | $args = array( |
||
187 | 'method' => 'POST', |
||
188 | 'body' => $body, |
||
189 | 'headers' => array( |
||
190 | 'Accept' => 'application/json', |
||
191 | ), |
||
192 | ); |
||
193 | $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'token' ) ), $args ); |
||
194 | |||
195 | if ( is_wp_error( $response ) ) { |
||
196 | return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() ); |
||
197 | } |
||
198 | |||
199 | $code = wp_remote_retrieve_response_code( $response ); |
||
200 | $entity = wp_remote_retrieve_body( $response ); |
||
201 | |||
202 | if ( $entity ) |
||
203 | $json = json_decode( $entity ); |
||
204 | else |
||
205 | $json = false; |
||
206 | |||
207 | if ( 200 != $code || !empty( $json->error ) ) { |
||
208 | if ( empty( $json->error ) ) |
||
209 | return new Jetpack_Error( 'unknown', '', $code ); |
||
210 | |||
211 | $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; |
||
212 | |||
213 | return new Jetpack_Error( (string) $json->error, $error_description, $code ); |
||
214 | } |
||
215 | |||
216 | if ( empty( $json->access_token ) || !is_scalar( $json->access_token ) ) { |
||
217 | return new Jetpack_Error( 'access_token', '', $code ); |
||
218 | } |
||
219 | |||
220 | if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) { |
||
221 | return new Jetpack_Error( 'token_type', '', $code ); |
||
222 | } |
||
223 | |||
224 | if ( empty( $json->scope ) ) { |
||
225 | return new Jetpack_Error( 'scope', 'No Scope', $code ); |
||
226 | } |
||
227 | @list( $role, $hmac ) = explode( ':', $json->scope ); |
||
228 | if ( empty( $role ) || empty( $hmac ) ) { |
||
229 | return new Jetpack_Error( 'scope', 'Malformed Scope', $code ); |
||
230 | } |
||
231 | if ( $jetpack->sign_role( $role ) !== $json->scope ) { |
||
232 | return new Jetpack_Error( 'scope', 'Invalid Scope', $code ); |
||
233 | } |
||
234 | |||
235 | if ( !$cap = $jetpack->translate_role_to_cap( $role ) ) |
||
236 | return new Jetpack_Error( 'scope', 'No Cap', $code ); |
||
237 | if ( ! current_user_can( $cap ) ) |
||
238 | return new Jetpack_Error( 'scope', 'current_user_cannot', $code ); |
||
239 | |||
240 | /** |
||
241 | * Fires after user has successfully received an auth token. |
||
242 | * |
||
243 | * @since 3.9.0 |
||
244 | */ |
||
245 | do_action( 'jetpack_user_authorized' ); |
||
246 | |||
247 | return (string) $json->access_token; |
||
248 | } |
||
249 | |||
250 | public function get_jetpack() { |
||
253 | |||
254 | public function check_admin_referer( $action ) { |
||
257 | |||
258 | public function wp_safe_redirect( $redirect ) { |
||
261 | |||
262 | public function do_exit() { |
||
265 | |||
266 | } |
||
267 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: