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 ) { |
||
| 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 ) { |
||
| 118 | return $this->verify_action( array( 'register', $data[0], $data[1] ) ); |
||
| 119 | } |
||
| 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 ) { |
||
| 138 | $action = $params[0]; |
||
| 139 | $verify_secret = $params[1]; |
||
| 140 | $state = isset( $params[2] ) ? $params[2] : ''; |
||
| 141 | |||
| 142 | if ( empty( $verify_secret ) ) { |
||
| 143 | return $this->error( new Jetpack_Error( 'verify_secret_1_missing', sprintf( 'The required "%s" parameter is missing.', 'secret_1' ), 400 ) ); |
||
| 144 | } else if ( ! is_string( $verify_secret ) ) { |
||
| 145 | return $this->error( new Jetpack_Error( 'verify_secret_1_malformed', sprintf( 'The required "%s" parameter is malformed.', 'secret_1' ), 400 ) ); |
||
| 146 | } |
||
| 147 | |||
| 148 | $secrets = Jetpack_Options::get_option( $action ); |
||
| 149 | if ( !$secrets || is_wp_error( $secrets ) ) { |
||
| 150 | Jetpack_Options::delete_option( $action ); |
||
| 151 | return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification took too long', 400 ) ); |
||
| 152 | } |
||
| 153 | |||
| 154 | @list( $secret_1, $secret_2, $secret_eol, $user_id ) = explode( ':', $secrets ); |
||
|
|
|||
| 155 | |||
| 156 | if ( empty( $secret_1 ) || empty( $secret_2 ) || empty( $secret_eol ) || $secret_eol < time() ) { |
||
| 157 | Jetpack_Options::delete_option( $action ); |
||
| 158 | return $this->error( new Jetpack_Error( 'verify_secrets_missing', 'Verification took too long', 400 ) ); |
||
| 159 | } |
||
| 160 | |||
| 161 | if ( ! hash_equals( $verify_secret, $secret_1 ) ) { |
||
| 162 | Jetpack_Options::delete_option( $action ); |
||
| 163 | return $this->error( new Jetpack_Error( 'verify_secrets_mismatch', 'Secret mismatch', 400 ) ); |
||
| 164 | } |
||
| 165 | |||
| 166 | if ( in_array( $action, array( 'authorize', 'register' ) ) ) { |
||
| 167 | // 'authorize' and 'register' actions require further testing |
||
| 168 | if ( empty( $state ) ) { |
||
| 169 | return $this->error( new Jetpack_Error( 'state_missing', sprintf( 'The required "%s" parameter is missing.', 'state' ), 400 ) ); |
||
| 170 | } else if ( ! ctype_digit( $state ) ) { |
||
| 171 | return $this->error( new Jetpack_Error( 'state_malformed', sprintf( 'The required "%s" parameter is malformed.', 'state' ), 400 ) ); |
||
| 172 | } |
||
| 173 | if ( empty( $user_id ) || $user_id !== $state ) { |
||
| 174 | Jetpack_Options::delete_option( $action ); |
||
| 175 | return $this->error( new Jetpack_Error( 'invalid_state', 'State is invalid', 400 ) ); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | Jetpack_Options::delete_option( $action ); |
||
| 180 | |||
| 181 | return $secret_2; |
||
| 182 | } |
||
| 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 ) { |
||
| 337 | if ( !$id = (int) $id ) { |
||
| 338 | return false; |
||
| 344 | |||
| 345 | function get_posts( $args ) { |
||
| 351 | |||
| 352 | function get_comment( $id ) { |
||
| 368 | |||
| 369 | function get_comments( $args ) { |
||
| 375 | |||
| 376 | function update_attachment_parent( $args ) { |
||
| 385 | |||
| 386 | function json_api( $args = array() ) { |
||
| 480 | } |
||
| 481 |
If you suppress an error, we recommend checking for the error condition explicitly: