Completed
Push — add/script-update-package-vers... ( 38150b...f5f944 )
by
unknown
06:18
created

Module   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 446
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 446
rs 9.1199
c 0
b 0
f 0
wmc 41
lcom 0
cbo 2

24 Methods

Rating   Name   Duplication   Size   Complexity  
name() 0 1 ?
A id_field() 0 3 1
A table_name() 0 3 1
A get_object_by_id() 0 3 1
A init_listeners() 0 2 1
A init_full_sync_listeners() 0 2 1
A init_before_send() 0 2 1
A set_defaults() 0 2 1
A reset_data() 0 2 1
A enqueue_full_sync_actions() 0 4 1
A estimate_full_sync_actions() 0 4 1
A get_full_sync_actions() 0 3 1
A count_actions() 0 3 1
A get_check_sum() 0 3 1
A still_valid_checksum() 0 7 3
B enqueue_all_ids_as_action() 0 46 6
A get_chunks_with_preceding_end() 0 12 2
A get_metadata() 0 21 2
A init_listeners_for_meta_type() 0 5 1
A init_meta_whitelist_handler() 0 5 1
A get_term_relationships() 0 6 1
A unserialize_meta() 0 4 1
A get_objects_by_id() 0 17 5
B get_min_max_object_ids_for_batches() 0 48 6

How to fix   Complexity   

Complex Class

Complex classes like Module 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 Module, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * A base abstraction of a sync module.
4
 *
5
 * @package automattic/jetpack-sync
6
 */
7
8
namespace Automattic\Jetpack\Sync\Modules;
9
10
use Automattic\Jetpack\Sync\Listener;
11
use Automattic\Jetpack\Sync\Replicastore;
12
13
/**
14
 * Basic methods implemented by Jetpack Sync extensions.
15
 *
16
 * @abstract
17
 */
18
abstract class Module {
19
	/**
20
	 * Number of items per chunk when grouping objects for performance reasons.
21
	 *
22
	 * @access public
23
	 *
24
	 * @var int
25
	 */
26
	const ARRAY_CHUNK_SIZE = 10;
27
28
	/**
29
	 * Sync module name.
30
	 *
31
	 * @access public
32
	 *
33
	 * @return string
34
	 */
35
	abstract public function name();
36
37
	/**
38
	 * The id field in the database.
39
	 *
40
	 * @access public
41
	 *
42
	 * @return string
43
	 */
44
	public function id_field() {
45
		return 'ID';
46
	}
47
48
	/**
49
	 * The table in the database.
50
	 *
51
	 * @access public
52
	 *
53
	 * @return string|bool
54
	 */
55
	public function table_name() {
56
		return false;
57
	}
58
59
	// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
60
61
	/**
62
	 * Retrieve a sync object by its ID.
63
	 *
64
	 * @access public
65
	 *
66
	 * @param string $object_type Type of the sync object.
67
	 * @param int    $id          ID of the sync object.
68
	 * @return mixed Object, or false if the object is invalid.
69
	 */
70
	public function get_object_by_id( $object_type, $id ) {
71
		return false;
72
	}
73
74
	/**
75
	 * Initialize callables action listeners.
76
	 * Override these to set up listeners and set/reset data/defaults.
77
	 *
78
	 * @access public
79
	 *
80
	 * @param callable $callable Action handler callable.
81
	 */
82
	public function init_listeners( $callable ) {
83
	}
84
85
	/**
86
	 * Initialize module action listeners for full sync.
87
	 *
88
	 * @access public
89
	 *
90
	 * @param callable $callable Action handler callable.
91
	 */
92
	public function init_full_sync_listeners( $callable ) {
93
	}
94
95
	/**
96
	 * Initialize the module in the sender.
97
	 *
98
	 * @access public
99
	 */
100
	public function init_before_send() {
101
	}
102
103
	/**
104
	 * Set module defaults.
105
	 *
106
	 * @access public
107
	 */
108
	public function set_defaults() {
109
	}
110
111
	/**
112
	 * Perform module cleanup.
113
	 * Usually triggered when uninstalling the plugin.
114
	 *
115
	 * @access public
116
	 */
117
	public function reset_data() {
118
	}
119
120
	/**
121
	 * Enqueue the module actions for full sync.
122
	 *
123
	 * @access public
124
	 *
125
	 * @param array   $config               Full sync configuration for this sync module.
126
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
127
	 * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
128
	 * @return array Number of actions enqueued, and next module state.
129
	 */
130
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
131
		// In subclasses, return the number of actions enqueued, and next module state (true == done).
132
		return array( null, true );
133
	}
