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_XMLRPC_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_XMLRPC_Server, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class Jetpack_XMLRPC_Server { |
||
| 7 | /** |
||
| 8 | * The current error object |
||
| 9 | */ |
||
| 10 | public $error = null; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Whitelist of the XML-RPC methods available to the Jetpack Server. If the |
||
| 14 | * user is not authenticated (->login()) then the methods are never added, |
||
| 15 | * so they will get a "does not exist" error. |
||
| 16 | */ |
||
| 17 | function xmlrpc_methods( $core_methods ) { |
||
| 18 | $jetpack_methods = array( |
||
| 19 | 'jetpack.jsonAPI' => array( $this, 'json_api' ), |
||
| 20 | 'jetpack.verifyAction' => array( $this, 'verify_action' ), |
||
| 21 | ); |
||
| 22 | |||
| 23 | $user = $this->login(); |
||
| 24 | |||
| 25 | if ( $user ) { |
||
| 26 | $jetpack_methods = array_merge( $jetpack_methods, array( |
||
| 27 | 'jetpack.testConnection' => array( $this, 'test_connection' ), |
||
| 28 | 'jetpack.testAPIUserCode' => array( $this, 'test_api_user_code' ), |
||
| 29 | 'jetpack.featuresAvailable' => array( $this, 'features_available' ), |
||
| 30 | 'jetpack.featuresEnabled' => array( $this, 'features_enabled' ), |
||
| 31 | 'jetpack.getPost' => array( $this, 'get_post' ), |
||
| 32 | 'jetpack.getPosts' => array( $this, 'get_posts' ), |
||
| 33 | 'jetpack.getComment' => array( $this, 'get_comment' ), |
||
| 34 | 'jetpack.getComments' => array( $this, 'get_comments' ), |
||
| 35 | 'jetpack.disconnectBlog' => array( $this, 'disconnect_blog' ), |
||
| 36 | 'jetpack.unlinkUser' => array( $this, 'unlink_user' ), |
||
| 37 | ) ); |
||
| 38 | |||
| 39 | if ( isset( $core_methods['metaWeblog.editPost'] ) ) { |
||
| 40 | $jetpack_methods['metaWeblog.newMediaObject'] = $core_methods['metaWeblog.newMediaObject']; |
||
| 41 | $jetpack_methods['jetpack.updateAttachmentParent'] = array( $this, 'update_attachment_parent' ); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Filters the XML-RPC methods available to Jetpack for authenticated users. |
||
| 46 | * |
||
| 47 | * @since 1.1.0 |
||
| 48 | * |
||
| 49 | * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
||
| 50 | * @param array $core_methods Available core XML-RPC methods. |
||
| 51 | * @param WP_User $user Information about a given WordPress user. |
||
| 52 | */ |
||
| 53 | $jetpack_methods = apply_filters( 'jetpack_xmlrpc_methods', $jetpack_methods, $core_methods, $user ); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Filters the XML-RPC methods available to Jetpack for unauthenticated users. |
||
| 58 | * |
||
| 59 | * @since 3.0.0 |
||
| 60 | * |
||
| 61 | * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
||
| 62 | * @param array $core_methods Available core XML-RPC methods. |
||
| 63 | */ |
||
| 64 | return apply_filters( 'jetpack_xmlrpc_unauthenticated_methods', $jetpack_methods, $core_methods ); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Whitelist of the bootstrap XML-RPC methods |
||
| 69 | */ |
||
| 70 | function bootstrap_xmlrpc_methods() { |
||
| 76 | |||
| 77 | function authorize_xmlrpc_methods() { |
||
| 80 | |||
| 81 | function remote_authorize( $request ) { |
||
| 82 | foreach( array( 'secret', 'state', 'redirect_uri', 'code' ) as $required ) { |
||
| 83 | if ( ! isset( $request[ $required ] ) || empty( $request[ $required ] ) ) { |
||
| 84 | return $this->error( new Jetpack_Error( 'missing_parameter', 'One or more parameters is missing from the request.', 400 ) ); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | if ( ! get_user_by( 'id', $request['state'] ) ) { |
||
| 89 | return $this->error( new Jetpack_Error( 'user_unknown', 'User not found.', 404 ) ); |
||
| 90 | } |
||
| 91 | |||
| 92 | if ( Jetpack::is_active() && Jetpack::is_user_connected( $request['state'] ) ) { |
||
| 93 | return $this->error( new Jetpack_Error( 'already_connected', 'User already connected.', 400 ) ); |
||
| 94 | } |
||
| 95 | |||
| 96 | $verified = $this->verify_action( array( 'authorize', $request['secret'], $request['state'] ) ); |
||
| 97 | |||
| 98 | if ( is_a( $verified, 'IXR_Error' ) ) { |
||
| 99 | return $verified; |
||
| 100 | } |
||
| 101 | |||
| 102 | wp_set_current_user( $request['state'] ); |
||
| 103 | |||
| 104 | $client_server = new Jetpack_Client_Server; |
||
| 105 | $result = $client_server->authorize( $request ); |
||
| 106 | |||
| 107 | if ( is_wp_error( $result ) ) { |
||
| 108 | return $this->error( $result ); |
||
| 109 | } |
||
| 110 | |||
| 111 | return $result; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Verifies that Jetpack.WordPress.com received a registration request from this site |
||
| 116 | */ |
||
| 117 | function verify_registration( $data ) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure |
||
| 123 | * |
||
| 124 | * Possible error_codes: |
||
| 125 | * |
||
| 126 | * verify_secret_1_missing |
||
| 127 | * verify_secret_1_malformed |
||
| 128 | * verify_secrets_missing: No longer have verification secrets stored |
||
| 129 | * verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com |
||
| 130 | * |
||
| 131 | * The 'authorize' and 'register' actions have additional error codes |
||
| 132 | * |
||
| 133 | * state_missing: a state ( user id ) was not supplied |
||
| 134 | * state_malformed: state is not the correct data type |
||
| 135 | * invalid_state: supplied state does not match the stored state |
||
| 136 | */ |
||
| 137 | function verify_action( $params ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Wrapper for wp_authenticate( $username, $password ); |
||
| 186 | * |
||
| 187 | * @return WP_User|IXR_Error |
||
| 188 | */ |
||
| 189 | function login() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the current error as an IXR_Error |
||
| 209 | * |
||
| 210 | * @return null|IXR_Error |
||
| 211 | */ |
||
| 212 | function error( $error = null ) { |
||
| 230 | |||
| 231 | /* API Methods */ |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Just authenticates with the given Jetpack credentials. |
||
| 235 | * |
||
| 236 | * @return bool|IXR_Error |
||
| 237 | */ |
||
| 238 | function test_connection() { |
||
| 241 | |||
| 242 | function test_api_user_code( $args ) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Disconnect this blog from the connected wordpress.com account |
||
| 287 | * @return boolean |
||
| 288 | */ |
||
| 289 | function disconnect_blog() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Unlink a user from WordPress.com |
||
| 298 | * |
||
| 299 | * This will fail if called by the Master User. |
||
| 300 | */ |
||
| 301 | function unlink_user() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Returns what features are available. Uses the slug of the module files. |
||
| 308 | * |
||
| 309 | * @return array|IXR_Error |
||
| 310 | */ |
||
| 311 | View Code Duplication | function features_available() { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Returns what features are enabled. Uses the slug of the modules files. |
||
| 323 | * |
||
| 324 | * @return array|IXR_Error |
||
| 325 | */ |
||
| 326 | View Code Duplication | function features_enabled() { |
|
| 335 | |||
| 336 | function get_post( $id ) { |
||
| 346 | |||
| 347 | View Code Duplication | function get_posts( $args ) { |
|
| 355 | |||
| 356 | function get_comment( $id ) { |
||
| 374 | |||
| 375 | View Code Duplication | function get_comments( $args ) { |
|
| 383 | |||
| 384 | function update_attachment_parent( $args ) { |
||
| 393 | |||
| 394 | function json_api( $args = array() ) { |
||
| 488 | } |
||
| 489 |
If you suppress an error, we recommend checking for the error condition explicitly: