Completed
Push — add/sync-rest-2 ( 88d6a7...6a7bf2 )
by
unknown
10:06
created

Jetpack_Sync_Full   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 55.56 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 6
c 4
b 1
f 1
lcom 1
cbo 1
dl 25
loc 45
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 7 1
A enqueue_all_constants() 0 4 1
A enqueue_all_posts() 14 15 2
A enqueue_all_comments() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/**
4
 * This class does a full resync of the database by 
5
 * enqueuing an outbound action for every single object
6
 * that we care about.
7
 * 
8
 * This class contains a few non-obvious optimisations that should be explained:
9
 * - we fire an action called jp_full_sync_start so that WPCOM can erase the contents of the cached database
10
 * - for each object type, we obtain a full list of object IDs to sync via a single API call (hoping that since they're ints, they can all fit in RAM)
11
 * - we load the full objects for those IDs in chunks of Jetpack_Sync_Full::$array_chunk_size (to reduce the number of MySQL calls)
12
 * - we fire a trigger for the entire array which the Jetpack_Sync_Client then serializes and queues.
13
 */
14
15
class Jetpack_Sync_Full {
16
	static $array_chunk_size = 5;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $array_chunk_size.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
17
18
	function start() {
19
		// TODO
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
20
		do_action( 'jp_full_sync_start' );
21
		$this->enqueue_all_constants();
22
		$this->enqueue_all_posts();
23
		$this->enqueue_all_comments();
24
	}
25
26
	private function enqueue_all_constants() {
27
		$client = Jetpack_Sync_Client::getInstance();
28
		$client->force_sync_constants();
29
	}
30
31 View Code Duplication
	private function enqueue_all_posts() {
32
		global $wpdb;
33
34
		// I hope this is never bigger than RAM...
35
		$post_ids = $wpdb->get_col( "SELECT id FROM $wpdb->posts");
36
37
		// Request posts in groups of N for efficiency
38
		$chunked_post_ids = array_chunk( $post_ids, self::$array_chunk_size );
39
40
		// Send each chunk as an array of objects
41
		foreach ( $chunked_post_ids as $chunk ) {
42
			$posts = get_posts( array( 'post__in' => $chunk, 'post_status' => 'any' ) );
43
			do_action( 'jp_full_sync_posts', $posts );
44
		}
45
	}
46
47 View Code Duplication
	private function enqueue_all_comments() {
48
		global $wpdb;
49
50
		$comment_ids = $wpdb->get_col( "SELECT comment_id FROM $wpdb->comments");
51
		$chunked_comment_ids = array_chunk( $comment_ids, self::$array_chunk_size );
52
53
		foreach ( $chunked_comment_ids as $chunk ) {
54
			$comments = get_comments( array( 'comment__in' => $chunk ) );
55
			do_action( 'jp_full_sync_comments', $comments );
56
		}
57
	}
58
	
59
}