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

WPCOM_JSON_API_Get_Term_Endpoint   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 30
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 4
loc 30
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B callback() 4 27 9

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_Term_Endpoint( array(
4
	'description' => 'Get information about a single term.',
5
	'group'       => 'taxonomy',
6
	'stat'        => 'terms:1',
7
	'method'      => 'GET',
8
	'path'        => '/sites/%s/taxonomies/%s/terms/slug:%s',
9
	'path_labels' => array(
10
		'$site'     => '(int|string) Site ID or domain',
11
		'$taxonomy' => '(string) Taxonomy',
12
		'$slug'     => '(string) Term slug',
13
	),
14
	'response_format' => array(
15
		'ID'          => '(int) The term ID.',
16
		'name'        => '(string) The name of the term.',
17
		'slug'        => '(string) The slug of the term.',
18
		'description' => '(string) The description of the term.',
19
		'post_count'  => '(int) The number of posts using this term.',
20
		'parent'      => '(int) The parent ID for the term, if hierarchical.',
21
	),
22
	'example_request'  => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/taxonomies/post_tag/terms/slug:wordpresscom'
23
) );
24
25
class WPCOM_JSON_API_Get_Term_Endpoint extends WPCOM_JSON_API_Endpoint {
26
	// /sites/%s/taxonomies/%s/terms/slug:%s -> $blog_id, $taxonomy, $slug
27
	function callback( $path = '', $blog_id = 0, $taxonomy = 'category', $slug = 0 ) {
28
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
29
		if ( is_wp_error( $blog_id ) ) {
30
			return $blog_id;
31
		}
32
33
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
34
			$this->load_theme_functions();
35
		}
36
37
		$taxonomy_meta = get_taxonomy( $taxonomy );
38 View Code Duplication
		if ( false === $taxonomy_meta || ( ! $taxonomy_meta->public && 
39
				! current_user_can( $taxonomy_meta->cap->assign_terms ) ) ) {
40
			return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_taxonomy'.

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...
41
		}
42
43
		$args = $this->query_args();
44
		$term = $this->get_taxonomy( $slug, $taxonomy, $args['context'] );
45
		if ( ! $term || is_wp_error( $term ) ) {
46
			return $term;
47
		}
48
49
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
50
		do_action( 'wpcom_json_api_objects', 'terms' );
51
52
		return $term;
53
	}
54
}
55