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_Sync_Module_Users 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_Sync_Module_Users, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { |
||
| 4 | const MAX_INITIAL_SYNC_USERS = 100; |
||
| 5 | |||
| 6 | function name() { |
||
| 9 | |||
| 10 | // this is here to support the backfill API |
||
| 11 | public function get_object_by_id( $object_type, $id ) { |
||
| 18 | |||
| 19 | public function init_listeners( $callable ) { |
||
| 20 | // users |
||
| 21 | add_action( 'user_register', array( $this, 'save_user_handler' ) ); |
||
| 22 | add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); |
||
| 23 | add_action( 'add_user_to_blog', array( $this, 'save_user_handler' ) ); |
||
| 24 | add_action( 'jetpack_sync_add_user', $callable, 10, 2 ); |
||
| 25 | add_action( 'jetpack_sync_register_user', $callable, 10, 2 ); |
||
| 26 | add_action( 'jetpack_sync_save_user', $callable ); |
||
| 27 | |||
| 28 | //Edit user info, see https://github.com/WordPress/WordPress/blob/c05f1dc805bddcc0e76fd90c4aaf2d9ea76dc0fb/wp-admin/user-edit.php#L126 |
||
| 29 | add_action( 'personal_options_update', array( $this, 'edited_user_handler' ) ); |
||
| 30 | add_action( 'edit_user_profile_update', array( $this, 'edited_user_handler' ) ); |
||
| 31 | add_action( 'jetpack_user_edited', $callable ); |
||
| 32 | |||
| 33 | add_action( 'jetpack_sync_user_locale', $callable, 10, 2 ); |
||
| 34 | add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 ); |
||
| 35 | |||
| 36 | add_action( 'deleted_user', array( $this, 'deleted_user_handler' ), 10, 2 ); |
||
| 37 | add_action( 'jetpack_deleted_user', $callable, 10, 3 ); |
||
| 38 | add_action( 'remove_user_from_blog', array( $this, 'remove_user_from_blog_handler' ), 10, 2 ); |
||
| 39 | add_action( 'jetpack_removed_user_from_blog', $callable, 10, 2 ); |
||
| 40 | |||
| 41 | // user roles |
||
| 42 | add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 43 | add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
||
| 44 | add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 45 | |||
| 46 | // user capabilities |
||
| 47 | add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 48 | add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 49 | add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 50 | |||
| 51 | // user authentication |
||
| 52 | add_action( 'wp_login', $callable, 10, 2 ); |
||
| 53 | add_action( 'wp_logout', $callable, 10, 0 ); |
||
| 54 | add_action( 'wp_masterbar_logout', $callable, 10, 0 ); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function init_full_sync_listeners( $callable ) { |
||
| 58 | add_action( 'jetpack_full_sync_users', $callable ); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function init_before_send() { |
||
| 62 | add_filter( 'jetpack_sync_before_send_jetpack_sync_add_user', array( $this, 'expand_user' ) ); |
||
| 63 | add_filter( 'jetpack_sync_before_send_jetpack_sync_register_user', array( $this, 'expand_user' ) ); |
||
| 64 | add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ), 10, 2 ); |
||
| 65 | add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 ); |
||
| 66 | add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); |
||
| 67 | |||
| 68 | // full sync |
||
| 69 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function sanitize_user_and_expand( $user ) { |
||
| 73 | $user = $this->add_to_user( $user ); |
||
| 74 | return $this->sanitize_user( $user ); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function sanitize_user( $user ) { |
||
| 78 | // this create a new user object and stops the passing of the object by reference. |
||
| 79 | $user = unserialize( serialize( $user ) ); |
||
| 80 | |||
| 81 | if ( is_object( $user ) && is_object( $user->data ) ) { |
||
| 82 | unset( $user->data->user_pass ); |
||
| 83 | } |
||
| 84 | |||
| 85 | $user->allcaps = $this->get_real_user_capabilities( $user ); |
||
| 86 | return $user; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function add_to_user( $user ) { |
||
| 90 | $user->allowed_mime_types = get_allowed_mime_types( $user ); |
||
| 91 | |||
| 92 | if ( function_exists( 'get_user_locale' ) ) { |
||
| 93 | |||
| 94 | // Only set the user locale if it is different from the site local |
||
| 95 | if ( get_locale() !== get_user_locale( $user->ID ) ) { |
||
| 96 | $user->locale = get_user_locale( $user->ID ); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | return $user; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function get_real_user_capabilities( $user ) { |
||
| 104 | $user_capabilities = array(); |
||
| 105 | foreach( Jetpack_Sync_Defaults::get_capabilities_whitelist() as $capability ) { |
||
| 106 | if ( $user_has_capabilities = user_can( $user , $capability ) ) { |
||
|
|
|||
| 107 | $user_capabilities[ $capability ] = true; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | return $user_capabilities; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function expand_user( $args ) { |
||
| 114 | list( $user ) = $args; |
||
| 115 | |||
| 116 | if ( $user ) { |
||
| 117 | return array( $this->add_to_user( $user ) ); |
||
| 118 | } |
||
| 119 | |||
| 120 | return false; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function expand_login_username( $args ) { |
||
| 129 | |||
| 130 | public function expand_logout_username( $args, $user_id ) { |
||
| 140 | |||
| 141 | public function deleted_user_handler( $deleted_user_id, $reassigned_user_id = '' ) { |
||
| 142 | $is_multisite = is_multisite(); |
||
| 143 | /** |
||
| 144 | * Fires when a user is deleted on a site |
||
| 145 | * |
||
| 146 | * @since 5.4.0 |
||
| 147 | * |
||
| 148 | * @param int $deleted_user_id - ID of the deleted user |
||
| 154 | |||
| 155 | public function edited_user_handler( $user_id ) { |
||
| 165 | |||
| 166 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 223 | |||
| 224 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
| 240 | |||
| 241 | function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { |
||
| 266 | |||
| 267 | function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
||
| 295 | |||
| 296 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 301 | |||
| 302 | public function estimate_full_sync_actions( $config ) { |
||
| 315 | |||
| 316 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 328 | |||
| 329 | function get_full_sync_actions() { |
||
| 332 | |||
| 333 | function get_initial_sync_user_config() { |
||
| 344 | |||
| 345 | public function expand_users( $args ) { |
||
| 350 | |||
| 351 | public function remove_user_from_blog_handler( $user_id, $blog_id ) { |
||
| 370 | |||
| 371 | private function is_add_new_user_to_blog() { |
||
| 374 | |||
| 375 | private function is_add_user_to_blog() { |
||
| 378 | |||
| 379 | private function is_create_user() { |
||
| 388 | |||
| 389 | private function get_reassigned_network_user_id() { |
||
| 402 | } |
||
| 403 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.