Completed
Push — update/syn-health-endpoint ( 88b639 )
by
unknown
22:25 queued 11:23
created

Jetpack_JSON_API_Sync_Modify_Health_Endpoint   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

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