Completed
Push — dependabot/npm_and_yarn/ini-1.... ( bb632f...6a445d )
by Yaroslav
100:51 queued 92:02
created

Jetpack_JSON_API_Sync_Histogram_Endpoint   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B result() 0 37 6
1
<?php
2
3
use Automattic\Jetpack\Sync\Actions;
4
use Automattic\Jetpack\Sync\Modules;
5
use Automattic\Jetpack\Sync\Queue;
6
use Automattic\Jetpack\Sync\Queue_Buffer;
7
use Automattic\Jetpack\Sync\Replicastore;
8
use Automattic\Jetpack\Sync\Sender;
9
use Automattic\Jetpack\Sync\Settings;
10
11
// POST /sites/%s/sync
12
class Jetpack_JSON_API_Sync_Endpoint extends Jetpack_JSON_API_Endpoint {
13
14
	/**
15
	 * This endpoint allows authentication both via a blog and a user token.
16
	 * If a user token is used, that user should have `manage_options` capability.
17
	 *
18
	 * @var array|string
19
	 */
20
	protected $needed_capabilities = 'manage_options';
21
22
	protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) {
23
		return parent::validate_call( $_blog_id, $capability, false );
24
	}
25
26
	protected function result() {
27
		$args = $this->input();
28
		$modules = null;
29
30
		// convert list of modules in comma-delimited format into an array
31
		// of "$modulename => true"
32
		if ( isset( $args['modules'] ) && ! empty( $args['modules'] ) ) {
33
			$modules = array_map( '__return_true', array_flip( array_map( 'trim', explode( ',', $args['modules'] ) ) ) );
34
		}
35
36 View Code Duplication
		foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) {
37
			if ( 'users' === $module_name && isset( $args[ $module_name ] ) && 'initial' === $args[ $module_name ] ) {
38
				$modules[ 'users' ] = 'initial';
39
			} elseif ( isset( $args[ $module_name ] ) ) {
40
				$ids = explode( ',', $args[ $module_name ] );
41
				if ( count( $ids ) > 0 ) {
42
					$modules[ $module_name ] = $ids;
43
				}
44
			}
45
		}
46
47
		if ( empty( $modules ) ) {
48
			$modules = null;
49
		}
50
		return array( 'scheduled' => Actions::do_full_sync( $modules ) );
51
	}
52
53
	protected function validate_queue( $query ) {
54
		if ( ! isset( $query ) ) {
55
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_queue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
56
		}
57
58 View Code Duplication
		if ( ! in_array( $query, array( 'sync', 'full_sync', 'immediate' ) ) ) {
59
			return new WP_Error( 'invalid_queue', 'Queue name should be sync, full_sync or immediate', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_queue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
		}
61
		return $query;
62
	}
63
}
64
65
// GET /sites/%s/sync/status
66
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
67
	protected function result() {
68
		$args   = $this->query_args();
69
		$fields = isset( $args['fields'] ) ? $args['fields'] : array();
70
		return Actions::get_sync_status( $fields );
71
	}
72
}
73
74
// GET /sites/%s/data-check
75
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
76
	protected function result() {
77
		Actions::mark_sync_read_only();
78
		$store = new Replicastore();
79
		return $store->checksum_all();
80
	}
81
}
82
83
// GET /sites/%s/data-histogram
84
class Jetpack_JSON_API_Sync_Histogram_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
85
	protected function result() {
86
		$args = $this->query_args();
87
88
		if ( isset( $args['columns'] ) ) {
89
			$columns = array_map( 'trim', explode( ',', $args['columns'] ) );
90
		} else {
91
			$columns = null; // go with defaults
92
		}
93
94
		$store = new Replicastore();
95
96
		if ( ! isset( $args['strip_non_ascii'] ) ) {
97
			$args['strip_non_ascii'] = true;
98
		}
99
100
		/**
101
		 * Hack: nullify the values of `start_id` and `end_id` if we're only requesting ranges.
102
		 *
103
		 * The endpoint doesn't support nullable values :(
104
		 */
105
		if ( true === $args['only_range_edges'] ) {
106
			if ( 0 === $args['start_id'] ) {
107
				$args['start_id'] = null;
108
			}
109
110
			if ( 0 === $args['end_id'] ) {
111
				$args['end_id'] = null;
112
			}
113
		}
114
115
		$histogram = $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns, $args['strip_non_ascii'], $args['shared_salt'], $args['only_range_edges'], $args['detailed_drilldown'] );
