Completed
Push — prepare/4.1 ( 0b4dc2...26fe0a )
by Jeremy
279:11 queued 269:16
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 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
D 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
 * WARNING: This file is distributed verbatim in Jetpack.
4
 * There should be nothing WordPress.com specific in this file.
5
 *
6
 * @hide-in-jetpack
7
 */
8
9
class WPCOM_JSON_API_Get_Term_Endpoint extends WPCOM_JSON_API_Endpoint {
10
	// /sites/%s/taxonomies/%s/terms/slug:%s -> $blog_id, $taxonomy, $slug
11
	function callback( $path = '', $blog_id = 0, $taxonomy = 'category', $slug = 0 ) {
12
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
13
		if ( is_wp_error( $blog_id ) ) {
14
			return $blog_id;
15
		}
16
17
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
18
			$this->load_theme_functions();
19
		}
20
21
		$taxonomy_meta = get_taxonomy( $taxonomy );
22 View Code Duplication
		if ( false === $taxonomy_meta || ( ! $taxonomy_meta->public && 
23
				! current_user_can( $taxonomy_meta->cap->assign_terms ) ) ) {
24
			return new WP_Error( 'invalid_taxonomy', 'The taxonomy does not exist', 400 );
25
		}
26
27
		$args = $this->query_args();
28
		$term = $this->get_taxonomy( $slug, $taxonomy, $args['context'] );
29
		if ( ! $term || is_wp_error( $term ) ) {
30
			return $term;
31
		}
32
33
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
34
		do_action( 'wpcom_json_api_objects', 'terms' );
35
36
		return $term;
37
	}
38
}
39