134
135
	/**
136
	 * Retrieve an estimated number of actions that will be enqueued.
137
	 *
138
	 * @access public
139
	 *
140
	 * @param array $config Full sync configuration for this sync module.
141
	 * @return array Number of items yet to be enqueued.
142
	 */
143
	public function estimate_full_sync_actions( $config ) {
144
		// In subclasses, return the number of items yet to be enqueued.
145
		return null;
146
	}
147
148
	// phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
149
150
	/**
151
	 * Retrieve the actions that will be sent for this module during a full sync.
152
	 *
153
	 * @access public
154
	 *
155
	 * @return array Full sync actions of this module.
156
	 */
157
	public function get_full_sync_actions() {
158
		return array();
159
	}
160
161
	/**
162
	 * Get the number of actions that we care about.
163
	 *
164
	 * @access protected
165
	 *
166
	 * @param array $action_names     Action names we're interested in.
167
	 * @param array $actions_to_count Unfiltered list of actions we want to count.
168
	 * @return array Number of actions that we're interested in.
169
	 */
170
	protected function count_actions( $action_names, $actions_to_count ) {
171
		return count( array_intersect( $action_names, $actions_to_count ) );
172
	}
173
174
	/**
175
	 * Calculate the checksum of one or more values.
176
	 *
177
	 * @access protected
178
	 *
179
	 * @param mixed $values Values to calculate checksum for.
180
	 * @return int The checksum.
181
	 */
182
	protected function get_check_sum( $values ) {
183
		return crc32( wp_json_encode( jetpack_json_wrap( $values ) ) );
184
	}
185
186
	/**
187
	 * Whether a particular checksum in a set of checksums is valid.
188
	 *
189
	 * @access protected
190
	 *
191
	 * @param array  $sums_to_check Array of checksums.
192
	 * @param string $name          Name of the checksum.
193
	 * @param int    $new_sum       Checksum to compare against.
194
	 * @return boolean Whether the checksum is valid.
195
	 */
196
	protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) {
197
		if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) {
198
			return true;
199
		}
200
201
		return false;
202
	}
203
204
	/**
205
	 * Enqueue all items of a sync type as an action.
206
	 *
207
	 * @access protected
208
	 *
209
	 * @param string  $action_name          Name of the action.
210
	 * @param string  $table_name           Name of the database table.
211
	 * @param string  $id_field             Name of the ID field in the database.
212
	 * @param string  $where_sql            The SQL WHERE clause to filter to the desired items.
213
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue in the same time.
214
	 * @param boolean $state                Whether enqueueing has finished.
215
	 * @return array Array, containing the number of chunks and TRUE, indicating enqueueing has finished.
216
	 */
217
	protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) {
218
		global $wpdb;
219
220
		if ( ! $where_sql ) {
221
			$where_sql = '1 = 1';
222
		}
223
224
		$items_per_page        = 1000;
225
		$page                  = 1;
226
		$chunk_count           = 0;
227
		$previous_interval_end = $state ? $state : '~0';
228
		$listener              = Listener::get_instance();
229
230
		// Count down from max_id to min_id so we get newest posts/comments/etc first.
231
		// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
232
		while ( $ids = $wpdb->get_col( "SELECT {$id_field} FROM {$table_name} WHERE {$where_sql} AND {$id_field} < {$previous_interval_end} ORDER BY {$id_field} DESC LIMIT {$items_per_page}" ) ) {
233
			// Request posts in groups of N for efficiency.
234
			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
235
236
			// If we hit our row limit, process and return.
237
			if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) {
238
				$remaining_items_count                      = $max_items_to_enqueue - $chunk_count;
239
				$remaining_items                            = array_slice( $chunked_ids, 0, $remaining_items_count );
240
				$remaining_items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $remaining_items, $previous_interval_end );
241
				$listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items_with_previous_interval_end );