0 ignored issues
show
Bug introduced by
It seems like $columns defined by array_map('trim', explode(',', $args['columns'])) on line 89 can also be of type array; however, Automattic\Jetpack\Sync\...e::checksum_histogram() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
116
117
		// Hack to disable Sync during this call, so we can resolve faster.
118
		Actions::mark_sync_read_only();
119
120
		return array( 'histogram' => $histogram, 'type' => $store->get_checksum_type() );
121
	}
122
}
123
124
// POST /sites/%s/sync/settings
125
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
126
	protected function result() {
127
		$args = $this->input();
128
129
		$sync_settings = Settings::get_settings();
130
131
		foreach ( $args as $key => $value ) {
132
			if ( $value !== false ) {
133
				if ( is_numeric( $value ) ) {
134
					$value = (int) $value;
135
				}
136
137
				// special case for sending empty arrays - a string with value 'empty'
138
				if ( $value === 'empty' ) {
139
					$value = array();
140
				}
141
142
				$sync_settings[ $key ] = $value;
143
			}
144
		}
145
146
		Settings::update_settings( $sync_settings );
147
148
		// re-fetch so we see what's really being stored
149
		return Settings::get_settings();
150
	}
151
}
152
153
// GET /sites/%s/sync/settings
154
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
155
	protected function result() {
156
157
		return Settings::get_settings();
158
	}
