Completed
Push — update/crm-integration-fixes ( a3fc2b )
by Jeremy
14:51 queued 06:26
created

WPCOM_JSON_API_Get_Post_Endpoint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 3
loc 33
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B callback() 3 28 7

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
new WPCOM_JSON_API_Get_Post_Endpoint( array(
4
	'description' => 'Get a single post (by ID).',
5
	'group'       => 'posts',
6
	'stat'        => 'posts:1',
7
	'new_version' => '1.1',
8
	'max_version' => '1',
9
	'method'      => 'GET',
10
	'path'        => '/sites/%s/posts/%d',
11
	'path_labels' => array(
12
		'$site'    => '(int|string) Site ID or domain',
13
		'$post_ID' => '(int) The post ID',
14
	),
15
16
	'example_request'  => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/7'
17
) );
18
19
new WPCOM_JSON_API_Get_Post_Endpoint( array(
20
	'description' => 'Get a single post (by name)',
21
	'group'       => '__do_not_document',
22
	'stat'        => 'posts:name',
23
	'method'      => 'GET',
24
	'path'        => '/sites/%s/posts/name:%s',
25
	'path_labels' => array(
26
		'$site'      => '(int|string) Site ID or domain',
27
		'$post_name' => '(string) The post name (a.k.a. slug)',
28
	),
29
30
	'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/name:blogging-and-stuff',
31
) );
32
33
new WPCOM_JSON_API_Get_Post_Endpoint( array(
34
	'description' => 'Get a single post (by slug).',
35
	'group'       => 'posts',
36
	'stat'        => 'posts:slug',
37
	'new_version' => '1.1',
38
	'max_version' => '1',
39
	'method'      => 'GET',
40
	'path'        => '/sites/%s/posts/slug:%s',
41
	'path_labels' => array(
42
		'$site'      => '(int|string) Site ID or domain',
43
		'$post_slug' => '(string) The post slug (a.k.a. sanitized name)',
44
	),
45
46
	'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/slug:blogging-and-stuff',
47
) );
48
49
class WPCOM_JSON_API_Get_Post_Endpoint extends WPCOM_JSON_API_Post_Endpoint {
50
	// /sites/%s/posts/%d      -> $blog_id, $post_id
51
	// /sites/%s/posts/name:%s -> $blog_id, $post_id // not documented
52
	// /sites/%s/posts/slug:%s -> $blog_id, $post_id
53
	function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
54
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
55
		if ( is_wp_error( $blog_id ) ) {
56
			return $blog_id;
57
		}
58
59
		$args = $this->query_args();
60
61
		if ( false === strpos( $path, '/posts/slug:' ) && false === strpos( $path, '/posts/name:' ) ) {
62
			$get_by = 'ID';
63
		} else {
64
			$get_by = 'name';
65
		}
66
67
		$return = $this->get_post_by( $get_by, $post_id, $args['context'] );
68
		if ( !$return || is_wp_error( $return ) ) {
69
			return $return;
70
		}
71
72 View Code Duplication
		if ( ! $this->current_user_can_access_post_type( $return['type'], $args['context'] ) ) {
73
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'unknown_post'.

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...
74
		}
75
76
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
77
		do_action( 'wpcom_json_api_objects', 'posts' );
78
79
		return $return;
80
	}
81
}
82