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_Comments extends Jetpack_Sync_Module { |
||
| 4 | |||
| 5 | public function name() { |
||
| 8 | |||
| 9 | public function init_listeners( $callable ) { |
||
| 28 | |||
| 29 | public function init_before_send() { |
||
| 30 | add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) ); |
||
| 31 | |||
| 32 | View Code Duplication | foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) { |
|
| 33 | foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { |
||
| 34 | $comment_action_name = "comment_{$comment_status}_{$comment_type}"; |
||
| 35 | add_filter( 'jetpack_sync_before_send_' . $comment_action_name, array( $this, 'expand_wp_insert_comment' ) ); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | // full sync |
||
| 40 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_comments', array( $this, 'expand_comment_ids' ) ); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function enqueue_full_sync_actions() { |
||
| 44 | global $wpdb; |
||
| 45 | return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_comments', $wpdb->comments, 'comment_ID', null ); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function get_full_sync_actions() { |
||
| 49 | return array( 'jetpack_full_sync_comments' ); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function count_full_sync_actions( $action_names ) { |
||
| 53 | return $this->count_actions( $action_names, array( 'jetpack_full_sync_comments' ) ); |
||
| 54 | } |
||
| 55 | |||
| 56 | function expand_wp_comment_status_change( $args ) { |
||
| 59 | |||
| 60 | function expand_wp_insert_comment( $args ) { |
||
| 63 | |||
| 64 | function filter_comment( $comment ) { |
||
| 89 | |||
| 90 | public function expand_comment_ids( $args ) { |
||
| 91 | $comment_ids = $args[0]; |
||
| 92 | $comments = get_comments( array( |
||
| 93 | 'include_unapproved' => true, |
||
| 102 | } |
||
| 103 |