Completed
Push — update/full-sync ( 7cf495 )
by
unknown
06:38
created

Term_Relationships   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 189
Duplicated Lines 5.29 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 10
loc 189
rs 10
c 0
b 0
f 0
wmc 14
lcom 0
cbo 2

10 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A id_field() 0 3 1
A table_name() 0 3 1
A init_full_sync_listeners() 0 3 1
A init_before_send() 0 4 1
A enqueue_full_sync_actions() 0 50 4
A get_chunks_with_preceding_end() 10 10 2
A estimate_full_sync_actions() 0 10 1
A get_full_sync_actions() 0 3 1
A expand_term_relationships() 0 7 1

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
 * Term relationships sync module.
4
 *
5
 * @package automattic/jetpack-sync
6
 */
7
8
namespace Automattic\Jetpack\Sync\Modules;
9
10
use Automattic\Jetpack\Sync\Defaults;
11
use Automattic\Jetpack\Sync\Listener;
12
use Automattic\Jetpack\Sync\Settings;
13
14
/**
15
 * Class to handle sync for term relationships.
16
 */
17
class Term_Relationships extends Module {
18
19
	const ARRAY_CHUNK_SIZE = 10;
20
21
	/**
22
	 * Sync module name.
23
	 *
24
	 * @access public
25
	 *
26
	 * @return string
27
	 */
28
	public function name() {
29
		return 'term_relationships';
30
	}
31
32
	/**
33
	 * The id field in the database.
34
	 *
35
	 * @access public
36
	 *
37
	 * @return string
38
	 */
39
	public function id_field() {
40
		return 'object_id';
41
	}
42
43
	/**
44
	 * The table in the database.
45
	 *
46
	 * @access public
47
	 *
48
	 * @return string
49
	 */
50
	public function table_name() {
51
		return 'term_relationships';
52
	}
53
54
	/**
55
	 * Initialize term relationships action listeners for full sync.
56
	 *
57
	 * @access public
58
	 *
59
	 * @param callable $callable Action handler callable.
60
	 */
61
	public function init_full_sync_listeners( $callable ) {
62
		add_action( 'jetpack_full_sync_term_relationships', $callable, 10, 2 );
63
	}
64
65
	/**
66
	 * Initialize the module in the sender.
67
	 *
68
	 * @access public
69
	 */
70
	public function init_before_send() {
71
		// Full sync.
72
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_term_relationships', array( $this, 'expand_term_relationships' ) );
73
	}
74
75
	/**
76
	 * Enqueue the term relationships actions for full sync.
77
	 *
78
	 * @access public
79
	 *
80
	 * @todo This method has similarities with Automattic\Jetpack\Sync\Modules\Module::enqueue_all_ids_as_action. Refactor to keep DRY.
81
	 * @see Automattic\Jetpack\Sync\Modules\Module::enqueue_all_ids_as_action
82
	 *
83
	 * @param array $config               Full sync configuration for this sync module.
84
	 * @param int   $max_items_to_enqueue Maximum number of items to enqueue.
85
	 * @param int   $offset               How many items have been enqueued already
86
	 * @return array Number of actions enqueued, and next module state.
87
	 */
88
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $offset ) {
89
		l( 'enqueue_full_sync_actions' );
90
		l( func_get_args() );
91
		global $wpdb;
92
		$items_per_page = 1000;
93
		$chunk_count    = 0;
94
95
		$listener    = Listener::get_instance();
96
		$action_name = 'jetpack_full_sync_term_relationships';
97
98
		$offset = $offset ? $offset : 0;
99
100
		l( '$max_items_to_enqueue', $max_items_to_enqueue );
101
102
		l( 'WHILE', "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships ORDER BY term_taxonomy_id DESC, object_id DESC LIMIT $offset, $items_per_page" );
103
104
		// Count down from max_id to min_id so we get term relationships for the newest posts and terms first.
105
		// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
106
		while ( $ids = $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships ORDER BY term_taxonomy_id DESC, object_id DESC LIMIT $offset, $items_per_page", ARRAY_A ) ) {
107
108
			// Request term relationships in groups of N for efficiency.
109
			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
110
111
			// If we hit our row limit, process and return.
112
			if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) {
113
				l( 'HIT  LIMIT' );
114
				$remaining_items_count                      = $max_items_to_enqueue - $chunk_count;
115
				$remaining_items                            = array_slice( $chunked_ids, 0, $remaining_items_count );
116
				$remaining_items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $remaining_items, $offset );
117
				$listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items_with_previous_interval_end );
118
119
				$offset = $offset + ( count( $remaining_items ) * self::ARRAY_CHUNK_SIZE );
120
				l( '$offset', $offset );
121
				return array( $remaining_items_count + $chunk_count, $offset );
122
			}
123
			$chunked_ids_with_previous_end = $this->get_chunks_with_preceding_end( $chunked_ids, $offset );
124
125
			l( 'TERM RELATIONSHIPS' );
126
			l( $chunked_ids_with_previous_end );
127
128
			$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids_with_previous_end );
129
130
			$chunk_count += count( $chunked_ids );
131
132
			// The $ids are ordered in descending order.
133
			$offset += count( $ids );
134
		}
135
136
		return array( $chunk_count, true );
137
	}
138
139
	/**
140
	 * Retrieve chunk IDs with previous interval end.
141
	 *
142
	 * @access protected
143
	 *
144
	 * @param array $chunks                All remaining items.
145
	 * @param int   $previous_interval_end The last item from the previous interval.
0 ignored issues
show
Bug introduced by
There is no parameter named $previous_interval_end. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
146
	 * @return array Chunk IDs with the previous interval end.
147
	 */
148 View Code Duplication
	protected function get_chunks_with_preceding_end( $chunks, $offset ) {
149
		$chunks_with_ends = array();
150
		foreach ( $chunks as $chunk ) {
151
			$chunks_with_ends[] = array(
152
				'ids'    => $chunk,
153
				'offset' => $offset += count( $chunk ),
154
			);
155
		}
156
		return $chunks_with_ends;
157
	}
158
159
160
	/**
161
	 * Retrieve an estimated number of actions that will be enqueued.
162
	 *
163
	 * @access public
164
	 *
165
	 * @param array $config Full sync configuration for this sync module.
166
	 * @return int Number of items yet to be enqueued.
167
	 */
168
	public function estimate_full_sync_actions( $config ) {
169
		global $wpdb;
170
171
		$query = "SELECT COUNT(*) FROM $wpdb->term_relationships";
172
173
		// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
174
		$count = $wpdb->get_var( $query );
175
176
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
177
	}
178
179
	/**
180
	 * Retrieve the actions that will be sent for this module during a full sync.
181
	 *
182
	 * @access public
183
	 *
184
	 * @return array Full sync actions of this module.
185
	 */
186
	public function get_full_sync_actions() {
187
		return array( 'jetpack_full_sync_term_relationships' );
188
	}
189
190
	/**
191
	 * Expand the term relationships within a hook before they are serialized and sent to the server.
192
	 *
193
	 * @access public
194
	 *
195
	 * @param array $args The hook parameters.
196
	 * @return array $args The expanded hook parameters.
197
	 */
198
	public function expand_term_relationships( $args ) {
199
		list( $term_relationships ) = $args;
200
201
		return array(
202
			'term_relationships' => $term_relationships,
203
		);
204
	}
205
}
206