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 | } |
||
| 46 | |||
| 47 | public function init_full_sync_listeners( $callable ) { |
||
| 48 | add_action( 'jetpack_full_sync_users', $callable ); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function init_before_send() { |
||
| 52 | add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) ); |
||
| 53 | add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 ); |
||
| 54 | add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); |
||
| 55 | |||
| 56 | // full sync |
||
| 57 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function sanitize_user_and_expand( $user ) { |
||
| 61 | $user = $this->sanitize_user( $user ); |
||
| 62 | |||
| 63 | return $this->add_to_user( $user ); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function sanitize_user( $user ) { |
||
| 67 | // this create a new user object and stops the passing of the object by reference. |
||
| 68 | $user = unserialize( serialize( $user ) ); |
||
| 69 | |||
| 70 | if ( is_object( $user ) && is_object( $user->data ) ) { |
||
| 71 | unset( $user->data->user_pass ); |
||
| 72 | } |
||
| 73 | |||
| 74 | return $user; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function add_to_user( $user ) { |
||
| 78 | $user->allowed_mime_types = get_allowed_mime_types( $user ); |
||
| 79 | |||
| 80 | if ( function_exists( 'get_user_locale' ) ) { |
||
| 81 | |||
| 82 | // Only set the user locale if it is different from the site local |
||
| 83 | if ( get_locale() !== get_user_locale( $user->ID ) ) { |
||
| 84 | $user->locale = get_user_locale( $user->ID ); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | return $user; |
||
| 88 | } |
||
| 89 | |||
| 90 | public function expand_user( $args ) { |
||
| 99 | |||
| 100 | public function expand_login_username( $args ) { |
||
| 106 | |||
| 107 | public function expand_logout_username( $args, $user_id ) { |
||
| 117 | |||
| 118 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 148 | |||
| 149 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
| 161 | |||
| 162 | function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { |
||
| 163 | if ( $meta_key === 'locale' ) { |
||
| 164 | if ( current_filter() === 'deleted_user_meta' ) { |
||
| 165 | /** |
||
| 166 | * Allow listeners to listen for user local delete changes |
||
| 167 | * |
||
| 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.