Completed
Pull Request — master (#11082)
by Jeff
08:06
created

wp-rest-functions.php ➔ rest_validate_request_arg()   D

Complexity

Conditions 47
Paths 146

Size

Total Lines 74
Code Lines 47

Duplication

Lines 28
Ratio 37.84 %

Importance

Changes 0
Metric Value
cc 47
eloc 47
nc 146
nop 3
dl 28
loc 74
rs 4.8387
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @version 2.0-beta13.1
4
 */
5
6
if ( ! defined( 'ABSPATH' ) ) {
7
	exit;
8
}
9
10
/**
11
 * core-integration.php
12
 */
13
14
if ( ! function_exists( 'wp_parse_slug_list' ) ) {
15
	/**
16
	 * Clean up an array, comma- or space-separated list of slugs.
17
	 *
18
	 * @since
19
	 *
20
	 * @param  array|string $list List of slugs.
21
	 * @return array Sanitized array of slugs.
22
	 */
23
	function wp_parse_slug_list( $list ) {
24
		if ( ! is_array( $list ) ) {
25
			$list = preg_split( '/[\s,]+/', $list );
26
		}
27
28
		foreach ( $list as $key => $value ) {
29
			$list[ $key ] = sanitize_title( $value );
30
		}
31
32
		return array_unique( $list );
33
	}
34
}
35
36
if ( ! function_exists( 'rest_get_server' ) ) {
37
	/**
38
	 * Retrieves the current REST server instance.
39
	 *
40
	 * Instantiates a new instance if none exists already.
41
	 *
42
	 * @since 4.5.0
43
	 *
44
	 * @global WP_REST_Server $wp_rest_server REST server instance.
45
	 *
46
	 * @return WP_REST_Server REST server instance.
47
	 */
48
	function rest_get_server() {
49
		/* @var WP_REST_Server $wp_rest_server */
50
		global $wp_rest_server;
51
52
		if ( empty( $wp_rest_server ) ) {
53
			/**
54
			 * Filter the REST Server Class.
55
			 *
56
			 * This filter allows you to adjust the server class used by the API, using a
57
			 * different class to handle requests.
58
			 *
59
			 * @since 4.4.0
60
			 *
61
			 * @param string $class_name The name of the server class. Default 'WP_REST_Server'.
62
			 */
63
			$wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' );
64
			$wp_rest_server = new $wp_rest_server_class;
65
66
			/**
67
			 * Fires when preparing to serve an API request.
68
			 *
69
			 * Endpoint objects should be created and register their hooks on this action rather
70
			 * than another action to ensure they're only loaded when needed.
71
			 *
72
			 * @since 4.4.0
73
			 *
74
			 * @param WP_REST_Server $wp_rest_server Server object.
75
			 */
76
			do_action( 'rest_api_init', $wp_rest_server );
77
		}
78
79
		return $wp_rest_server;
80
	}
81
}
82
83
/**
84
 * plugin.php
85
 */
86
87
if ( ! function_exists( 'rest_authorization_required_code' ) ) {
88
	/**
89
	 * Returns a contextual HTTP error code for authorization failure.
90
	 *
91
	 * @return integer
92
	 */
93
	function rest_authorization_required_code() {
0 ignored issues
show
Best Practice introduced by
The function rest_authorization_required_code() has been defined more than once; this definition is ignored, only the first definition in includes/vendor/wp-api-functions.php (L17-19) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
94
		return is_user_logged_in() ? 403 : 401;
95
	}
96
}
97
98 View Code Duplication
if ( ! function_exists( 'register_rest_field' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
	/**
100
	 * Registers a new field on an existing WordPress object type.
101
	 *
102
	 * @global array $wp_rest_additional_fields Holds registered fields, organized
103
	 *                                          by object type.
104
	 *
105
	 * @param string|array $object_type Object(s) the field is being registered
106
	 *                                  to, "post"|"term"|"comment" etc.
107
	 * @param string $attribute         The attribute name.
108
	 * @param array  $args {
109
	 *     Optional. An array of arguments used to handle the registered field.
110
	 *
111
	 *     @type string|array|null $get_callback    Optional. The callback function used to retrieve the field
112
	 *                                              value. Default is 'null', the field will not be returned in
113
	 *                                              the response.
114
	 *     @type string|array|null $update_callback Optional. The callback function used to set and update the
115
	 *                                              field value. Default is 'null', the value cannot be set or
116
	 *                                              updated.
117
	 *     @type string|array|null $schema          Optional. The callback function used to create the schema for
118
	 *                                              this field. Default is 'null', no schema entry will be returned.
119
	 * }
120
	 */
121
	function register_rest_field( $object_type, $attribute, $args = array() ) {
0 ignored issues
show
Best Practice introduced by
The function register_rest_field() has been defined more than once; this definition is ignored, only the first definition in includes/vendor/wp-api-functions.php (L45-61) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
122
		$defaults = array(
123
			'get_callback'    => null,
124
			'update_callback' => null,
125
			'schema'          => null,
126
		);
127
128
		$args = wp_parse_args( $args, $defaults );
129
130
		global $wp_rest_additional_fields;
131
132
		$object_types = (array) $object_type;
133
134
		foreach ( $object_types as $object_type ) {
135
			$wp_rest_additional_fields[ $object_type ][ $attribute ] = $args;
136
		}
137
	}
138
}
139
140 View Code Duplication
if ( ! function_exists( 'register_api_field' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
	/**
142
	 * Backwards compat shim
143
	 */
144
	function register_api_field( $object_type, $attributes, $args = array() ) {
0 ignored issues
show
Best Practice introduced by
The function register_api_field() has been defined more than once; this definition is ignored, only the first definition in includes/vendor/wp-api-functions.php (L68-71) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
145
		_deprecated_function( 'register_api_field', 'WPAPI-2.0', 'register_rest_field' );
146
		register_rest_field( $object_type, $attributes, $args );
147
	}
148
}
149
150
if ( ! function_exists( 'rest_validate_request_arg' ) ) {
151
	/**
152
	 * Validate a request argument based on details registered to the route.
153
	 *
154
	 * @param  mixed            $value
155
	 * @param  WP_REST_Request  $request
156
	 * @param  string           $param
157
	 * @return WP_Error|boolean
158
	 */
159
	function rest_validate_request_arg( $value, $request, $param ) {
0 ignored issues
show
Best Practice introduced by
The function rest_validate_request_arg() has been defined more than once; this definition is ignored, only the first definition in includes/vendor/wp-api-functions.php (L83-156) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
160
161
		$attributes = $request->get_attributes();
162
		if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) {
163
			return true;
164
		}
165
		$args = $attributes['args'][ $param ];
166
167 View Code Duplication
		if ( ! empty( $args['enum'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
			if ( ! in_array( $value, $args['enum'] ) ) {
169
				return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not one of %s', 'woocommerce' ), $param, implode( ', ', $args['enum'] ) ) );
170
			}
171
		}
172
173
		if ( 'integer' === $args['type'] && ! is_numeric( $value ) ) {
174
			return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s', 'woocommerce' ), $param, 'integer' ) );
175
		}
176
177 View Code Duplication
		if ( 'string' === $args['type'] && ! is_string( $value ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
			return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not of type %s', 'woocommerce' ), $param, 'string' ) );
179
		}
180
181 View Code Duplication
		if ( isset( $args['format'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
			switch ( $args['format'] ) {
183
				case 'date-time' :
184
					if ( ! rest_parse_date( $value ) ) {
185
						return new WP_Error( 'rest_invalid_date', __( 'The date you provided is invalid.', 'woocommerce' ) );
186
					}
187
					break;
188
189
				case 'email' :
190
					if ( ! is_email( $value ) ) {
191
						return new WP_Error( 'rest_invalid_email', __( 'The email address you provided is invalid.', 'woocommerce' ) );
192
					}
193
					break;
194
			}
195
		}
196
197
		if ( in_array( $args['type'], array( 'numeric', 'integer' ) ) && ( isset( $args['minimum'] ) || isset( $args['maximum'] ) ) ) {
198
			if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {
199 View Code Duplication
				if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
					return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (exclusive)', 'woocommerce' ), $param, $args['minimum'] ) );
201
				} else if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {
202
					return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be greater than %d (inclusive)', 'woocommerce' ), $param, $args['minimum'] ) );
203
				}
204
			} else if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {
205 View Code Duplication
				if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
					return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (exclusive)', 'woocommerce' ), $param, $args['maximum'] ) );
207
				} else if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {
208
					return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be less than %d (inclusive)', 'woocommerce' ), $param, $args['maximum'] ) );
209
				}
210
			} else if ( isset( $args['maximum'] ) && isset( $args['minimum'] ) ) {
211
				if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
212
					if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) {
213
						return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (exclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) );
214
					}
215 View Code Duplication
				} else if ( empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
					if ( $value >= $args['maximum'] || $value < $args['minimum'] ) {
217
						return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (exclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) );
218
					}
219
				} else if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
220
					if ( $value > $args['maximum'] || $value <= $args['minimum'] ) {
221
						return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (exclusive) and %d (inclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) );
222
					}
223
				} else if ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
224
					if ( $value > $args['maximum'] || $value < $args['minimum'] ) {
225
						return new WP_Error( 'rest_invalid_param', sprintf( __( '%s must be between %d (inclusive) and %d (inclusive)', 'woocommerce' ), $param, $args['minimum'], $args['maximum'] ) );
226
					}
227
				}
228
			}
229
		}
