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_save_user', $callable, 10, 2 ); |
||
| 25 | add_action( 'jetpack_sync_user_locale', $callable, 10, 2 ); |
||
| 26 | add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 ); |
||
| 27 | |||
| 28 | add_action( 'deleted_user', $callable, 10, 2 ); |
||
| 29 | add_action( 'remove_user_from_blog', $callable, 10, 2 ); |
||
| 30 | |||
| 31 | // user roles |
||
| 32 | add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 33 | add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
||
| 34 | add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 35 | |||
| 36 | // user capabilities |
||
| 37 | add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 38 | add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 39 | add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 40 | |||
| 41 | // user authentication |
||
| 42 | add_action( 'wp_login', $callable, 10, 2 ); |
||
| 43 | add_action( 'wp_login_failed', $callable, 10, 2 ); |
||
| 44 | add_action( 'wp_logout', $callable, 10, 0 ); |
||
| 45 | add_action( 'wp_masterbar_logout', $callable, 10, 0 ); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function init_full_sync_listeners( $callable ) { |
||
| 49 | add_action( 'jetpack_full_sync_users', $callable ); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function init_before_send() { |
||
| 53 | add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) ); |
||
| 54 | add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 ); |
||
| 55 | add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); |
||
| 56 | |||
| 57 | // full sync |
||
| 58 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function sanitize_user_and_expand( $user ) { |
||
| 62 | $user = $this->sanitize_user( $user ); |
||
| 63 | |||
| 64 | return $this->add_to_user( $user ); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function sanitize_user( $user ) { |
||
| 68 | // this create a new user object and stops the passing of the object by reference. |
||
| 69 | $user = unserialize( serialize( $user ) ); |
||
| 70 | |||
| 71 | if ( is_object( $user ) && is_object( $user->data ) ) { |
||
| 72 | unset( $user->data->user_pass ); |
||
| 73 | } |
||
| 74 | |||
| 75 | return $user; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function add_to_user( $user ) { |
||
| 79 | $user->allowed_mime_types = get_allowed_mime_types( $user ); |
||
| 80 | |||
| 81 | if ( function_exists( 'get_user_locale' ) ) { |
||
| 82 | |||
| 83 | // Only set the user locale if it is different from the site local |
||
| 84 | if ( get_locale() !== get_user_locale( $user->ID ) ) { |
||
| 85 | $user->locale = get_user_locale( $user->ID ); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | return $user; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function expand_user( $args ) { |
||
| 100 | |||
| 101 | public function expand_login_username( $args ) { |
||
| 107 | |||
| 108 | public function expand_logout_username( $args, $user_id ) { |
||
| 118 | |||
| 119 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 149 | |||
| 150 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
| 162 | |||
| 163 | function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { |
||
| 164 | if ( $meta_key === 'locale' ) { |
||
| 165 | if ( current_filter() === 'deleted_user_meta' ) { |
||
| 166 | /** |
||
| 167 | * Allow listeners to listen for user local delete changes |
||
| 168 | * |
||
| 169 | * @since 4.8.0 |
||
| 170 | * |
||
| 171 | * @param int $user_id - The ID of the user whos locale is being deleted |
||
| 172 | */ |
||
| 173 | do_action( 'jetpack_sync_user_locale_delete', $user_id ); |
||
| 174 | } else { |
||
| 175 | /** |
||
| 176 | * Allow listeners to listen for user local changes |
||
| 177 | * |
||
| 178 | * @since 4.8.0 |
||
| 179 | * |
||
| 180 | * @param int $user_id - The ID of the user whos locale is being changed |
||
| 181 | * @param int $value - The value of the new locale |
||
| 182 | */ |
||
| 183 | do_action( 'jetpack_sync_user_locale', $user_id, $value ); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | $this->save_user_cap_handler( $meta_id, $user_id, $meta_key, $value ); |
||
| 187 | } |
||
| 188 | |||
| 189 | function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
||
| 212 | |||
| 213 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 217 | |||
| 218 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 219 | global $wpdb; |
||
| 220 | |||
| 221 | $query = "SELECT count(*) FROM $wpdb->usermeta"; |
||
| 222 | |||
| 223 | if ( $where_sql = $this->get_where_sql( $config ) ) { |
||
| 224 | $query .= ' WHERE ' . $where_sql; |
||
| 225 | } |
||
| 226 | |||
| 227 | $count = $wpdb->get_var( $query ); |
||
| 228 | |||
| 229 | return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
||
| 230 | } |
||
| 231 | |||
| 232 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 244 | |||
| 245 | function get_full_sync_actions() { |
||
| 248 | |||
| 249 | function get_initial_sync_user_config() { |
||
| 260 | |||
| 261 | public function expand_users( $args ) { |
||
| 266 | } |
||
| 267 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.