Completed
Pull Request — trunk (#541)
by Justin
07:44
created

CMB2_REST_Controller_Fields   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 23.6 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 9
lcom 1
cbo 2
dl 21
loc 89
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register_routes() 21 21 1
B get_fields() 0 22 5
A get_field() 0 20 3

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
 * Creates CMB2 objects/fields endpoint for WordPres REST API.
4
 * Allows access to fields registered to a specific post type and more.
5
 *
6
 * @todo  Add better documentation.
7
 * @todo  Research proper schema.
8
 *
9
 * @since 2.2.0
10
 *
11
 * @category  WordPress_Plugin
12
 * @package   CMB2
13
 * @author    WebDevStudios
14
 * @license   GPL-2.0+
15
 * @link      http://webdevstudios.com
16
 */
17
class CMB2_REST_Controller_Fields extends CMB2_REST_Controller {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
19
	/**
20
	 * Register the routes for the objects of the controller.
21
	 *
22
	 * @since 2.2.0
23
	 */
24 View Code Duplication
	public function register_routes() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
		// Returns specific box's fields.
26
		register_rest_route( CMB2_REST::BASE, '/boxes/(?P<cmb_id>[\w-]+)/fields/', array(
27
			array(
28
				'methods'         => WP_REST_Server::READABLE,
29
				'callback'        => array( $this, 'get_fields' ),
30
				'permission_callback' => array( $this, 'get_item_permissions_check' ),
31
			),
32
			'schema' => array( $this, 'get_item_schema' ),
33
		) );
34
35
		// Returns specific field data.
36
		register_rest_route( CMB2_REST::BASE, '/boxes/(?P<cmb_id>[\w-]+)/fields/(?P<field_id>[\w-]+)', array(
37
			array(
38
				'methods'         => WP_REST_Server::READABLE,
39
				'callback'        => array( $this, 'get_field' ),
40
				'permission_callback' => array( $this, 'get_item_permissions_check' ),
41
			),
42
			'schema' => array( $this, 'get_item_schema' ),
43
		) );
44
	}
45
46
	/**
47
	 * Get all box fields
48
	 *
49
	 * @since 2.2.0
50
	 *
51
	 * @param WP_REST_Request $request The API request object.
52
	 * @return array
53
	 */
54
	public function get_fields( $request ) {
55
		$this->initiate_request( $request );
56
57
		$cmb_id = $this->request->get_param( 'cmb_id' );
58
59
		if ( $cmb_id && ( $cmb = cmb2_get_metabox( $cmb_id, $this->object_id, $this->object_type ) ) ) {
60
			$fields = array();
61
			foreach ( $cmb->prop( 'fields', array() ) as $field ) {
62
				$field = $this->get_rest_field( $cmb, $field['id'] );
63
64
				if ( ! is_wp_error( $field ) ) {
65
					$fields[ $field['id'] ] = $field;
66
				} else {
67
					$fields[ $field['id'] ] = array( 'error' => $field->get_error_message() );
68
				}
69
			}
70
71
			return $this->prepare_item( $fields );
72
		}
73
74
		return $this->prepare_item( array( 'error' => __( 'No box found by that id.', 'cmb2' ) ) );
75
	}
76
77
	/**
78
	 * Get a specific field
79
	 *
80
	 * @since 2.2.0
81
	 *
82
	 * @param WP_REST_Request $request The API request object.
83
	 * @return array|WP_Error
84
	 */
85
	public function get_field( $request ) {
86
		$this->initiate_request( $request );
87
88
		$cmb = cmb2_get_metabox( $this->request->get_param( 'cmb_id' ), $this->object_id, $this->object_type  );
89
90
		if ( ! $cmb ) {
91
			return $this->prepare_item( array( 'error' => __( 'No box found by that id.', 'cmb2' ) ) );
92
		}
93
94
		$field = $this->get_rest_field( $cmb, $this->request->get_param( 'field_id' ) );
95
96
		if ( is_wp_error( $field ) ) {
97
			return $this->prepare_item( array( 'error' => $field->get_error_message() ) );
98
		}
99
100
		// wp_die( '<xmp>$field: '. print_r( $field, true ) .'</xmp>' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
		// echo '<xmp>$field: '. print_r( $field, true ) .'</xmp>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
		// $field = $this->prepare_item( $field );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
		return $this->prepare_item( $field );
104
	}
105
}
106