159
}
160
161
// GET /sites/%s/sync/object
162
class Jetpack_JSON_API_Sync_Object extends Jetpack_JSON_API_Sync_Endpoint {
163
	protected function result() {
164
		$args = $this->query_args();
165
166
		$module_name = $args['module_name'];
167
168
		if ( ! $sync_module = Modules::get_module( $module_name ) ) {
169
			return new WP_Error( 'invalid_module', 'You specified an invalid sync module' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_module'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
170
		}
171
172
		$object_type = $args['object_type'];
173
		$object_ids  = $args['object_ids'];
174
175
		$codec = Sender::get_instance()->get_codec();
176
177
		Actions::mark_sync_read_only();
178
		Settings::set_is_syncing( true );
179
		$objects = $codec->encode( $sync_module->get_objects_by_id( $object_type, $object_ids ) );
180
		Settings::set_is_syncing( false );
181
182
		return array(
183
			'objects' => $objects,
184
			'codec' => $codec->name(),
185
		);
186
	}
187
}
188
189
class Jetpack_JSON_API_Sync_Now_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
190
	protected function result() {
191
		$args = $this->input();
192
		$queue_name = $this->validate_queue( $args['queue'] );
193
194
		if ( is_wp_error( $queue_name ) ){
195
			return $queue_name;
196
		}
197
198
		$sender = Sender::get_instance();
199
		$response = $sender->do_sync_for_queue( new Queue( $args['queue'] ) );
0 ignored issues
show
Documentation introduced by
new \Automattic\Jetpack\...c\Queue($args['queue']) is of type object<Automattic\Jetpack\Sync\Queue>, but the function expects a object<Automattic\Jetpac...tic\Jetpack\Sync\Queue>.

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...
200
201
		return array(
202
			'response' => $response
203
		);
204
	}
205
}
206
207
class Jetpack_JSON_API_Sync_Checkout_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
208
	protected function result() {
209
		$args       = $this->input();
210
		$queue_name = $this->validate_queue( $args['queue'] );
211
212
		if ( is_wp_error( $queue_name ) ) {
213
			return $queue_name;
214
		}
215
216
		if ( $args['number_of_items'] < 1 || $args['number_of_items'] > 100 ) {
217
			return new WP_Error( 'invalid_number_of_items', 'Number of items needs to be an integer that is larger than 0 and less then 100', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_number_of_items'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
218
		}
219
220
		$number_of_items = absint( $args['number_of_items'] );
221
222
		if ( 'immediate' === $queue_name ) {
223
			return $this->immediate_full_sync_pull( $number_of_items );
224
		}
225
226
		return $this->queue_pull( $queue_name, $number_of_items, $args );
227
	}
228
229
	function queue_pull( $queue_name, $number_of_items, $args ){
230
		$queue = new Queue( $queue_name );
231
232
		if ( 0 === $queue->size() ) {
233
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'queue_size'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
234
		}
235
236
		$sender = Sender::get_instance();
237
238
		// try to give ourselves as much time as possible.
239
		set_time_limit( 0 );
240
241
		if ( $args['pop'] ) {
242
			$buffer = new Queue_Buffer( 'pop', $queue->pop( $number_of_items ) );
0 ignored issues
show
Bug introduced by
It seems like $queue->pop($number_of_items) targeting Automattic\Jetpack\Sync\Queue::pop() can also be of type null or object; however, Automattic\Jetpack\Sync\...e_Buffer::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that 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...
243
		} else {
244
			// let's delete the checkin state.
245
			if ( $args['force'] ) {
246
				$queue->unlock();
247
			}
248
			$buffer = $this->get_buffer( $queue, $number_of_items );
249
		}
250
		// Check that the $buffer is not checkout out already.
251
		if ( is_wp_error( $buffer ) ) {
252
			return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'buffer_open'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
253
		}
254
255
		if ( ! is_object( $buffer ) ) {
256
			return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'buffer_non-object'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
257
		}
258
259
		Settings::set_is_syncing( true );
260
		list( $items_to_send, $skipped_items_ids ) = $sender->get_items_to_send( $buffer, $args['encode'] );
261
		Settings::set_is_syncing( false );
262
263
		return array(
264
			'buffer_id'      => $buffer->id,
265
			'items'          => $items_to_send,
266
			'skipped_items'  => $skipped_items_ids,
267
			'codec'          => $args['encode'] ? $sender->get_codec()->name() : null,
268
			'sent_timestamp' => time(),
269
		);
270
	}
271
272
	public $items = [];
273
274
	public function jetpack_sync_send_data_listener() {
275
		foreach ( func_get_args()[0] as $key => $item ) {
276
			$this->items[ $key ] = $item;
277
		}
278
	}
279
280
	/**
281
	 * Check out a buffer of full sync actions.
282
	 *
283
	 * @param null $number_of_items Number of Actions to check-out.
284
	 *
285
	 * @return array Sync Actions to be returned to requestor
286
	 */
287
	public function immediate_full_sync_pull( $number_of_items = null ) {
288
		// try to give ourselves as much time as possible.
289
		set_time_limit( 0 );
290
291
		$original_send_data_cb = array( 'Automattic\Jetpack\Sync\Actions', 'send_data' );
292
		$temp_send_data_cb     = array( $this, 'jetpack_sync_send_data_listener' );
293
294
		Sender::get_instance()->set_enqueue_wait_time( 0 );
295
		remove_filter( 'jetpack_sync_send_data', $original_send_data_cb );
296
		add_filter( 'jetpack_sync_send_data', $temp_send_data_cb, 10, 6 );
297
		Sender::get_instance()->do_full_sync();
298
		remove_filter( 'jetpack_sync_send_data', $temp_send_data_cb );
299
		add_filter( 'jetpack_sync_send_data', $original_send_data_cb, 10, 6 );
300
301
		return array(
302
			'items'          => $this->items,
303
			'codec'          => Sender::get_instance()->get_codec()->name(),
304
			'sent_timestamp' => time(),
305
			'status'         => Actions::get_sync_status(),
306
		);
307
	}
308
309
	protected function get_buffer( $queue, $number_of_items ) {
310
		$start = time();
311
		$max_duration = 5; // this will try to get the buffer
312
313
		$buffer = $queue->checkout( $number_of_items );
314
		$duration = time() - $start;
315
316
		while( is_wp_error( $buffer ) && $duration < $max_duration ) {
317
			sleep( 2 );
318
			$duration = time() - $start;
319
			$buffer = $queue->checkout( $number_of_items );
320
		}
321
322
		if ( $buffer === false ) {
323
			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'queue_size'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
324
		}
325
326
		return $buffer;
327
	}
328
}
329
330
class Jetpack_JSON_API_Sync_Close_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
331
	protected function result() {
332
333
		$request_body = $this->input();
334
		$queue_name = $this->validate_queue( $request_body['queue'] );
335
336
		if ( is_wp_error( $queue_name ) ) {
337
			return $queue_name;
338
		}
339
340
		if ( ! isset( $request_body['buffer_id'] ) ) {
341
			return new WP_Error( 'missing_buffer_id', 'Please provide a buffer id', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'missing_buffer_id'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
342
		}
343
344
		if ( ! isset( $request_body['item_ids'] ) || ! is_array( $request_body['item_ids'] ) ) {
345
			return new WP_Error( 'missing_item_ids', 'Please provide a list of item ids in the item_ids argument', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'missing_item_ids'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
346
		}
347
348
		//Limit to A-Z,a-z,0-9,_,-
349
		$request_body ['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] );
350
		$request_body['item_ids'] = array_filter( array_map( array( 'Jetpack_JSON_API_Sync_Close_Endpoint', 'sanitize_item_ids' ), $request_body['item_ids'] ) );
351
352
		$queue = new Queue( $queue_name );
353
354
		$items = $queue->peek_by_id( $request_body['item_ids'] );
355
356
		// Update Full Sync Status if queue is "full_sync".
357
		if ( 'full_sync' === $queue_name ) {
358
			$full_sync_module = Modules::get_module( 'full-sync' );
359
360
			$full_sync_module->update_sent_progress_action( $items );
361
		}
362
363
		$buffer = new Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] );
364
		$response = $queue->close( $buffer, $request_body['item_ids'] );
0 ignored issues
show
Documentation introduced by
$buffer is of type object<Automattic\Jetpack\Sync\Queue_Buffer>, but the function expects a object<Automattic\Jetpac...pack\Sync\Queue_Buffer>.

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...
365
366
		// Perform another checkout?
367
		if ( isset( $request_body['continue'] ) && $request_body['continue'] ) {
368
			if ( in_array( $queue_name, array( 'full_sync', 'immediate' ), true ) ) {
369
				// Send Full Sync Actions.
370
				Sender::get_instance()->do_full_sync();
371
			} else {
372
				// Send Incremental Sync Actions.
373
				if ( $queue->has_any_items() ) {
374
					Sender::get_instance()->do_sync();
375
				}
376
			}
377
		}
378
379
		if ( is_wp_error( $response ) ) {
380
			return $response;
381
		}
382
383
		return array(
384
			'success' => $response,
385
			'status' => Actions::get_sync_status(),
386
		);
387
	}
388
389
	protected static function sanitize_item_ids( $item ) {
390
		// lets not delete any options that don't start with jpsq_sync-
391
		if ( ! is_string( $item ) || substr( $item, 0, 5 ) !== 'jpsq_' ) {
392
			return null;
393
		}
394
		//Limit to A-Z,a-z,0-9,_,-,.
395
		return preg_replace( '/[^A-Za-z0-9-_.]/', '', $item );
396
	}
397
}
398
399
class Jetpack_JSON_API_Sync_Unlock_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
400
	protected function result() {
401
		$args = $this->input();
402
403
		if ( ! isset( $args['queue'] ) ) {
404
			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_queue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
405
		}
406
407 View Code Duplication
		if ( ! in_array( $args['queue'], array( 'sync', 'full_sync' ) ) ) {
408
			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_queue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
409
		}
410
411
		$queue = new Queue( $args['queue'] );
412
413
		// False means that there was no lock to delete.
414
		$response = $queue->unlock();
415
		return array(
416
			'success' => $response
417
		);
418
	}
419
}
420
421
class Jetpack_JSON_API_Sync_Object_Id_Range extends Jetpack_JSON_API_Sync_Endpoint {
422
	protected function result() {
423
		$args = $this->query_args();
424
425
		$module_name = $args['sync_module'];
426
		$batch_size  = $args['batch_size'];
427
428
		if ( ! $this->is_valid_sync_module( $module_name ) ) {
429
			return new WP_Error( 'invalid_module', 'This sync module cannot be used to calculate a range.', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_module'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
430
		}
431
432
		$module = Modules::get_module( $module_name );
433
434
		return array(
435
			'ranges' => $module->get_min_max_object_ids_for_batches( $batch_size ),
436
		);
437
	}
438
439
	protected function is_valid_sync_module( $module_name ) {
440
		return in_array(
441
			$module_name,
442
			array(
443
				'comments',
444
				'posts',
445
				'terms',
446
				'term_relationships',
447
				'users',
448
			),
449
			true
450
		);
451
	}
452
}
453