Completed
Push — add/user-options-rest-endpoint ( c29b58 )
by
unknown
08:24
created

WPCOM_REST_API_V2_Endpoint_User_Option   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 13
loc 104
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A register_routes() 0 23 2
A get_option_permissions_check() 13 13 2
A get_option() 0 24 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * REST API endpoint for user options
4
 *
5
 * @package Jetpack
6
 * @since TODO
7
 */
8
9
/**
10
 * Class WPCOM_REST_API_V2_Endpoint_User_Option
11
 */
12
class WPCOM_REST_API_V2_Endpoint_User_Option extends WP_REST_Controller {
13
14
	/**
15
	 * Namespace prefix.
16
	 *
17
	 * @var string
18
	 */
19
	public $namespace = 'wpcom/v2';
20
21
	/**
22
	 * Endpoint base route.
23
	 *
24
	 * @var string
25
	 */
26
	public $rest_base = 'user-option';
27
28
	/**
29
	 * WPCOM_REST_API_V2_Endpoint_User_Option constructor.
30
	 */
31
	public function __construct() {
32
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
33
	}
34
35
	/**
36
	 * Register routes.
37
	 */
38
	public function register_routes() {
39
		register_rest_route(
40
			$this->namespace,
41
			$this->rest_base . '/',
42
			array(
43
				array(
44
					'methods'             => WP_REST_Server::READABLE,
45
					'callback'            => array( $this, 'get_option' ),
46
					'permission_callback' => array( $this, 'get_option_permissions_check' ),
47
					'args'                => array(
48
						'option' => array(
49
							'description'       => __( 'User option name.', 'jetpack' ),
50
							'type'              => 'string',
51
							'required'          => 'true',
52
							'validate_callback' => function ( $param ) {
53
								return is_string( $param ) && ! is_numeric( $param );
54
							},
55
						),
56
					),
57
				),
58
			)
59
		);
60
	}
61
62
63
	/**
64
	 * Checks if a given request has access to admin menus.
65
	 *
66
	 * @param WP_REST_Request $request Full details about the request.
67
	 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
68
	 */
69 View Code Duplication
	public function get_option_permissions_check( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
70
71
		// https://wordpress.org/support/article/roles-and-capabilities/#read
72
		if ( ! current_user_can( 'read' ) ) {
73
			return new WP_Error(
74
				'rest_forbidden',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'rest_forbidden'.

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...
75
				__( 'Sorry, you are not allowed to read user options on this site.', 'jetpack' ),
76
				array( 'status' => rest_authorization_required_code() )
77
			);
78
		}
79
80
		return true;
81
	}
82
83
84
85
	/**
86
	 * Retrieves the admin menu.
87
	 *
88
	 * @param WP_REST_Request $request Full details about the request.
89
	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
90
	 */
91
	public function get_option( $request ) {
92
93
		if ( empty( $request['option'] ) ) {
94
			return new WP_Error(
95
				'rest_forbidden',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'rest_forbidden'.

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...
96
				__( 'You must provide a valid option.', 'jetpack' ),
97
				array( 'status' => rest_authorization_required_code() )
98
			);
99
		}
100
101
		$option_key = $request['option'];
102
103
		$option = get_user_option( $option_key );
104
105
		if ( false === $option ) {
106
			return new WP_Error(
107
				'rest_forbidden',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'rest_forbidden'.

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...
108
				__( "No option found with key '$option_key'.", 'jetpack' ),
109
				array( 'status' => rest_authorization_required_code() )
110
			);
111
		}
112
113
		return rest_ensure_response( $option );
114
	}
115
}
116
117
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_User_Option' );
118