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_WP_Replicastore 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_WP_Replicastore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Jetpack_Sync_WP_Replicastore implements iJetpack_Sync_Replicastore { |
||
| 10 | |||
| 11 | |||
| 12 | public function reset() { |
||
| 13 | global $wpdb; |
||
| 14 | |||
| 15 | $wpdb->query( "DELETE FROM $wpdb->posts" ); |
||
| 16 | $wpdb->query( "DELETE FROM $wpdb->comments" ); |
||
| 17 | |||
| 18 | // also need to delete terms from cache |
||
| 19 | $term_ids = $wpdb->get_col( "SELECT term_id FROM $wpdb->terms" ); |
||
| 20 | foreach ( $term_ids as $term_id ) { |
||
| 21 | wp_cache_delete( $term_id, 'terms' ); |
||
| 22 | } |
||
| 23 | |||
| 24 | $wpdb->query( "DELETE FROM $wpdb->terms" ); |
||
| 25 | |||
| 26 | $wpdb->query( "DELETE FROM $wpdb->term_taxonomy" ); |
||
| 27 | $wpdb->query( "DELETE FROM $wpdb->term_relationships" ); |
||
| 28 | |||
| 29 | // callables and constants |
||
| 30 | $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'jetpack_%'" ); |
||
| 31 | $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key NOT LIKE '\_%'" ); |
||
| 32 | } |
||
| 33 | |||
| 34 | function full_sync_start() { |
||
| 37 | |||
| 38 | function full_sync_end( $checksum ) { |
||
| 41 | |||
| 42 | View Code Duplication | public function post_count( $status = null, $min_id = null, $max_id = null ) { |
|
| 43 | global $wpdb; |
||
| 44 | |||
| 45 | $where = ""; |
||
|
|
|||
| 46 | |||
| 47 | if ( $status ) { |
||
| 48 | $where = "post_status = '" . esc_sql( $status ) . "'"; |
||
| 49 | } else { |
||
| 50 | $where = "1=1"; |
||
| 51 | } |
||
| 52 | |||
| 53 | if ( $min_id != null ) { |
||
| 54 | $where .= " AND ID >= " . intval( $min_id ); |
||
| 55 | } |
||
| 56 | |||
| 57 | if ( $max_id != null ) { |
||
| 58 | $where .= " AND ID <= " . intval( $max_id ); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE $where" ); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function get_posts( $status = null, $min_id = null, $max_id = null ) { |
||
| 65 | $args = array( 'orderby' => 'ID', 'posts_per_page' => -1 ); |
||
| 66 | |||
| 67 | if ( $status ) { |
||
| 68 | $args['post_status'] = $status; |
||
| 69 | } else { |
||
| 70 | $args['post_status'] = 'any'; |
||
| 71 | } |
||
| 72 | |||
| 73 | return get_posts( $args ); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function get_post( $id ) { |
||
| 79 | |||
| 80 | public function upsert_post( $post, $silent = false ) { |
||
| 81 | global $wpdb; |
||
| 82 | |||
| 83 | // reject the post if it's not a WP_Post |
||
| 84 | if ( ! $post instanceof WP_Post ) { |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | |||
| 88 | $post = $post->to_array(); |
||
| 89 | |||
| 90 | // reject posts without an ID |
||
| 91 | if ( ! isset( $post['ID'] ) ) { |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | |||
| 95 | $now = current_time( 'mysql' ); |
||
| 96 | $now_gmt = get_gmt_from_date( $now ); |
||
| 97 | |||
| 98 | $defaults = array( |
||
| 99 | 'ID' => 0, |
||
| 100 | 'post_author' => '0', |
||
| 101 | 'post_content' => '', |
||
| 102 | 'post_content_filtered' => '', |
||
| 103 | 'post_title' => '', |
||
| 104 | 'post_name' => '', |
||
| 105 | 'post_excerpt' => '', |
||
| 106 | 'post_status' => 'draft', |
||
| 107 | 'post_type' => 'post', |
||
| 108 | 'comment_status' => 'closed', |
||
| 109 | 'comment_count' => '0', |
||
| 110 | 'ping_status' => '', |
||
| 111 | 'post_password' => '', |
||
| 112 | 'to_ping' => '', |
||
| 113 | 'pinged' => '', |
||
| 114 | 'post_parent' => 0, |
||
| 115 | 'menu_order' => 0, |
||
| 116 | 'guid' => '', |
||
| 117 | 'post_date' => $now, |
||
| 118 | 'post_date_gmt' => $now_gmt, |
||
| 119 | 'post_modified' => $now, |
||
| 120 | 'post_modified_gmt' => $now_gmt, |
||
| 121 | ); |
||
| 122 | |||
| 123 | $post = array_intersect_key( $post, $defaults ); |
||
| 124 | |||
| 125 | $post = sanitize_post( $post, 'db' ); |
||
| 126 | |||
| 127 | unset( $post['filter'] ); |
||
| 128 | |||
| 129 | $exists = $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS( SELECT 1 FROM $wpdb->posts WHERE ID = %d )", $post['ID'] ) ); |
||
| 130 | |||
| 131 | if ( $exists ) { |
||
| 132 | $wpdb->update( $wpdb->posts, $post, array( 'ID' => $post['ID'] ) ); |
||
| 133 | } else { |
||
| 134 | $wpdb->insert( $wpdb->posts, $post ); |
||
| 135 | } |
||
| 136 | |||
| 137 | clean_post_cache( $post['ID'] ); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function delete_post( $post_id ) { |
||
| 141 | wp_delete_post( $post_id, true ); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function posts_checksum( $min_id = null, $max_id = null ) { |
||
| 145 | global $wpdb; |
||
| 146 | |||
| 147 | $where_sql = Jetpack_Sync_Defaults::get_blacklisted_post_types_sql(); |
||
| 148 | |||
| 149 | if ( $min_id !== null ) { |
||
| 150 | $min_id = intval( $min_id ); |
||
| 151 | $where_sql .= " AND ID >= $min_id"; |
||
| 152 | } |
||
| 153 | |||
| 154 | if ( $max_id !== null ) { |
||
| 155 | $max_id = intval( $max_id ); |
||
| 156 | $where_sql .= " AND ID <= $max_id"; |
||
| 157 | } |
||
| 158 | |||
| 159 | $query = <<<ENDSQL |
||
| 160 | SELECT CONV(BIT_XOR(CRC32(CONCAT(ID,post_modified))), 10, 16) |
||
| 161 | FROM $wpdb->posts |
||
| 162 | WHERE $where_sql |
||
| 163 | ENDSQL; |
||
| 164 | |||
| 165 | return $wpdb->get_var( $query ); |
||
| 166 | } |
||
| 167 | |||
| 168 | View Code Duplication | public function comment_count( $status = null, $min_id = null, $max_id = null ) { |
|
| 189 | |||
| 190 | private function comment_status_to_approval_value( $status ) { |
||
| 208 | |||
| 209 | public function get_comments( $status = null, $min_id = null, $max_id = null ) { |
||
| 210 | $args = array( 'orderby' => 'ID', 'status' => 'all' ); |
||
| 211 | |||
| 212 | if ( $status ) { |
||
| 213 | $args['status'] = $status; |
||
| 214 | } |
||
| 215 | |||
| 216 | return get_comments( $args ); |
||
| 217 | } |
||
| 218 | |||
| 219 | public function get_comment( $id ) { |
||
| 222 | |||
| 223 | public function upsert_comment( $comment ) { |
||
| 224 | global $wpdb, $wp_version; |
||
| 225 | |||
| 226 | if ( version_compare( $wp_version, '4.4', '<' ) ) { |
||
| 227 | $comment = (array) $comment; |
||
| 228 | } else { |
||
| 229 | // WP 4.4 introduced the WP_Comment Class |
||
| 230 | $comment = $comment->to_array(); |
||
| 231 | } |
||
| 232 | |||
| 233 | // filter by fields on comment table |
||
| 234 | $comment_fields_whitelist = array( |
||
| 235 | 'comment_ID', |
||
| 236 | 'comment_post_ID', |
||
| 237 | 'comment_author', |
||
| 238 | 'comment_author_email', |
||
| 239 | 'comment_author_url', |
||
| 240 | 'comment_author_IP', |
||
| 241 | 'comment_date', |
||
| 242 | 'comment_date_gmt', |
||
| 243 | 'comment_content', |
||
| 244 | 'comment_karma', |
||
| 245 | 'comment_approved', |
||
| 246 | 'comment_agent', |
||
| 247 | 'comment_type', |
||
| 248 | 'comment_parent', |
||
| 249 | 'user_id' |
||
| 250 | ); |
||
| 251 | |||
| 252 | foreach ( $comment as $key => $value ) { |
||
| 253 | if ( ! in_array( $key, $comment_fields_whitelist ) ) { |
||
| 254 | unset( $comment[ $key ] ); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | $exists = $wpdb->get_var( |
||
| 259 | $wpdb->prepare( |
||
| 260 | "SELECT EXISTS( SELECT 1 FROM $wpdb->comments WHERE comment_ID = %d )", |
||
| 261 | $comment['comment_ID'] |
||
| 262 | ) |
||
| 263 | ); |
||
| 264 | |||
| 265 | if ( $exists ) { |
||
| 266 | $wpdb->update( $wpdb->comments, $comment, array( 'comment_ID' => $comment['comment_ID'] ) ); |
||
| 267 | } else { |
||
| 268 | $wpdb->insert( $wpdb->comments, $comment ); |
||
| 269 | } |
||
| 270 | |||
| 271 | wp_update_comment_count( $comment['comment_post_ID'] ); |
||
| 272 | } |
||
| 273 | |||
| 274 | public function trash_comment( $comment_id ) { |
||
| 277 | |||
| 278 | public function delete_comment( $comment_id ) { |
||
| 281 | |||
| 282 | public function spam_comment( $comment_id ) { |
||
| 285 | |||
| 286 | public function comments_checksum( $min_id = null, $max_id = null ) { |
||
| 287 | global $wpdb; |
||
| 288 | |||
| 289 | $where_sql = "1=1"; |
||
| 290 | |||
| 291 | if ( $min_id !== null ) { |
||
| 292 | $min_id = intval( $min_id ); |
||
| 293 | $where_sql .= " AND comment_ID >= $min_id"; |
||
| 294 | } |
||
| 295 | |||
| 296 | if ( $max_id !== null ) { |
||
| 297 | $max_id = intval( $max_id ); |
||
| 298 | $where_sql .= " AND comment_ID <= $max_id"; |
||
| 299 | } |
||
| 300 | |||
| 301 | $query = <<<ENDSQL |
||
| 302 | SELECT CONV(BIT_XOR(CRC32(CONCAT(comment_ID,comment_content))), 10, 16) |
||
| 303 | FROM $wpdb->comments |
||
| 304 | WHERE $where_sql |
||
| 305 | ENDSQL; |
||
| 306 | |||
| 307 | return $wpdb->get_var( $query ); |
||
| 308 | } |
||
| 309 | |||
| 310 | public function options_checksum() { |
||
| 320 | |||
| 321 | |||
| 322 | public function update_option( $option, $value ) { |
||
| 325 | |||
| 326 | public function get_option( $option, $default = false ) { |
||
| 329 | |||
| 330 | public function delete_option( $option ) { |
||
| 333 | |||
| 334 | public function set_theme_support( $theme_support ) { |
||
| 337 | |||
| 338 | public function current_theme_supports( $feature ) { |
||
| 341 | |||
| 342 | public function get_metadata( $type, $object_id, $meta_key = '', $single = false ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * |
||
| 348 | * Stores remote meta key/values alongside an ID mapping key |
||
| 349 | * |
||
| 350 | * @param $type |
||
| 351 | * @param $object_id |
||
| 352 | * @param $meta_key |
||
| 353 | * @param $meta_value |
||
| 354 | * @param $meta_id |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | public function upsert_metadata( $type, $object_id, $meta_key, $meta_value, $meta_id ) { |
||
| 389 | |||
| 390 | public function delete_metadata( $type, $object_id, $meta_ids ) { |
||
| 407 | |||
| 408 | // constants |
||
| 409 | public function get_constant( $constant ) { |
||
| 418 | |||
| 419 | public function set_constant( $constant, $value ) { |
||
| 422 | |||
| 423 | public function get_updates( $type ) { |
||
| 432 | |||
| 433 | public function set_updates( $type, $updates ) { |
||
| 438 | |||
| 439 | // functions |
||
| 440 | public function get_callable( $name ) { |
||
| 449 | |||
| 450 | public function set_callable( $name, $value ) { |
||
| 453 | |||
| 454 | // network options |
||
| 455 | public function get_site_option( $option ) { |
||
| 458 | |||
| 459 | public function update_site_option( $option, $value ) { |
||
| 462 | |||
| 463 | public function delete_site_option( $option ) { |
||
| 466 | |||
| 467 | // terms |
||
| 468 | // terms |
||
| 469 | public function get_terms( $taxonomy ) { |
||
| 472 | |||
| 473 | public function get_term( $taxonomy, $term_id, $is_term_id = true ) { |
||
| 481 | |||
| 482 | private function ensure_taxonomy( $taxonomy ) { |
||
| 501 | |||
| 502 | public function get_the_terms( $object_id, $taxonomy ) { |
||
| 505 | |||
| 506 | public function update_term( $term_object ) { |
||
| 539 | |||
| 540 | public function delete_term( $term_id, $taxonomy ) { |
||
| 543 | |||
| 544 | public function update_object_terms( $object_id, $taxonomy, $terms, $append ) { |
||
| 547 | |||
| 548 | public function delete_object_terms( $object_id, $tt_ids ) { |
||
| 588 | |||
| 589 | // users |
||
| 590 | public function user_count() { |
||
| 593 | |||
| 594 | public function get_user( $user_id ) { |
||
| 597 | |||
| 598 | public function upsert_user( $user ) { |
||
| 601 | |||
| 602 | public function delete_user( $user_id ) { |
||
| 603 | $this->invalid_call(); |
||
| 604 | } |
||
| 605 | |||
| 606 | public function get_allowed_mime_types( $user_id ) { |
||
| 607 | |||
| 608 | } |
||
| 609 | |||
| 610 | public function checksum_all() { |
||
| 611 | return array( |
||
| 612 | 'posts' => $this->posts_checksum(), |
||
| 613 | 'comments' => $this->comments_checksum(), |
||
| 614 | 'options' => $this->options_checksum(), |
||
| 615 | ); |
||
| 616 | } |
||
| 617 | |||
| 618 | function checksum_histogram( $object_type, $buckets, $start_id = null, $end_id = null ) { |
||
| 619 | global $wpdb; |
||
| 620 | |||
| 621 | $wpdb->queries = array(); |
||
| 622 | |||
| 623 | switch( $object_type ) { |
||
| 624 | View Code Duplication | case "posts": |
|
| 625 | $object_count = $this->post_count( null, $start_id, $end_id ); |
||
| 626 | $object_table = $wpdb->posts; |
||
| 627 | $id_field = 'ID'; |
||
| 628 | $checksum_method = array( $this, 'posts_checksum' ); |
||
| 629 | break; |
||
| 630 | View Code Duplication | case "comments": |
|
| 631 | $object_count = $this->comment_count( null, $start_id, $end_id ); |
||
| 632 | $object_table = $wpdb->comments; |
||
| 633 | $id_field = 'comment_ID'; |
||
| 634 | $checksum_method = array( $this, 'comments_checksum' ); |
||
| 635 | break; |
||
| 636 | default: |
||
| 637 | return false; |
||
| 638 | } |
||
| 639 | |||
| 640 | $bucket_size = intval( ceil( $object_count / $buckets ) ); |
||
| 641 | $query_offset = 0; |
||
| 642 | $histogram = array(); |
||
| 643 | |||
| 644 | $where = "1=1"; |
||
| 645 | |||
| 646 | if ( $start_id ) { |
||
| 647 | $where .= " AND $id_field >= " . intval( $start_id ); |
||
| 648 | } |
||
| 649 | |||
| 650 | if ( $end_id ) { |
||
| 651 | $where .= " AND $id_field <= " . intval( $end_id ); |
||
| 652 | } |
||
| 653 | |||
| 654 | do { |
||
| 655 | list( $first_id, $last_id ) = $wpdb->get_row( |
||
| 656 | "SELECT MIN($id_field) as min_id, MAX($id_field) as max_id FROM ( SELECT $id_field FROM $object_table WHERE $where ORDER BY $id_field ASC LIMIT $query_offset, $bucket_size ) as ids", |
||
| 657 | ARRAY_N |
||
| 658 | ); |
||
| 659 | |||
| 660 | // get the checksum value |
||
| 661 | $value = call_user_func( $checksum_method, $first_id, $last_id ); |
||
| 662 | |||
| 663 | if ( $first_id === null || $last_id === null ) { |
||
| 664 | break; |
||
| 665 | } elseif ( $first_id === $last_id ) { |
||
| 666 | $histogram[$first_id] = $value; |
||
| 667 | } else { |
||
| 668 | $histogram["{$first_id}-{$last_id}"] = $value; |
||
| 669 | } |
||
| 670 | |||
| 671 | $query_offset += $bucket_size; |
||
| 672 | } while ( true ); |
||
| 673 | |||
| 674 | return $histogram; |
||
| 675 | } |
||
| 676 | |||
| 677 | private function invalid_call() { |
||
| 682 | } |
||
| 683 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.