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 | public function set_wp_version( $version ) { |
||
| 13 | |||
| 14 | public function get_wp_version() { |
||
| 19 | |||
| 20 | public function reset() { |
||
| 41 | |||
| 42 | function full_sync_start() { |
||
| 45 | |||
| 46 | function full_sync_end( $checksum ) { |
||
| 49 | |||
| 50 | public function post_count( $status = null ) { |
||
| 53 | |||
| 54 | public function get_posts( $status = null ) { |
||
| 65 | |||
| 66 | public function get_post( $id ) { |
||
| 69 | |||
| 70 | public function upsert_post( $post, $silent = false ) { |
||
| 71 | global $wpdb; |
||
| 72 | |||
| 73 | // reject the post if it's not a WP_Post |
||
| 74 | if ( ! $post instanceof WP_Post ) { |
||
|
|
|||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | $post = $post->to_array(); |
||
| 79 | |||
| 80 | // reject posts without an ID |
||
| 81 | if ( ! isset( $post['ID'] ) ) { |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | $now = current_time( 'mysql' ); |
||
| 86 | $now_gmt = get_gmt_from_date( $now ); |
||
| 87 | |||
| 88 | $defaults = array( |
||
| 89 | 'ID' => 0, |
||
| 90 | 'post_author' => '0', |
||
| 91 | 'post_content' => '', |
||
| 92 | 'post_content_filtered' => '', |
||
| 93 | 'post_title' => '', |
||
| 94 | 'post_name' => '', |
||
| 95 | 'post_excerpt' => '', |
||
| 96 | 'post_status' => 'draft', |
||
| 97 | 'post_type' => 'post', |
||
| 98 | 'comment_status' => 'closed', |
||
| 99 | 'comment_count' => '0', |
||
| 100 | 'ping_status' => '', |
||
| 101 | 'post_password' => '', |
||
| 102 | 'to_ping' => '', |
||
| 103 | 'pinged' => '', |
||
| 104 | 'post_parent' => 0, |
||
| 105 | 'menu_order' => 0, |
||
| 106 | 'guid' => '', |
||
| 107 | 'post_date' => $now, |
||
| 108 | 'post_date_gmt' => $now_gmt, |
||
| 109 | 'post_modified' => $now, |
||
| 110 | 'post_modified_gmt' => $now_gmt, |
||
| 111 | ); |
||
| 112 | |||
| 113 | $post = array_intersect_key( $post, $defaults ); |
||
| 114 | |||
| 115 | $post = sanitize_post( $post, 'db' ); |
||
| 116 | |||
| 117 | unset( $post['filter'] ); |
||
| 118 | |||
| 119 | $exists = $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS( SELECT 1 FROM $wpdb->posts WHERE ID = %d )", $post['ID'] ) ); |
||
| 120 | |||
| 121 | if ( $exists ) { |
||
| 122 | $wpdb->update( $wpdb->posts, $post, array( 'ID' => $post['ID'] ) ); |
||
| 123 | } else { |
||
| 124 | $wpdb->insert( $wpdb->posts, $post ); |
||
| 125 | } |
||
| 126 | |||
| 127 | clean_post_cache( $post['ID'] ); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function delete_post( $post_id ) { |
||
| 133 | |||
| 134 | public function posts_checksum() { |
||
| 147 | |||
| 148 | public function comment_count( $status = null ) { |
||
| 162 | |||
| 163 | private function comment_status_to_approval_value( $status ) { |
||
| 181 | |||
| 182 | public function get_comments( $status = null ) { |
||
| 191 | |||
| 192 | public function get_comment( $id ) { |
||
| 195 | |||
| 196 | public function upsert_comment( $comment ) { |
||
| 246 | |||
| 247 | public function trash_comment( $comment_id ) { |
||
| 250 | |||
| 251 | public function delete_comment( $comment_id ) { |
||
| 254 | |||
| 255 | public function spam_comment( $comment_id ) { |
||
| 258 | |||
| 259 | public function comments_checksum() { |
||
| 268 | |||
| 269 | public function update_option( $option, $value ) { |
||
| 272 | |||
| 273 | public function get_option( $option ) { |
||
| 276 | |||
| 277 | public function delete_option( $option ) { |
||
| 280 | |||
| 281 | public function set_theme_support( $theme_support ) { |
||
| 284 | |||
| 285 | public function current_theme_supports( $feature ) { |
||
| 288 | |||
| 289 | public function get_metadata( $type, $object_id, $meta_key = '', $single = false ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * |
||
| 295 | * Stores remote meta key/values alongside an ID mapping key |
||
| 296 | * |
||
| 297 | * @param $type |
||
| 298 | * @param $object_id |
||
| 299 | * @param $meta_key |
||
| 300 | * @param $meta_value |
||
| 301 | * @param $meta_id |
||
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | public function upsert_metadata( $type, $object_id, $meta_key, $meta_value, $meta_id ) { |
||
| 306 | |||
| 307 | $table = _get_meta_table( $type ); |
||
| 308 | if ( ! $table ) { |
||
| 309 | return false; |
||
| 310 | } |
||
| 311 | |||
| 312 | global $wpdb; |
||
| 313 | |||
| 314 | $exists = $wpdb->get_var( $wpdb->prepare( |
||
| 315 | "SELECT EXISTS( SELECT 1 FROM $table WHERE meta_id = %d )", |
||
| 316 | $meta_id |
||
| 317 | ) ); |
||
| 318 | |||
| 319 | if ( $exists ) { |
||
| 320 | $wpdb->update( $table, array( 'meta_key' => $meta_key, |
||
| 321 | 'meta_value' => serialize( $meta_value ) |
||
| 322 | ), array( 'meta_id' => $meta_id ) ); |
||
| 323 | } else { |
||
| 324 | $object_id_field = $type . '_id'; |
||
| 325 | $wpdb->insert( $table, array( 'meta_id' => $meta_id, |
||
| 326 | $object_id_field => $object_id, |
||
| 327 | 'meta_key' => $meta_key, |
||
| 328 | 'meta_value' => serialize( $meta_value ) |
||
| 329 | ) ); |
||
| 330 | } |
||
| 331 | |||
| 332 | wp_cache_delete( $object_id, $type . '_meta' ); |
||
| 333 | |||
| 334 | return true; |
||
| 335 | } |
||
| 336 | |||
| 337 | public function delete_metadata( $type, $object_id, $meta_ids ) { |
||
| 354 | |||
| 355 | // constants |
||
| 356 | public function get_constant( $constant ) { |
||
| 365 | |||
| 366 | public function set_constants( $constants ) { |
||
| 375 | |||
| 376 | public function get_updates( $type ) { |
||
| 385 | |||
| 386 | public function set_updates( $type, $updates ) { |
||
| 391 | |||
| 392 | // functions |
||
| 393 | public function get_callable( $name ) { |
||
| 394 | $value = get_option( 'jetpack_' . $name ); |
||
| 395 | |||
| 396 | if ( $value ) { |
||
| 397 | return $value; |
||
| 398 | } |
||
| 399 | |||
| 400 | return null; |
||
| 401 | } |
||
| 402 | |||
| 403 | public function set_callable( $name, $value ) { |
||
| 404 | update_option( 'jetpack_' . $name, $value ); |
||
| 405 | } |
||
| 406 | |||
| 407 | // network options |
||
| 408 | public function get_site_option( $option ) { |
||
| 411 | |||
| 412 | public function update_site_option( $option, $value ) { |
||
| 415 | |||
| 416 | public function delete_site_option( $option ) { |
||
| 419 | |||
| 420 | // terms |
||
| 421 | // terms |
||
| 422 | public function get_terms( $taxonomy ) { |
||
| 425 | |||
| 426 | public function get_term( $taxonomy, $term_id, $is_term_id = true ) { |
||
| 434 | |||
| 435 | private function ensure_taxonomy( $taxonomy ) { |
||
| 454 | |||
| 455 | public function get_the_terms( $object_id, $taxonomy ) { |
||
| 458 | |||
| 459 | public function update_term( $term_object ) { |
||
| 492 | |||
| 493 | public function delete_term( $term_id, $taxonomy ) { |
||
| 496 | |||
| 497 | public function update_object_terms( $object_id, $taxonomy, $terms, $append ) { |
||
| 500 | |||
| 501 | public function delete_object_terms( $object_id, $tt_ids ) { |
||
| 541 | |||
| 542 | // users |
||
| 543 | public function user_count() { |
||
| 546 | |||
| 547 | public function get_user( $user_id ) { |
||
| 550 | |||
| 551 | public function upsert_user( $user ) { |
||
| 554 | |||
| 555 | public function delete_user( $user_id ) { |
||
| 558 | |||
| 559 | public function checksum_all() { |
||
| 565 | |||
| 566 | private function invalid_call() { |
||
| 571 | } |
||
| 572 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.