WP_Test_REST_Request_Validation   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 165
Duplicated Lines 81.82 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A test_validate_within_min_max_range_inclusive() 0 19 1
B test_validate_within_min_max_range_min_exclusive() 24 24 1
B test_validate_within_min_max_range_max_exclusive() 24 24 1
B test_validate_within_min_max_range_both_exclusive() 25 25 1
A test_validate_greater_than_min_inclusive() 14 14 1
A test_validate_greater_than_min_exclusive() 17 17 1
A test_validate_less_than_max_inclusive() 14 14 1
A test_validate_less_than_max_exclusive() 17 17 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
3
class WP_Test_REST_Request_Validation extends WP_Test_REST_TestCase {
4
5
	public function test_validate_within_min_max_range_inclusive() {
6
		$request = new WP_REST_Request( 'GET', '/wp/v2/foo', array(
7
			'args' => array(
8
				'minmaxrange' => array(
9
					'type'    => 'integer',
10
					'minimum' => 2,
11
					'maximum' => 10,
12
				),
13
			),
14
		) );
15
		$ret = rest_validate_request_arg( 1, $request, 'minmaxrange' );
16
		$this->assertEquals( 'minmaxrange must be between 2 (inclusive) and 10 (inclusive)', $ret->get_error_message() );
17
		$ret = rest_validate_request_arg( 2, $request, 'minmaxrange' );
18
		$this->assertTrue( $ret );
19
		$ret = rest_validate_request_arg( 10, $request, 'minmaxrange' );
20
		$this->assertTrue( $ret );
21
		$ret = rest_validate_request_arg( 11, $request, 'minmaxrange' );
22
		$this->assertEquals( 'minmaxrange must be between 2 (inclusive) and 10 (inclusive)', $ret->get_error_message() );
23
	}
24
25 View Code Duplication
	public function test_validate_within_min_max_range_min_exclusive() {
26
		$request = new WP_REST_Request( 'GET', '/wp/v2/foo', array(
27
			'args' => array(
28
				'minmaxrange' => array(
29
					'type'             => 'integer',
30
					'minimum'          => 2,
31
					'maximum'          => 10,
32
					'exclusiveMinimum' => true,
33
				),
34
			),
35
		) );
36
		$ret = rest_validate_request_arg( 1, $request, 'minmaxrange' );
37
		$this->assertEquals( 'minmaxrange must be between 2 (exclusive) and 10 (inclusive)', $ret->get_error_message() );
38
		$ret = rest_validate_request_arg( 2, $request, 'minmaxrange' );
39
		$this->assertEquals( 'minmaxrange must be between 2 (exclusive) and 10 (inclusive)', $ret->get_error_message() );
40
		$ret = rest_validate_request_arg( 3, $request, 'minmaxrange' );
41
		$this->assertTrue( $ret );
42
		$ret = rest_validate_request_arg( 9, $request, 'minmaxrange' );
43
		$this->assertTrue( $ret );
44
		$ret = rest_validate_request_arg( 10, $request, 'minmaxrange' );
45
		$this->assertTrue( $ret );
46
		$ret = rest_validate_request_arg( 11, $request, 'minmaxrange' );
47
		$this->assertEquals( 'minmaxrange must be between 2 (exclusive) and 10 (inclusive)', $ret->get_error_message() );
48
	}
49
50 View Code Duplication
	public function test_validate_within_min_max_range_max_exclusive() {
51
		$request = new WP_REST_Request( 'GET', '/wp/v2/foo', array(
52
			'args' => array(
53
				'minmaxrange' => array(
54
					'type'             => 'integer',
55
					'minimum'          => 2,
56
					'maximum'          => 10,
57
					'exclusiveMaximum' => true,
58
				),
59
			),
60
		) );
61
		$ret = rest_validate_request_arg( 1, $request, 'minmaxrange' );
62
		$this->assertEquals( 'minmaxrange must be between 2 (inclusive) and 10 (exclusive)', $ret->get_error_message() );
63
		$ret = rest_validate_request_arg( 2, $request, 'minmaxrange' );
64
		$this->assertTrue( $ret );
65
		$ret = rest_validate_request_arg( 3, $request, 'minmaxrange' );
66
		$this->assertTrue( $ret );
67
		$ret = rest_validate_request_arg( 9, $request, 'minmaxrange' );
68
		$this->assertTrue( $ret );
69
		$ret = rest_validate_request_arg( 10, $request, 'minmaxrange' );
70
		$this->assertEquals( 'minmaxrange must be between 2 (inclusive) and 10 (exclusive)', $ret->get_error_message() );
71
		$ret = rest_validate_request_arg( 11, $request, 'minmaxrange' );
72
		$this->assertEquals( 'minmaxrange must be between 2 (inclusive) and 10 (exclusive)', $ret->get_error_message() );
73
	}
74
75 View Code Duplication
	public function test_validate_within_min_max_range_both_exclusive() {
76
		$request = new WP_REST_Request( 'GET', '/wp/v2/foo', array(
77
			'args' => array(
78
				'minmaxrange' => array(
79
					'type'             => 'integer',
80
					'minimum'          => 2,
81
					'maximum'          => 10,
82
					'exclusiveMinimum' => true,
83
					'exclusiveMaximum' => true,
84
				),
85
			),
86
		) );
87
		$ret = rest_validate_request_arg( 1, $request, 'minmaxrange' );
88
		$this->assertEquals( 'minmaxrange must be between 2 (exclusive) and 10 (exclusive)', $ret->get_error_message() );
89
		$ret = rest_validate_request_arg( 2, $request, 'minmaxrange' );
90
		$this->assertEquals( 'minmaxrange must be between 2 (exclusive) and 10 (exclusive)', $ret->get_error_message() );
91
		$ret = rest_validate_request_arg( 3, $request, 'minmaxrange' );
92
		$this->assertTrue( $ret );
93
		$ret = rest_validate_request_arg( 9, $request, 'minmaxrange' );
94
		$this->assertTrue( $ret );
95
		$ret = rest_validate_request_arg( 10, $request, 'minmaxrange' );
96
		$this->assertEquals( 'minmaxrange must be between 2 (exclusive) and 10 (exclusive)', $ret->get_error_message() );
97
		$ret = rest_validate_request_arg( 11, $request, 'minmaxrange' );
98
		$this->assertEquals( 'minmaxrange must be between 2 (exclusive) and 10 (exclusive)', $ret->get_error_message() );
99
	}
100
101 View Code Duplication
	public function test_validate_greater_than_min_inclusive() {
102
		$request = new WP_REST_Request( 'GET', '/wp/v2/foo', array(
103
			'args' => array(
104
				'greaterthanmin' => array(
105
					'type'             => 'integer',
106
					'minimum'          => 2,
107
				),
108
			),
109
		) );
110
		$ret = rest_validate_request_arg( 1, $request, 'greaterthanmin' );
111
		$this->assertEquals( 'greaterthanmin must be greater than 2 (inclusive)', $ret->get_error_message() );
112
		$ret = rest_validate_request_arg( 2, $request, 'greaterthanmin' );
113
		$this->assertTrue( $ret );
114
	}
115
116 View Code Duplication
	public function test_validate_greater_than_min_exclusive() {
117
		$request = new WP_REST_Request( 'GET', '/wp/v2/foo', array(
118
			'args' => array(
119
				'greaterthanmin' => array(
120
					'type'             => 'integer',
121
					'minimum'          => 2,
122
					'exclusiveMinimum' => true,
123
				),
124
			),
125
		) );
126
		$ret = rest_validate_request_arg( 1, $request, 'greaterthanmin' );
127
		$this->assertEquals( 'greaterthanmin must be greater than 2 (exclusive)', $ret->get_error_message() );
128
		$ret = rest_validate_request_arg( 2, $request, 'greaterthanmin' );
129
		$this->assertEquals( 'greaterthanmin must be greater than 2 (exclusive)', $ret->get_error_message() );
130
		$ret = rest_validate_request_arg( 3, $request, 'greaterthanmin' );
131
		$this->assertTrue( $ret );
132
	}
133
134 View Code Duplication
	public function test_validate_less_than_max_inclusive() {
135
		$request = new WP_REST_Request( 'GET', '/wp/v2/foo', array(
136
			'args' => array(
137
				'lessthanmax' => array(
138
					'type'             => 'integer',
139
					'maximum'          => 10,
140
				),
141
			),
142
		) );
143
		$ret = rest_validate_request_arg( 11, $request, 'lessthanmax' );
144
		$this->assertEquals( 'lessthanmax must be less than 10 (inclusive)', $ret->get_error_message() );
145
		$ret = rest_validate_request_arg( 10, $request, 'lessthanmax' );
146
		$this->assertTrue( $ret );
147
	}
148
149 View Code Duplication
	public function test_validate_less_than_max_exclusive() {
150
		$request = new WP_REST_Request( 'GET', '/wp/v2/foo', array(
151
			'args' => array(
152
				'lessthanmax' => array(
153
					'type'             => 'integer',
154
					'maximum'          => 10,
155
					'exclusiveMaximum' => true,
156
				),
157
			),
158
		) );
159
		$ret = rest_validate_request_arg( 11, $request, 'lessthanmax' );
160
		$this->assertEquals( 'lessthanmax must be less than 10 (exclusive)', $ret->get_error_message() );
161
		$ret = rest_validate_request_arg( 10, $request, 'lessthanmax' );
162
		$this->assertEquals( 'lessthanmax must be less than 10 (exclusive)', $ret->get_error_message() );
163
		$ret = rest_validate_request_arg( 9, $request, 'lessthanmax' );
164
		$this->assertTrue( $ret );
165
	}
166
167
}
168