Completed
Push — add/changelog-960 ( c446e0 )
by Jeremy
121:05 queued 110:36
created

Jetpack_JSON_API_Sync_Modify_Health_Endpoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A result() 0 17 3
1
<?php
2
3
use Automattic\Jetpack\Sync\Actions;
4
use Automattic\Jetpack\Sync\Health;
5
use Automattic\Jetpack\Sync\Modules;
6
use Automattic\Jetpack\Sync\Queue;
7
use Automattic\Jetpack\Sync\Queue_Buffer;
8
use Automattic\Jetpack\Sync\Replicastore;
9
use Automattic\Jetpack\Sync\Sender;
10
use Automattic\Jetpack\Sync\Settings;
11
12
// POST /sites/%s/sync
13
class Jetpack_JSON_API_Sync_Endpoint extends Jetpack_JSON_API_Endpoint {
14
15
	/**
16
	 * This endpoint allows authentication both via a blog and a user token.
17
	 * If a user token is used, that user should have `manage_options` capability.
18
	 *
19
	 * @var array|string
20
	 */
21
	protected $needed_capabilities = 'manage_options';
22
23
	protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) {
24
		return parent::validate_call( $_blog_id, $capability, false );
25
	}
26
27
	protected function result() {
28
		$args = $this->input();
29
		$modules = null;
30
31
		// convert list of modules in comma-delimited format into an array
32
		// of "$modulename => true"
33 View Code Duplication
		if ( isset( $args['modules'] ) && ! empty( $args['modules'] ) ) {
34
			$modules = array_map( '__return_true', array_flip( array_map( 'trim', explode( ',', $args['modules'] ) ) ) );
35
		}
36
37 View Code Duplication
		foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) {
38
			if ( 'users' === $module_name && isset( $args[ $module_name ] ) && 'initial' === $args[ $module_name ] ) {
39
				$modules[ 'users' ] = 'initial';
40
			} elseif ( isset( $args[ $module_name ] ) ) {
41
				$ids = explode( ',', $args[ $module_name ] );
42
				if ( count( $ids ) > 0 ) {
43
					$modules[ $module_name ] = $ids;
44
				}
45
			}
46
		}
47
48
		if ( empty( $modules ) ) {
49
			$modules = null;
50
		}
51
		return array( 'scheduled' => Actions::do_full_sync( $modules ) );
52
	}
53
54
	protected function validate_queue( $query ) {
55
		if ( ! isset( $query ) ) {
56
			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...
57
		}
58
59 View Code Duplication
		if ( ! in_array( $query, array( 'sync', 'full_sync', 'immediate' ) ) ) {
60
			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...
61
		}
62
		return $query;
63
	}
64
}
65
66
// GET /sites/%s/sync/status
67
class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
68
	protected function result() {
69
		$args   = $this->query_args();
70
		$fields = isset( $args['fields'] ) ? $args['fields'] : array();
71
		return Actions::get_sync_status( $fields );
72
	}
73
}
74
75
// GET /sites/%s/data-check
76
class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
77
	protected function result() {
78
		Actions::mark_sync_read_only();
79
		$store = new Replicastore();
80
		return $store->checksum_all();
81
	}
82
}
83
84
// GET /sites/%s/data-histogram
85
class Jetpack_JSON_API_Sync_Histogram_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
86
	protected function result() {
87
		$args = $this->query_args();
88
89
		if ( isset( $args['columns'] ) ) {
90
			$columns = array_map( 'trim', explode( ',', $args['columns'] ) );
91
		} else {
92
			$columns = null; // go with defaults
93
		}
94
95
		$store = new Replicastore();
96
97
		if ( ! isset( $args['strip_non_ascii'] ) ) {
98
			$args['strip_non_ascii'] = true;
99
		}
100
101
		/**
102
		 * Hack: nullify the values of `start_id` and `end_id` if we're only requesting ranges.
103
		 *
104
		 * The endpoint doesn't support nullable values :(
105
		 */
106
		if ( true === $args['only_range_edges'] ) {
107
			if ( 0 === $args['start_id'] ) {
108
				$args['start_id'] = null;
109
			}
110
111
			if ( 0 === $args['end_id'] ) {
112
				$args['end_id'] = null;
113
			}
114
		}
115
116
		$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 90 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...
117
118
		// Hack to disable Sync during this call, so we can resolve faster.
119
		Actions::mark_sync_read_only();
120
121
		return array( 'histogram' => $histogram, 'type' => $store->get_checksum_type() );
122
	}
