Completed
Pull Request — master (#11082)
by Jeff
07:29
created

WC_REST_Settings_API_Controller   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 14.1 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 11
loc 78
rs 10
wmc 6
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A permissions_check() 11 11 2
A filter_setting() 0 12 2
A allowed_setting_keys() 0 6 1
A is_setting_type_valid() 0 7 1

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
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Base class for settings endpoints so common code can be shared. Handles permissions
8
 * and helper functions used by both settings and groups endpoints.
9
 *
10
 * @author   WooThemes
11
 * @category API
12
 * @package  WooCommerce/Abstracts
13
 * @extends  WP_REST_Controller
14
 * @version  2.7.0
15
 */
16
class WC_REST_Settings_API_Controller extends WC_REST_Controller {
17
18
	/**
19
	 * Route base.
20
	 *
21
	 * @var string
22
	 */
23
	protected $rest_base = 'settings';
24
25
	/**
26
	 * Makes sure the current user has access to the settings APIs.
27
	 *
28
	 * @since  2.7.0
29
	 * @param  WP_REST_Request $request Full details about the request.
30
	 * @return WP_Error|boolean
31
	 */
32 View Code Duplication
	public function permissions_check( $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
33
		if ( ! current_user_can( 'manage_options' ) ) {
34
			return new WP_Error(
35
				'woocommerce_rest_cannot_view',
36
				__( 'Sorry, you cannot access settings.', 'woocommerce' ),
37
				array( 'status' => rest_authorization_required_code() )
38
			);
39
		}
40
41
		return true;
42
	}
43
44
	/**
45
	 * Filters out bad values from the settings array/filter so we
46
	 * only return known values via the API.
47
	 *
48
	 * @since 2.7.0
49
	 * @param  array $setting
50
	 * @return array
51
	 */
52
	public function filter_setting( $setting ) {
53
		$setting = array_intersect_key(
54
			$setting,
55
			array_flip( array_filter( array_keys( $setting ), array( $this, 'allowed_setting_keys' ) ) )
56
		);
57
58
		if ( empty( $setting['options'] ) ) {
59
			unset( $setting['options'] );
60
		}
61
62
		return $setting;
63
	}
64
65
	/**
66
	 * Callback for allowed keys for each setting response.
67
	 *
68
	 * @since  2.7.0
69
	 * @param  string $key Key to check
70
	 * @return boolean
71
	 */
72
	public function allowed_setting_keys( $key ) {
73
		return in_array( $key, array(
74
			'id', 'label', 'description', 'default', 'tip',
75
			'placeholder', 'type', 'options', 'value',
76
		) );
77
	}
78
79
	/**
80
	 * Boolean for if a setting type is a valid supported setting type.
81
	 *
82
	 * @since  2.7.0
83
	 * @param  string  $type
84
	 * @return boolean
85
	 */
86
	public function is_setting_type_valid( $type ) {
87
		return in_array( $type, array(
88
			'text', 'email', 'number', 'color', 'password',
89
			'textarea', 'select', 'multiselect', 'radio', 'checkbox',
90
			'multi_select_countries', 'image_width',
91
		) );
92
	}
93
}
94