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() { |
||
| 71 | return array( |
||
| 72 | 'jetpack.verifyRegistration' => array( $this, 'verify_registration' ), |
||
| 73 | 'jetpack.remoteAuthorize' => array( $this, 'remote_authorize' ), |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | function authorize_xmlrpc_methods() { |
||
| 83 | |||
| 84 | function activate_manage( $request ) { |
||
| 100 | |||
| 101 | function remote_authorize( $request ) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Verifies that Jetpack.WordPress.com received a registration request from this site |
||
| 142 | */ |
||
| 143 | function verify_registration( $data ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure |
||
| 149 | * |
||
| 150 | * Possible error_codes: |
||
| 151 | * |
||
| 152 | * verify_secret_1_missing |
||
| 153 | * verify_secret_1_malformed |
||
| 154 | * verify_secrets_missing: No longer have verification secrets stored |
||
| 155 | * verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com |
||
| 156 | * |
||
| 157 | * The 'authorize' and 'register' actions have additional error codes |
||
| 158 | * |
||
| 159 | * state_missing: a state ( user id ) was not supplied |
||
| 160 | * state_malformed: state is not the correct data type |
||
| 161 | * invalid_state: supplied state does not match the stored state |
||
| 162 | */ |
||
| 163 | function verify_action( $params ) { |
||
| 164 | $action = $params[0]; |
||
| 165 | $verify_secret = $params[1]; |
||
| 166 | $state = isset( $params[2] ) ? $params[2] : ''; |
||
| 167 | |||
| 168 | if ( empty( $verify_secret ) ) { |
||
| 169 | return $this->error( new Jetpack_Error( 'verify_secret_1_missing', sprintf( 'The required "%s" parameter is missing.', 'secret_1' ), 400 ) ); |
||
| 170 | } else if ( ! is_string( $verify_secret ) ) { |
||
| 171 | return $this->error( new Jetpack_Error( 'verify_secret_1_malformed', sprintf( 'The required "%s" parameter is malformed.', 'secret_1' ), 400 ) ); |
||
| 172 | } |
||
| 173 | |||
| 174 | $secrets = Jetpack_Options::get_option( $action ); |
||
| 175 | if ( !$secrets || is_wp_error( $secrets ) ) { |
||
| 176 | Jetpack_Options::delete_option( $action ); |
||
| 177 | return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification took too long', 400 ) ); |
||
| 178 | } |
||
| 179 | |||
| 180 | @list( $secret_1, $secret_2, $secret_eol, $user_id ) = explode( ':', $secrets ); |
||
| 181 | |||
| 182 | if ( empty( $secret_1 ) || empty( $secret_2 ) || empty( $secret_eol ) || $secret_eol < time() ) { |
||
| 183 | Jetpack_Options::delete_option( $action ); |
||
| 184 | return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification took too long', 400 ) ); |
||
| 185 | } |
||
| 186 | |||
| 187 | if ( ! hash_equals( $verify_secret, $secret_1 ) ) { |
||
| 188 | Jetpack_Options::delete_option( $action ); |
||
| 189 | return $this->error( new Jetpack_Error( 'verify_secrets_mismatch', 'Secret mismatch', 400 ) ); |
||
| 190 | } |
||
| 191 | |||
| 192 | if ( in_array( $action, array( 'authorize', 'register' ) ) ) { |
||
| 193 | // 'authorize' and 'register' actions require further testing |
||
| 194 | if ( empty( $state ) ) { |
||
| 195 | return $this->error( new Jetpack_Error( 'state_missing', sprintf( 'The required "%s" parameter is missing.', 'state' ), 400 ) ); |
||
| 196 | } else if ( ! ctype_digit( $state ) ) { |
||
| 197 | return $this->error( new Jetpack_Error( 'state_malformed', sprintf( 'The required "%s" parameter is malformed.', 'state' ), 400 ) ); |
||
| 198 | } |
||
| 199 | if ( empty( $user_id ) || $user_id !== $state ) { |
||
| 200 | Jetpack_Options::delete_option( $action ); |
||
| 201 | return $this->error( new Jetpack_Error( 'invalid_state', 'State is invalid', 400 ) ); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | Jetpack_Options::delete_option( $action ); |
||
| 206 | |||
| 207 | return $secret_2; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Wrapper for wp_authenticate( $username, $password ); |
||
| 212 | * |
||
| 213 | * @return WP_User|IXR_Error |
||
| 214 | */ |
||
| 215 | function login() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns the current error as an IXR_Error |
||
| 235 | * |
||
| 236 | * @return null|IXR_Error |
||
| 237 | */ |
||
| 238 | function error( $error = null ) { |
||
| 256 | |||
| 257 | /* API Methods */ |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Just authenticates with the given Jetpack credentials. |
||
| 261 | * |
||
| 262 | * @return bool|IXR_Error |
||
| 263 | */ |
||
| 264 | function test_connection() { |
||
| 267 | |||
| 268 | function test_api_user_code( $args ) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Disconnect this blog from the connected wordpress.com account |
||
| 313 | * @return boolean |
||
| 314 | */ |
||
| 315 | function disconnect_blog() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Unlink a user from WordPress.com |
||
| 324 | * |
||
| 325 | * This will fail if called by the Master User. |
||
| 326 | */ |
||
| 327 | function unlink_user() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns what features are available. Uses the slug of the module files. |
||
| 334 | * |
||
| 335 | * @return array|IXR_Error |
||
| 336 | */ |
||
| 337 | View Code Duplication | function features_available() { |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Returns what features are enabled. Uses the slug of the modules files. |
||
| 349 | * |
||
| 350 | * @return array|IXR_Error |
||
| 351 | */ |
||
| 352 | View Code Duplication | function features_enabled() { |
|
| 361 | |||
| 362 | function get_post( $id ) { |
||
| 372 | |||
| 373 | View Code Duplication | function get_posts( $args ) { |
|
| 381 | |||
| 382 | function get_comment( $id ) { |
||
| 400 | |||
| 401 | View Code Duplication | function get_comments( $args ) { |
|
| 409 | |||
| 410 | function update_attachment_parent( $args ) { |
||
| 419 | |||
| 420 | function json_api( $args = array() ) { |
||
| 514 | } |
||
| 515 |
If you suppress an error, we recommend checking for the error condition explicitly: