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 |
||
| 20 | class Jetpack_XMLRPC_Server { |
||
| 21 | /** |
||
| 22 | * The current error object |
||
| 23 | * |
||
| 24 | * @var \WP_Error |
||
| 25 | */ |
||
| 26 | public $error = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The current user |
||
| 30 | * |
||
| 31 | * @var \WP_User |
||
| 32 | */ |
||
| 33 | public $user = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The tracking manager object. |
||
| 37 | * |
||
| 38 | * @var Automattic\Jetpack\Tracking |
||
| 39 | */ |
||
| 40 | private $tracking; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The connection manager object. |
||
| 44 | * |
||
| 45 | * @var Automattic\Jetpack\Connection\Manager |
||
| 46 | */ |
||
| 47 | private $connection; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Creates a new XMLRPC server object. |
||
| 51 | * |
||
| 52 | * @param Automattic\Jetpack\Connection\Manager $manager the connection manager object. |
||
|
|
|||
| 53 | */ |
||
| 54 | public function __construct( $manager = null ) { |
||
| 55 | $this->connection = is_null( $manager ) ? new Connection_Manager() : $manager; |
||
| 56 | $this->tracking = new Tracking( 'jetpack', $manager ); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Whitelist of the XML-RPC methods available to the Jetpack Server. If the |
||
| 61 | * user is not authenticated (->login()) then the methods are never added, |
||
| 62 | * so they will get a "does not exist" error. |
||
| 63 | * |
||
| 64 | * @param array $core_methods Core XMLRPC methods. |
||
| 65 | */ |
||
| 66 | public function xmlrpc_methods( $core_methods ) { |
||
| 67 | $jetpack_methods = array( |
||
| 68 | 'jetpack.jsonAPI' => array( $this, 'json_api' ), |
||
| 69 | 'jetpack.verifyAction' => array( $this, 'verify_action' ), |
||
| 70 | 'jetpack.getUser' => array( $this, 'get_user' ), |
||
| 71 | 'jetpack.remoteRegister' => array( $this, 'remote_register' ), |
||
| 72 | 'jetpack.remoteProvision' => array( $this, 'remote_provision' ), |
||
| 73 | ); |
||
| 74 | |||
| 75 | $this->user = $this->login(); |
||
| 76 | |||
| 77 | if ( $this->user ) { |
||
| 78 | $jetpack_methods = array_merge( |
||
| 79 | $jetpack_methods, |
||
| 80 | array( |
||
| 81 | 'jetpack.testConnection' => array( $this, 'test_connection' ), |
||
| 82 | 'jetpack.testAPIUserCode' => array( $this, 'test_api_user_code' ), |
||
| 83 | 'jetpack.featuresAvailable' => array( $this, 'features_available' ), |
||
| 84 | 'jetpack.featuresEnabled' => array( $this, 'features_enabled' ), |
||
| 85 | 'jetpack.disconnectBlog' => array( $this, 'disconnect_blog' ), |
||
| 86 | 'jetpack.unlinkUser' => array( $this, 'unlink_user' ), |
||
| 87 | 'jetpack.idcUrlValidation' => array( $this, 'validate_urls_for_idc_mitigation' ), |
||
| 88 | ) |
||
| 89 | ); |
||
| 90 | |||
| 91 | if ( isset( $core_methods['metaWeblog.editPost'] ) ) { |
||
| 92 | $jetpack_methods['metaWeblog.newMediaObject'] = $core_methods['metaWeblog.newMediaObject']; |
||
| 93 | $jetpack_methods['jetpack.updateAttachmentParent'] = array( $this, 'update_attachment_parent' ); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Filters the XML-RPC methods available to Jetpack for authenticated users. |
||
| 98 | * |
||
| 99 | * @since 1.1.0 |
||
| 100 | * |
||
| 101 | * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
||
| 102 | * @param array $core_methods Available core XML-RPC methods. |
||
| 103 | * @param \WP_User $user Information about a given WordPress user. |
||
| 104 | */ |
||
| 105 | $jetpack_methods = apply_filters( 'jetpack_xmlrpc_methods', $jetpack_methods, $core_methods, $this->user ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Filters the XML-RPC methods available to Jetpack for unauthenticated users. |
||
| 110 | * |
||
| 111 | * @since 3.0.0 |
||
| 112 | * |
||
| 113 | * @param array $jetpack_methods XML-RPC methods available to the Jetpack Server. |
||
| 114 | * @param array $core_methods Available core XML-RPC methods. |
||
| 115 | */ |
||
| 116 | return apply_filters( 'jetpack_xmlrpc_unauthenticated_methods', $jetpack_methods, $core_methods ); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Whitelist of the bootstrap XML-RPC methods |
||
| 121 | */ |
||
| 122 | public function bootstrap_xmlrpc_methods() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Additional method needed for authorization calls. |
||
| 131 | */ |
||
| 132 | public function authorize_xmlrpc_methods() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Remote provisioning methods. |
||
| 140 | */ |
||
| 141 | public function provision_xmlrpc_methods() { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Used to verify whether a local user exists and what role they have. |
||
| 152 | * |
||
| 153 | * @param int|string|array $request One of: |
||
| 154 | * int|string The local User's ID, username, or email address. |
||
| 155 | * array A request array containing: |
||
| 156 | * 0: int|string The local User's ID, username, or email address. |
||
| 157 | * |
||
| 158 | * @return array|\IXR_Error Information about the user, or error if no such user found: |
||
| 159 | * roles: string[] The user's rols. |
||
| 160 | * login: string The user's username. |
||
| 161 | * email_hash string[] The MD5 hash of the user's normalized email address. |
||
| 162 | * caps string[] The user's capabilities. |
||
| 163 | * allcaps string[] The user's granular capabilities, merged from role capabilities. |
||
| 164 | * token_key string The Token Key of the user's Jetpack token. Empty string if none. |
||
| 165 | */ |
||
| 166 | public function get_user( $request ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Remote authorization XMLRPC method handler. |
||
| 217 | * |
||
| 218 | * @param array $request the request. |
||
| 219 | */ |
||
| 220 | public function remote_authorize( $request ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * This XML-RPC method is called from the /jpphp/provision endpoint on WPCOM in order to |
||
| 262 | * register this site so that a plan can be provisioned. |
||
| 263 | * |
||
| 264 | * @param array $request An array containing at minimum nonce and local_user keys. |
||
| 265 | * |
||
| 266 | * @return \WP_Error|array |
||
| 267 | */ |
||
| 268 | public function remote_register( $request ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * This XML-RPC method is called from the /jpphp/provision endpoint on WPCOM in order to |
||
| 347 | * register this site so that a plan can be provisioned. |
||
| 348 | * |
||
| 349 | * @param array $request An array containing at minimum a nonce key and a local_username key. |
||
| 350 | * |
||
| 351 | * @return \WP_Error|array |
||
| 352 | */ |
||
| 353 | public function remote_provision( $request ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Given an array containing a local user identifier and a nonce, will attempt to fetch and set |
||
| 411 | * an access token for the given user. |
||
| 412 | * |
||
| 413 | * @param array $request An array containing local_user and nonce keys at minimum. |
||
| 414 | * @param \IXR_Client $ixr_client The client object, optional. |
||
| 415 | * @return mixed |
||
| 416 | */ |
||
| 417 | public function remote_connect( $request, $ixr_client = false ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Getter for the local user to act as. |
||
| 486 | * |
||
| 487 | * @param array $request the current request data. |
||
| 488 | */ |
||
| 489 | private function fetch_and_verify_local_user( $request ) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Gets the user object by its data. |
||
| 509 | * |
||
| 510 | * @param string $user_id can be any identifying user data. |
||
| 511 | */ |
||
| 512 | private function get_user_by_anything( $user_id ) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Track an error. |
||
| 528 | * |
||
| 529 | * @param string $name Event name. |
||
| 530 | * @param \WP_Error|\IXR_Error $error The error object. |
||
| 531 | * @param \WP_User $user The user object. |
||
| 532 | */ |
||
| 533 | private function tracks_record_error( $name, $error, $user = null ) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Possible error_codes: |
||
| 559 | * |
||
| 560 | * - verify_secret_1_missing |
||
| 561 | * - verify_secret_1_malformed |
||
| 562 | * - verify_secrets_missing: verification secrets are not found in database |
||
| 563 | * - verify_secrets_incomplete: verification secrets are only partially found in database |
||
| 564 | * - verify_secrets_expired: verification secrets have expired |
||
| 565 | * - verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com |
||
| 566 | * - state_missing: required parameter of state not found |
||
| 567 | * - state_malformed: state is not a digit |
||
| 568 | * - invalid_state: state in request does not match the stored state |
||
| 569 | * |
||
| 570 | * The 'authorize' and 'register' actions have additional error codes |
||
| 571 | * |
||
| 572 | * state_missing: a state ( user id ) was not supplied |
||
| 573 | * state_malformed: state is not the correct data type |
||
| 574 | * invalid_state: supplied state does not match the stored state |
||
| 575 | * |
||
| 576 | * @param array $params action An array of 3 parameters: |
||
| 577 | * [0]: string action. Possible values are `authorize`, `publicize` and `register`. |
||
| 578 | * [1]: string secret_1. |
||
| 579 | * [2]: int state. |
||
| 580 | * @return \IXR_Error|string IXR_Error on failure, secret_2 on success. |
||
| 581 | */ |
||
| 582 | public function verify_action( $params ) { |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Wrapper for wp_authenticate( $username, $password ); |
||
| 598 | * |
||
| 599 | * @return \WP_User|bool |
||
| 600 | */ |
||
| 601 | public function login() { |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Returns the current error as an \IXR_Error |
||
| 621 | * |
||
| 622 | * @param \WP_Error|\IXR_Error $error The error object, optional. |
||
| 623 | * @param string $tracks_event_name The event name. |
||
| 624 | * @param \WP_User $user The user object. |
||
| 625 | * @return bool|\IXR_Error |
||
| 626 | */ |
||
| 627 | public function error( $error = null, $tracks_event_name = null, $user = null ) { |
||
| 650 | |||
| 651 | /* API Methods */ |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Just authenticates with the given Jetpack credentials. |
||
| 655 | * |
||
| 656 | * @return string The current Jetpack version number |
||
| 657 | */ |
||
| 658 | public function test_connection() { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Test the API user code. |
||
| 664 | * |
||
| 665 | * @param array $args arguments identifying the test site. |
||
| 666 | */ |
||
| 667 | public function test_api_user_code( $args ) { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Disconnect this blog from the connected wordpress.com account |
||
| 719 | * |
||
| 720 | * @return boolean |
||
| 721 | */ |
||
| 722 | public function disconnect_blog() { |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Unlink a user from WordPress.com |
||
| 745 | * |
||
| 746 | * This will fail if called by the Master User. |
||
| 747 | */ |
||
| 748 | public function unlink_user() { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Returns any object that is able to be synced. |
||
| 763 | * |
||
| 764 | * @deprecated since 7.8.0 |
||
| 765 | * @see Automattic\Jetpack\Sync\Sender::sync_object() |
||
| 766 | * |
||
| 767 | * @param array $args the synchronized object parameters. |
||
| 768 | * @return string Encoded sync object. |
||
| 769 | */ |
||
| 770 | public function sync_object( $args ) { |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Returns the home URL and site URL for the current site which can be used on the WPCOM side for |
||
| 777 | * IDC mitigation to decide whether sync should be allowed if the home and siteurl values differ between WPCOM |
||
| 778 | * and the remote Jetpack site. |
||
| 779 | * |
||
| 780 | * @return array |
||
| 781 | */ |
||
| 782 | public function validate_urls_for_idc_mitigation() { |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Returns what features are available. Uses the slug of the module files. |
||
| 791 | * |
||
| 792 | * @return array |
||
| 793 | */ |
||
| 794 | View Code Duplication | public function features_available() { |
|
| 803 | |||
| 804 | /** |
||
| 805 | * Returns what features are enabled. Uses the slug of the modules files. |
||
| 806 | * |
||
| 807 | * @return array |
||
| 808 | */ |
||
| 809 | View Code Duplication | public function features_enabled() { |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Updates the attachment parent object. |
||
| 821 | * |
||
| 822 | * @param array $args attachment and parent identifiers. |
||
| 823 | */ |
||
| 824 | public function update_attachment_parent( $args ) { |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Serve a JSON API request. |
||
| 838 | * |
||
| 839 | * @param array $args request arguments. |
||
| 840 | */ |
||
| 841 | public function json_api( $args = array() ) { |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Handles authorization actions after connecting a site, such as enabling modules. |
||
| 946 | * |
||
| 947 | * This do_post_authorization() is used in this class, as opposed to calling |
||
| 948 | * Jetpack::handle_post_authorization_actions() directly so that we can mock this method as necessary. |
||
| 949 | * |
||
| 950 | * @return void |
||
| 951 | */ |
||
| 952 | public function do_post_authorization() { |
||
| 957 | } |
||
| 958 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.