230
231
		return true;
232
	}
233
}
234
235 View Code Duplication
if ( ! function_exists( 'rest_sanitize_request_arg' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
	/**
237
	 * Sanitize a request argument based on details registered to the route.
238
	 *
239
	 * @param  mixed            $value
240
	 * @param  WP_REST_Request  $request
241
	 * @param  string           $param
242
	 * @return mixed
243
	 */
244
	function rest_sanitize_request_arg( $value, $request, $param ) {
0 ignored issues
show
Best Practice introduced by
The function rest_sanitize_request_arg() has been defined more than once; this definition is ignored, only the first definition in includes/vendor/wp-api-functions.php (L168-197) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
245
246
		$attributes = $request->get_attributes();
247
		if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) {
248
			return $value;
249
		}
250
		$args = $attributes['args'][ $param ];
251
252
		if ( 'integer' === $args['type'] ) {
253
			return (int) $value;
254
		}
255
256
		if ( isset( $args['format'] ) ) {
257
			switch ( $args['format'] ) {
258
				case 'date-time' :
259
					return sanitize_text_field( $value );
260
261
				case 'email' :
262
					/*
263
					 * sanitize_email() validates, which would be unexpected
264
					 */
265
					return sanitize_text_field( $value );
266
267
				case 'uri' :
268
					return esc_url_raw( $value );
269
			}
270
		}
271
272
		return $value;
273
	}
274
275
}
276