242
243
				$last_chunk = end( $remaining_items );
244
				return array( $remaining_items_count + $chunk_count, end( $last_chunk ) );
245
			}
246
			$chunked_ids_with_previous_end = $this->get_chunks_with_preceding_end( $chunked_ids, $previous_interval_end );
247
248
			$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids_with_previous_end );
249
250
			$chunk_count += count( $chunked_ids );
251
			$page++;
252
			// The $ids are ordered in descending order.
253
			$previous_interval_end = end( $ids );
254
		}
255
256
		if ( $wpdb->last_error ) {
257
			// return the values that were passed in so all these chunks get retried.
258
			return array( $max_items_to_enqueue, $state );
259
		}
260
261
		return array( $chunk_count, true );
262
	}
263
264
	/**
265
	 * Retrieve chunk IDs with previous interval end.
266
	 *
267
	 * @access protected
268
	 *
269
	 * @param array $chunks                All remaining items.
270
	 * @param int   $previous_interval_end The last item from the previous interval.
271
	 * @return array Chunk IDs with the previous interval end.
272
	 */
273
	protected function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) {
274
		$chunks_with_ends = array();
275
		foreach ( $chunks as $chunk ) {
276
			$chunks_with_ends[] = array(
277
				'ids'          => $chunk,
278
				'previous_end' => $previous_interval_end,
279
			);
280
			// Chunks are ordered in descending order.
281
			$previous_interval_end = end( $chunk );
282
		}
283
		return $chunks_with_ends;
284
	}
285
286
	/**
287
	 * Get metadata of a particular object type within the designated meta key whitelist.
288
	 *
289
	 * @access protected
290
	 *
291
	 * @todo Refactor to use $wpdb->prepare() on the SQL query.
292
	 *
293
	 * @param array  $ids                Object IDs.
294
	 * @param string $meta_type          Meta type.
295
	 * @param array  $meta_key_whitelist Meta key whitelist.
296
	 * @return array Unserialized meta values.
297
	 */
298
	protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) {
299
		global $wpdb;
300
		$table = _get_meta_table( $meta_type );
301
		$id    = $meta_type . '_id';
302
		if ( ! $table ) {
303
			return array();
304
		}
305
306
		$private_meta_whitelist_sql = "'" . implode( "','", array_map( 'esc_sql', $meta_key_whitelist ) ) . "'";
307
308
		return array_map(
309
			array( $this, 'unserialize_meta' ),
310
			$wpdb->get_results(
311
				// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
312
				"SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )' .
313
				" AND meta_key IN ( $private_meta_whitelist_sql ) ",
314
				// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
315
				OBJECT
316
			)
317
		);
318
	}
319
320
	/**
321
	 * Initialize listeners for the particular meta type.
322
	 *
323
	 * @access public
324
	 *
325
	 * @param string   $meta_type Meta type.
326
	 * @param callable $callable  Action handler callable.
327
	 */
328
	public function init_listeners_for_meta_type( $meta_type, $callable ) {
329
		add_action( "added_{$meta_type}_meta", $callable, 10, 4 );
330
		add_action( "updated_{$meta_type}_meta", $callable, 10, 4 );
331
		add_action( "deleted_{$meta_type}_meta", $callable, 10, 4 );
332
	}
333
334
	/**
335
	 * Initialize meta whitelist handler for the particular meta type.
336
	 *
337
	 * @access public
338
	 *
339
	 * @param string   $meta_type         Meta type.
340
	 * @param callable $whitelist_handler Action handler callable.
341
	 */