123
}
124
125
// phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound
126
/**
127
 * POST /sites/%s/sync/health
128
 */
129
class Jetpack_JSON_API_Sync_Modify_Health_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
130
131
	/**
132
	 * Callback for sync/health endpoint.
133
	 *
134
	 * @return array|WP_Error result of request.
135
	 */
136
	protected function result() {
137
		$args = $this->input();
138
139
		switch ( $args['status'] ) {
140
			case Health::STATUS_IN_SYNC:
141
			case Health::STATUS_OUT_OF_SYNC:
142
				Health::update_status( $args['status'] );
143
				break;
144
			default:
145
				return new WP_Error( 'invalid_status', 'Invalid Sync Status Provided.' );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_status'.

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...
146
		}
147
148
		// re-fetch so we see what's really being stored.
149
		return array(
150
			'success' => Health::get_status(),
151
		);
152
	}
153
}
154
// phpcs:enable
155
156
// POST /sites/%s/sync/settings
157
class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
158
	protected function result() {
159
		$args = $this->input();
160
161
		$sync_settings = Settings::get_settings();
162
163
		foreach ( $args as $key => $value ) {
164
			if ( $value !== false ) {
165
				if ( is_numeric( $value ) ) {
166
					$value = (int) $value;
167
				}
168
169
				// special case for sending empty arrays - a string with value 'empty'
170
				if ( $value === 'empty' ) {
171
					$value = array();
172
				}
173
174
				$sync_settings[ $key ] = $value;
175
			}
176
		}
177
178
		Settings::update_settings( $sync_settings );
179
180
		// re-fetch so we see what's really being stored
181
		return Settings::get_settings();
182
	}
183
}
184
185
// GET /sites/%s/sync/settings
186
class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
187
	protected function result() {
188
189
		return Settings::get_settings();
190
	}
191
}
192
193
// GET /sites/%s/sync/object
194
class Jetpack_JSON_API_Sync_Object extends Jetpack_JSON_API_Sync_Endpoint {
195
	protected function result() {
196
		$args = $this->query_args();
197
198
		$module_name = $args['module_name'];
199
200
		if ( ! $sync_module = Modules::get_module( $module_name ) ) {
201
			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...
202
		}
203
204
		$object_type = $args['object_type'];
205
		$object_ids  = $args['object_ids'];
206
207
		$codec = Sender::get_instance()->get_codec();
208
209
		Actions::mark_sync_read_only();
210
		Settings::set_is_syncing( true );
211
		$objects = $codec->encode( $sync_module->get_objects_by_id( $object_type, $object_ids ) );
212
		Settings::set_is_syncing( false );
213
214
		return array(
215
			'objects' => $objects,
216
			'codec' => $codec->name(),
217
		);
218
	}
219
}
220
221
class Jetpack_JSON_API_Sync_Now_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
222
	protected function result() {
223
		$args = $this->input();
224
		$queue_name = $this->validate_queue( $args['queue'] );
225
226
		if ( is_wp_error( $queue_name ) ){
227
			return $queue_name;
228
		}
229
230
		$sender = Sender::get_instance();
231
		$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...
232
233
		return array(
234
			'response' => $response
235
		);
236
	}
237
}
238
239
class Jetpack_JSON_API_Sync_Checkout_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
240
	protected function result() {
241
		$args       = $this->input();
242
		$queue_name = $this->validate_queue( $args['queue'] );
243
244
		if ( is_wp_error( $queue_name ) ) {
245
			return $queue_name;
246
		}
247
248
		if ( $args['number_of_items'] < 1 || $args['number_of_items'] > 100 ) {
249
			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...
250
		}
251
252
		$number_of_items = absint( $args['number_of_items'] );
253
254
		if ( 'immediate' === $queue_name ) {
255
			return $this->immediate_full_sync_pull( $number_of_items );
256
		}
257
258
		return $this->queue_pull( $queue_name, $number_of_items, $args );
259
	}
260
261
	function queue_pull( $queue_name, $number_of_items, $args ){
262
		$queue = new Queue( $queue_name );
263
264
		if ( 0 === $queue->size() ) {
265
			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...
266
		}
267
268
		$sender = Sender::get_instance();
269
270
		// try to give ourselves as much time as possible.
271
		set_time_limit( 0 );
272
273
		if ( $args['pop'] ) {
274
			$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...
275
		} else {
276
			// let's delete the checkin state.
277
			if ( $args['force'] ) {
278
				$queue->unlock();
279
			}
280
			$buffer = $this->get_buffer( $queue, $number_of_items );
281
		}
282
		// Check that the $buffer is not checkout out already.
283
		if ( is_wp_error( $buffer ) ) {
284
			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...
285
		}
286
287
		if ( ! is_object( $buffer ) ) {
288
			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...
289
		}
290
291
		Settings::set_is_syncing( true );
292
		list( $items_to_send, $skipped_items_ids ) = $sender->get_items_to_send( $buffer, $args['encode'] );
293
		Settings::set_is_syncing( false );
294
295
		return array(
296
			'buffer_id'      => $buffer->id,
297
			'items'          => $items_to_send,
298
			'skipped_items'  => $skipped_items_ids,
299
			'codec'          => $args['encode'] ? $sender->get_codec()->name() : null,
300
			'sent_timestamp' => time(),
301
		);
302
	}
303
304
	public $items = [];
305
306
	public function jetpack_sync_send_data_listener() {
307
		foreach ( func_get_args()[0] as $key => $item ) {
308
			$this->items[ $key ] = $item;
309
		}
310
	}
311
312
	/**
313
	 * Check out a buffer of full sync actions.
314
	 *
315
	 * @param null $number_of_items Number of Actions to check-out.
316
	 *
317
	 * @return array Sync Actions to be returned to requestor
318
	 */
319
	public function immediate_full_sync_pull( $number_of_items = null ) {
320
		// try to give ourselves as much time as possible.
321
		set_time_limit( 0 );
322
323
		$original_send_data_cb = array( 'Automattic\Jetpack\Sync\Actions', 'send_data' );
324
		$temp_send_data_cb     = array( $this, 'jetpack_sync_send_data_listener' );
325
326
		Sender::get_instance()->set_enqueue_wait_time( 0 );
327
		remove_filter( 'jetpack_sync_send_data', $original_send_data_cb );
328
		add_filter( 'jetpack_sync_send_data', $temp_send_data_cb, 10, 6 );
329
		Sender::get_instance()->do_full_sync();
330
		remove_filter( 'jetpack_sync_send_data', $temp_send_data_cb );
331
		add_filter( 'jetpack_sync_send_data', $original_send_data_cb, 10, 6 );
332
333
		return array(
334
			'items'          => $this->items,
335
			'codec'          => Sender::get_instance()->get_codec()->name(),
336
			'sent_timestamp' => time(),
337
			'status'         => Actions::get_sync_status(),
338
		);
339
	}
340
341
	protected function get_buffer( $queue, $number_of_items ) {
342
		$start = time();
343
		$max_duration = 5; // this will try to get the buffer
344
345
		$buffer = $queue->checkout( $number_of_items );
346
		$duration = time() - $start;
347
348
		while( is_wp_error( $buffer ) && $duration < $max_duration ) {
349
			sleep( 2 );
350
			$duration = time() - $start;
351
			$buffer = $queue->checkout( $number_of_items );
352
		}
353
354
		if ( $buffer === false ) {
355
			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...
356
		}
357
358
		return $buffer;
359
	}
