Completed
Push — master ( 280e7c...a946e7 )
by litefeel
48:45 queued 23:46
created

Writing_On_GitHub_Base_Client_Test   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 239
Duplicated Lines 20.08 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 48
loc 239
rs 8.439
c 0
b 0
f 0
wmc 47
lcom 2
cbo 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Writing_On_GitHub_Base_Client_Test often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Writing_On_GitHub_Base_Client_Test, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
abstract class Writing_On_GitHub_Base_Client_Test extends Writing_On_GitHub_TestCase {
4
5
	/**
6
	 * @var string
7
	 */
8
	const HOST_OPTION_VALUE = 'https://api.github.com';
9
10
	/**
11
	 * @var string
12
	 */
13
	const REPO_OPTION_VALUE = 'woghtest/wogh-test';
14
15
	/**
16
	 * @var string
17
	 */
18
	const TOKEN_OPTION_VALUE = 'the-token';
19
20
	/**
21
	 * @var array
22
	 */
23
	protected static $responses = array();
24
25
	/**
26
	 * @var array
27
	 */
28
	protected static $validations = array();
29
30
	public function setUp() {
31
		parent::setUp();
32
33
		WP_HTTP_TestCase::init();
34
		update_option( 'wogh_repository', self::REPO_OPTION_VALUE );
35
		update_option( 'wogh_oauth_token', self::TOKEN_OPTION_VALUE );
36
		update_option( 'wogh_host', self::HOST_OPTION_VALUE );
37
		$this->http_responder = array( $this, 'mock_github_api' );
38
	}
39
40
	/**
41
	 * This does some checks and fails the test if something is wrong
42
	 * or returns intended mock data for the given endpoint + method.
43
	 *
44
	 * @return void|string
45
	 */
46
	public function mock_github_api( $request, $url ) {
47
		$host_length = strlen( self::HOST_OPTION_VALUE );
48
49
		if ( self::HOST_OPTION_VALUE !== substr( $url, 0, $host_length ) ) {
50
			$this->assertTrue( false, 'Called wrong host.' );
51
		}
52
53
		if (
54
			! isset( $request['headers']['Authorization'] ) ||
55
			'token ' . self::TOKEN_OPTION_VALUE !== $request['headers']['Authorization']
56
		) {
57
			$this->assertTrue( false, 'Missing authorization key.' );
58
		}
59
60
		$url = explode( '/', substr( $url, $host_length + 1 ) );
61
62
		if ( 'repos' !== $url[0] ) {
63
			$this->assertTrue( false, 'Called wrong endpoint.' );
64
		}
65
66
		$repo = $url[1] . '/' . $url[2];
67
68
		if ( self::REPO_OPTION_VALUE !== $repo ) {
69
			$this->assertTrue( false, 'Called wrong repo.' );
70
		}
71
72
		$parts = array_slice( $url, 4 );
73
		array_unshift( $parts, strtolower( $request['method'] ) );
74
		$endpoint = implode( '_', $parts );
75
		$endpoint = str_replace( '?recursive=1', '', $endpoint );
76
		$this->assertTrue( call_user_func( static::$validations[ $endpoint ], $request ), 'Request did not validate.' );
77
78
		return static::$responses[ $endpoint ];
79
	}
80
81
	protected function set_get_refs_heads_master( $succeed ) {
82
		$this->set_endpoint(
83
			function ( $request ) {
84
				if ( '[]' === $request['body'] ) {
85
					return false;
86
				}
87
88
				return true;
89
			}, $succeed ? '200 OK' : '404 Not Found', $succeed
90
		);
91
	}
92
93
	protected function set_get_commits( $succeed ) {
94
		$this->set_endpoint(
95
			function ( $request ) {
96
				if ( '[]' === $request['body'] ) {
97
					return false;
98
				}
99
100
				return true;
101
			}, $succeed ? '200 OK' : '404 Not Found', $succeed, 'db2510854e6aeab68ead26b48328b19f4bdf926e'
102
		);
103
	}
104
105
	protected function set_get_trees( $succeed, $sha = '' ) {
106
		$this->set_endpoint(
107
			function ( $request ) {
108
				if ( '[]' === $request['body'] ) {
109
					return false;
110
				}
111
112
				return true;
113
			}, $succeed ? '200 OK' : '422 Unprocessable Entity', $succeed,
114
			$sha ? $sha : '9108868e3800bec6763e51beb0d33e15036c3626'
115
		);
116
	}
117
118
	protected function set_get_blobs( $succeed ) {
119
		$shas = array(
120
			'9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25',
121
			'8d9b2e6fd93761211dc03abd71f4a9189d680fd0',
122
			'2d73165945b0ccbe4932f1363457986b0ed49f19',
123
		);
124
125
		foreach ( $shas as $sha ) {
126
			$this->set_endpoint(
127
				function ( $request ) {
128
					if ( '[]' === $request['body'] ) {
129
						return false;
130
					}
131
132
					return true;
133
				}, $succeed ? '200 OK' : '404 Not Found', $succeed, $sha
134
			);
135
		}
136
	}
137
138
	protected function set_post_trees( $succeed ) {
139
		$this->set_endpoint(
140
			function ( $request ) {
141
				$body = json_decode( $request['body'], true );
142
143
				if ( ! isset( $body['tree'] ) ) {
144
					return false;
145
				}
146
147
				if ( 1 !== count( $body['tree'] ) ) {
148
					return false;
149
				}
150
151
				$blob = reset( $body['tree'] );
152
153
				if (
154
					! isset( $blob['path'] ) ||
155
					! isset( $blob['type'] ) ||
156
					! isset( $blob['content'] ) ||
157
					! isset( $blob['mode'] )
158
				) {
159
					return false;
160
				}
161
162
				return true;
163
			},
164
			$succeed ? '201 Created' : '404 Not Found',
165
			$succeed
166
		);
167
	}
168
169
	protected function set_post_commits( $succeed, $anonymous = true ) {
170
		$this->set_endpoint(
171
			function ( $request ) use ( $anonymous ) {
172
				$body = json_decode( $request['body'], true );
173
174
				if (
175
					! isset( $body['tree'] ) ||
176
					! isset( $body['message'] ) ||
177
					! isset( $body['parents'] ) ||
178
					! isset( $body['author'] )
179
				) {
180
					return false;
181
				}
182
183
				if ( 1 !== count( $body['parents'] ) ) {
184
					return false;
185
				}
186
187
				if ( ! $anonymous ) {
188
					if (
189
						'James DiGioia' !== $body['author']['name'] ||
190
						'[email protected]' !== $body['author']['email']
191
					) {
192
						return false;
193
					}
194
				} else {
195
					if (
196
						'Anonymous' !== $body['author']['name'] ||
197
						'[email protected]' !== $body['author']['email']
198
					) {
199
						return false;
200
					}
201
				}
202
203
				return true;
204
			},
205
			$succeed ? '201 Created' : '404 Not Found',
206
			$succeed
207
		);
208
	}
209
210
	protected function set_patch_refs_heads_master( $succeed ) {
211
		$this->set_endpoint(
212
			function ( $request ) {
213
				$body = json_decode( $request['body'], true );
214
215
				if ( ! isset( $body['sha'] ) ) {
216
					return false;
217
				}
218
219
				return true;
220
			},
221
			$succeed ? '201 Created' : '404 Not Found',
222
			$succeed
223
		);
224
	}
225
226
	private function set_endpoint( $validation, $status, $succeed, $sha = '' ) {
227
		list( , $caller ) = debug_backtrace( false );
228
		$endpoint = substr( $caller['function'], 4 ) . ( $sha ? "_$sha" : '' );
229
230
		static::$validations[ $endpoint ] = $validation;
231
232
		static::$responses[ $endpoint ] = array(
233
			'headers' => array(
234
				'status' => $status,
235
			),
236
			'body'    => file_get_contents(
237
				$this->data_dir . $endpoint . '_' . ( $succeed ? 'succeed' : 'fail' ) . '.json'
238
			),
239
		);
240
	}
241
}
242