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:
| 1 | <?php |
||
| 3 | class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { |
||
| 4 | function name() { |
||
| 7 | |||
| 8 | public function init_listeners( $callable ) { |
||
| 9 | // users |
||
| 10 | add_action( 'user_register', array( $this, 'save_user_handler' ) ); |
||
| 11 | add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); |
||
| 12 | add_action( 'add_user_to_blog', array( $this, 'save_user_handler' ) ); |
||
| 13 | add_action( 'jetpack_sync_save_user', $callable, 10, 2 ); |
||
| 14 | |||
| 15 | add_action( 'deleted_user', $callable, 10, 2 ); |
||
| 16 | add_action( 'remove_user_from_blog', $callable, 10, 2 ); |
||
| 17 | |||
| 18 | // user roles |
||
| 19 | add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 20 | add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
||
| 21 | add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 22 | |||
| 23 | // user capabilities |
||
| 24 | add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 25 | add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 26 | add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 27 | |||
| 28 | // user authentication |
||
| 29 | add_action( 'wp_login', $callable, 10, 2 ); |
||
| 30 | add_action( 'wp_login_failed', $callable, 10, 2 ); |
||
| 31 | add_action( 'wp_logout', $callable, 10, 0 ); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function init_full_sync_listeners( $callable ) { |
||
| 35 | add_action( 'jetpack_full_sync_users', $callable ); |
||
| 36 | } |
||
| 37 | |||
| 38 | public function init_before_send() { |
||
| 39 | add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) ); |
||
| 40 | add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 2 ); |
||
| 41 | add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); |
||
| 42 | |||
| 43 | // full sync |
||
| 44 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function sanitize_user_and_expand( $user ) { |
||
| 48 | $user = $this->sanitize_user( $user ); |
||
| 49 | |||
| 50 | return $this->add_to_user( $user ); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function sanitize_user( $user ) { |
||
| 54 | unset( $user->data->user_pass ); |
||
| 55 | |||
| 56 | return $user; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function add_to_user( $user ) { |
||
| 60 | $user->allowed_mime_types = get_allowed_mime_types( $user ); |
||
| 61 | |||
| 62 | return $user; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function expand_user( $args ) { |
||
| 66 | list( $user ) = $args; |
||
| 67 | |||
| 68 | return array( $this->add_to_user( $user ) ); |
||
| 69 | } |
||
| 70 | |||
| 71 | public function expand_login_username( $args ) { |
||
| 72 | list( $login, $user ) = $args; |
||
| 73 | $user = $this->sanitize_user( $user ); |
||
| 74 | |||
| 75 | return array( $login, $user ); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function expand_logout_username( $args, $user_id ) { |
||
| 79 | $user = get_userdata( $user_id ); |
||
| 80 | $user = $this->sanitize_user( $user ); |
||
| 81 | $login = $user->data->user_login; |
||
| 82 | |||
| 83 | return array( $login, $user ); |
||
| 84 | } |
||
| 85 | |||
| 86 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 117 | |||
| 118 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
| 119 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Fires when the client needs to sync an updated user |
||
| 123 | * |
||
| 124 | * @since 4.2.0 |
||
| 125 | * |
||
| 126 | * @param object The WP_User object |
||
| 130 | |||
| 131 | function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
||
| 155 | |||
| 156 | public function enqueue_full_sync_actions( $config ) { |
||
| 157 | global $wpdb; |
||
| 158 | return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->users, 'ID', $this->get_where_sql( $config ) ); |
||
| 159 | } |
||
| 160 | |||
| 161 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 174 | |||
| 175 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 183 | |||
| 184 | function get_full_sync_actions() { |
||
| 187 | |||
| 188 | public function expand_users( $args ) { |
||
| 193 | } |
||
| 194 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.