342
	public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) {
343
		add_filter( "jetpack_sync_before_enqueue_added_{$meta_type}_meta", $whitelist_handler );
344
		add_filter( "jetpack_sync_before_enqueue_updated_{$meta_type}_meta", $whitelist_handler );
345
		add_filter( "jetpack_sync_before_enqueue_deleted_{$meta_type}_meta", $whitelist_handler );
346
	}
347
348
	/**
349
	 * Retrieve the term relationships for the specified object IDs.
350
	 *
351
	 * @access protected
352
	 *
353
	 * @todo This feels too specific to be in the abstract sync Module class. Move it?
354
	 *
355
	 * @param array $ids Object IDs.
356
	 * @return array Term relationships - object ID and term taxonomy ID pairs.
357
	 */
358
	protected function get_term_relationships( $ids ) {
359
		global $wpdb;
360
361
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
362
		return $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )', OBJECT );
363
	}
364
365
	/**
366
	 * Unserialize the value of a meta object, if necessary.
367
	 *
368
	 * @access public
369
	 *
370
	 * @param object $meta Meta object.
371
	 * @return object Meta object with possibly unserialized value.
372
	 */
373
	public function unserialize_meta( $meta ) {
374
		$meta->meta_value = maybe_unserialize( $meta->meta_value );
375
		return $meta;
376
	}
377
378
	/**
379
	 * Retrieve a set of objects by their IDs.
380
	 *
381
	 * @access public
382
	 *
383
	 * @param string $object_type Object type.
384
	 * @param array  $ids         Object IDs.
385
	 * @return array Array of objects.
386
	 */
387
	public function get_objects_by_id( $object_type, $ids ) {
388
		if ( empty( $ids ) || empty( $object_type ) ) {
389
			return array();
390
		}
391
392
		$objects = array();
393
		foreach ( (array) $ids as $id ) {
394
			$object = $this->get_object_by_id( $object_type, $id );
395
396
			// Only add object if we have the object.
397
			if ( $object ) {
398
				$objects[ $id ] = $object;
399
			}
400
		}
401
402
		return $objects;
403
	}
404
405
	/**
406
	 * Gets a list of minimum and maximum object ids for each batch based on the given batch size.
407
	 *
408
	 * @access public
409
	 *
410
	 * @param int         $batch_size The batch size for objects.
411
	 * @param string|bool $where_sql  The sql where clause minus 'WHERE', or false if no where clause is needed.
412
	 *
413
	 * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found.
414
	 */
415
	public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) {
416
		global $wpdb;
417
418
		if ( ! $this->table_name() ) {
419
			return false;
420
		}
421
422
		$results      = array();
423
		$table        = $wpdb->{$this->table_name()};
424
		$current_max  = 0;
425
		$current_min  = 1;
426
		$id_field     = $this->id_field();
427
		$replicastore = new Replicastore();
428
429
		$total = $replicastore->get_min_max_object_id(
430
			$id_field,
431
			$table,
432
			$where_sql,
0 ignored issues
show
Bug introduced by
It seems like $where_sql defined by parameter $where_sql on line 415 can also be of type boolean; however, Automattic\Jetpack\Sync\...get_min_max_object_id() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
433
			false
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
434
		);
435
436
		while ( $total->max > $current_max ) {
437
			$where  = $where_sql ?
438
				$where_sql . " AND $id_field > $current_max" :
439
				"$id_field > $current_max";
440
			$result = $replicastore->get_min_max_object_id(
441
				$id_field,
442
				$table,
443
				$where,
444
				$batch_size
445
			);
446
			if ( empty( $result->min ) && empty( $result->max ) ) {
447
				// Our query produced no min and max. We can assume the min from the previous query,
448
				// and the total max we found in the initial query.
449
				$current_max = (int) $total->max;
450
				$result      = (object) array(
451
					'min' => $current_min,
452
					'max' => $current_max,
453
				);
454
			} else {
455
				$current_min = (int) $result->min;
456
				$current_max = (int) $result->max;
457
			}
458
			$results[] = $result;
459
		}
460
461
		return $results;
462
	}
463
}
464