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 | const MAX_INITIAL_SYNC_USERS = 100; |
||
| 5 | |||
| 6 | function name() { |
||
| 9 | |||
| 10 | public function init_listeners( $callable ) { |
||
| 11 | // users |
||
| 12 | add_action( 'user_register', array( $this, 'save_user_handler' ) ); |
||
| 13 | add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); |
||
| 14 | add_action( 'add_user_to_blog', array( $this, 'save_user_handler' ) ); |
||
| 15 | add_action( 'jetpack_sync_save_user', $callable, 10, 2 ); |
||
| 16 | |||
| 17 | add_action( 'deleted_user', $callable, 10, 2 ); |
||
| 18 | add_action( 'remove_user_from_blog', $callable, 10, 2 ); |
||
| 19 | |||
| 20 | // user roles |
||
| 21 | add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 22 | add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
||
| 23 | add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 24 | |||
| 25 | // user capabilities |
||
| 26 | add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 27 | add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 28 | add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 29 | |||
| 30 | // user authentication |
||
| 31 | add_action( 'wp_login', $callable, 10, 2 ); |
||
| 32 | add_action( 'wp_login_failed', $callable, 10, 2 ); |
||
| 33 | add_action( 'wp_logout', $callable, 10, 0 ); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function init_full_sync_listeners( $callable ) { |
||
| 37 | add_action( 'jetpack_full_sync_users', $callable ); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function init_before_send() { |
||
| 48 | |||
| 49 | public function sanitize_user_and_expand( $user ) { |
||
| 50 | $user = $this->sanitize_user( $user ); |
||
| 51 | |||
| 52 | return $this->add_to_user( $user ); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function sanitize_user( $user ) { |
||
| 56 | // this create a new user object and stops the passing of the object by reference. |
||
| 57 | $user = unserialize( serialize( $user ) ); |
||
| 58 | unset( $user->data->user_pass ); |
||
| 59 | |||
| 60 | return $user; |
||
| 61 | } |
||
| 62 | |||
| 63 | public function add_to_user( $user ) { |
||
| 64 | $user->allowed_mime_types = get_allowed_mime_types( $user ); |
||
| 65 | |||
| 66 | return $user; |
||
| 67 | } |
||
| 68 | |||
| 69 | public function expand_user( $args ) { |
||
| 70 | list( $user ) = $args; |
||
| 71 | |||
| 72 | if ( $user ) { |
||
| 73 | return array( $this->add_to_user( $user ) ); |
||
| 74 | } |
||
| 75 | |||
| 76 | return false; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function expand_login_username( $args ) { |
||
| 80 | list( $login, $user ) = $args; |
||
| 81 | $user = $this->sanitize_user( $user ); |
||
| 82 | |||
| 83 | return array( $login, $user ); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function expand_logout_username( $args, $user_id ) { |
||
| 87 | $user = get_userdata( $user_id ); |
||
| 88 | $user = $this->sanitize_user( $user ); |
||
| 89 | $login = $user->data->user_login; |
||
| 90 | |||
| 91 | return array( $login, $user ); |
||
| 92 | } |
||
| 93 | |||
| 94 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 125 | |||
| 126 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
| 127 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Fires when the client needs to sync an updated user |
||
| 131 | * |
||
| 132 | * @since 4.2.0 |
||
| 133 | * |
||
| 138 | |||
| 139 | function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
||
| 163 | |||
| 164 | public function enqueue_full_sync_actions( $config ) { |
||
| 179 | |||
| 180 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 193 | |||
| 194 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 206 | |||
| 207 | function get_full_sync_actions() { |
||
| 210 | |||
| 211 | function get_initial_sync_user_config() { |
||
| 222 | |||
| 223 | public function expand_users( $args ) { |
||
| 235 | |||
| 236 | public function get_next_user_ids( $from ) { |
||
| 247 | } |
||
| 248 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.