360
}
361
362
class Jetpack_JSON_API_Sync_Close_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
363
	protected function result() {
364
365
		$request_body = $this->input();
366
		$queue_name = $this->validate_queue( $request_body['queue'] );
367
368
		if ( is_wp_error( $queue_name ) ) {
369
			return $queue_name;
370
		}
371
372
		if ( ! isset( $request_body['buffer_id'] ) ) {
373
			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...
374
		}
375
376
		if ( ! isset( $request_body['item_ids'] ) || ! is_array( $request_body['item_ids'] ) ) {
377
			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...
378
		}
379
380
		//Limit to A-Z,a-z,0-9,_,-
381
		$request_body ['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] );
382
		$request_body['item_ids'] = array_filter( array_map( array( 'Jetpack_JSON_API_Sync_Close_Endpoint', 'sanitize_item_ids' ), $request_body['item_ids'] ) );
383
384
		$queue = new Queue( $queue_name );
385
386
		$items = $queue->peek_by_id( $request_body['item_ids'] );
387
388
		// Update Full Sync Status if queue is "full_sync".
389
		if ( 'full_sync' === $queue_name ) {
390
			$full_sync_module = Modules::get_module( 'full-sync' );
391
392
			$full_sync_module->update_sent_progress_action( $items );
393
		}
394
395
		$buffer = new Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] );
396
		$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...
397
398
		// Perform another checkout?
399
		if ( isset( $request_body['continue'] ) && $request_body['continue'] ) {
400
			if ( in_array( $queue_name, array( 'full_sync', 'immediate' ), true ) ) {
401
				// Send Full Sync Actions.
402
				Sender::get_instance()->do_full_sync();
403
			} else {
404
				// Send Incremental Sync Actions.
405
				if ( $queue->has_any_items() ) {
406
					Sender::get_instance()->do_sync();
407
				}
408
			}
409
		}
410
411
		if ( is_wp_error( $response ) ) {
412
			return $response;
413
		}
414
415
		return array(
416
			'success' => $response,
417
			'status' => Actions::get_sync_status(),
418
		);
419
	}
420
421
	protected static function sanitize_item_ids( $item ) {
422
		// lets not delete any options that don't start with jpsq_sync-
423
		if ( ! is_string( $item ) || substr( $item, 0, 5 ) !== 'jpsq_' ) {
424
			return null;
425
		}
426
		//Limit to A-Z,a-z,0-9,_,-,.
427
		return preg_replace( '/[^A-Za-z0-9-_.]/', '', $item );
428
	}
429
}
430
431
class Jetpack_JSON_API_Sync_Unlock_Endpoint extends Jetpack_JSON_API_Sync_Endpoint {
432
	protected function result() {
433
		$args = $this->input();
434
435
		if ( ! isset( $args['queue'] ) ) {
436
			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...
437
		}
438
439 View Code Duplication
		if ( ! in_array( $args['queue'], array( 'sync', 'full_sync' ) ) ) {
440
			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...
441
		}
442
443
		$queue = new Queue( $args['queue'] );
444
445
		// False means that there was no lock to delete.
446
		$response = $queue->unlock();
447
		return array(
448
			'success' => $response
449
		);
450
	}
451
}
452
453
class Jetpack_JSON_API_Sync_Object_Id_Range extends Jetpack_JSON_API_Sync_Endpoint {
454
	protected function result() {
455
		$args = $this->query_args();
456
457
		$module_name = $args['sync_module'];
458
		$batch_size  = $args['batch_size'];
459
460
		if ( ! $this->is_valid_sync_module( $module_name ) ) {
461
			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...
462
		}
463
464
		$module = Modules::get_module( $module_name );
465
466
		return array(
467
			'ranges' => $module->get_min_max_object_ids_for_batches( $batch_size ),
468
		);
469
	}
470
471
	protected function is_valid_sync_module( $module_name ) {
472
		return in_array(
473
			$module_name,
474
			array(
475
				'comments',
476
				'posts',
477
				'terms',
478
				'term_relationships',
479
				'users',
480
			),
481
			true
482
		);
483
	}
484
}
485