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 |
||
| 17 | abstract class CMB2_REST_Controller extends WP_REST_Controller { |
||
|
|
|||
| 18 | |||
| 19 | /** |
||
| 20 | * The current request object |
||
| 21 | * @var WP_REST_Request $request |
||
| 22 | * @since 2.2.0 |
||
| 23 | */ |
||
| 24 | public $request; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The current server object |
||
| 28 | * @var WP_REST_Server $server |
||
| 29 | * @since 2.2.0 |
||
| 30 | */ |
||
| 31 | public $server; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Box object id |
||
| 35 | * @var mixed |
||
| 36 | * @since 2.2.0 |
||
| 37 | */ |
||
| 38 | public $object_id = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Box object type |
||
| 42 | * @var string |
||
| 43 | * @since 2.2.0 |
||
| 44 | */ |
||
| 45 | public $object_type = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The initial route |
||
| 49 | * @var string |
||
| 50 | * @since 2.2.0 |
||
| 51 | */ |
||
| 52 | protected static $route = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Defines which endpoint the initial request is. |
||
| 56 | * @var string $request_type |
||
| 57 | * @since 2.2.0 |
||
| 58 | */ |
||
| 59 | protected static $request_type = ''; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * @since 2.2.0 |
||
| 64 | */ |
||
| 65 | public function __construct( WP_REST_Server $wp_rest_server ) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Check if a given request has access to a field or box. |
||
| 71 | * By default, no special permissions needed, but filtering return value. |
||
| 72 | * |
||
| 73 | * @since 2.2.0 |
||
| 74 | * |
||
| 75 | * @param WP_REST_Request $request Full details about the request. |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | public function get_item_permissions_check( $request ) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Prepare a CMB2 object for serialization |
||
| 94 | * |
||
| 95 | * @since 2.2.0 |
||
| 96 | * |
||
| 97 | * @param mixed $data |
||
| 98 | * @return array $data |
||
| 99 | */ |
||
| 100 | public function prepare_item( $post ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Output buffers a callback and returns the results. |
||
| 106 | * |
||
| 107 | * @since 2.2.0 |
||
| 108 | * |
||
| 109 | * @param mixed $cb Callable function/method. |
||
| 110 | * @return mixed Results of output buffer after calling function/method. |
||
| 111 | */ |
||
| 112 | public function get_cb_results( $cb ) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Prepare a CMB2 object for serialization |
||
| 123 | * |
||
| 124 | * @since 2.2.0 |
||
| 125 | * |
||
| 126 | * @param mixed $data |
||
| 127 | * @param WP_REST_Request $request Request object |
||
| 128 | * @return array $data |
||
| 129 | */ |
||
| 130 | public function prepare_item_for_response( $data, $request = null ) { |
||
| 144 | |||
| 145 | View Code Duplication | protected function initiate_rest_read_box( $request, $request_type ) { |
|
| 152 | |||
| 153 | View Code Duplication | protected function initiate_rest_write_box( $request, $request_type ) { |
|
| 160 | |||
| 161 | protected function initiate_rest_box( $request, $request_type ) { |
||
| 170 | |||
| 171 | public function initiate_request( $request, $request_type ) { |
||
| 188 | |||
| 189 | public static function get_intial_request_type() { |
||
| 192 | |||
| 193 | public static function get_intial_route() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get CMB2 fields schema, conforming to JSON Schema |
||
| 199 | * |
||
| 200 | * @since 2.2.0 |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | public function get_item_schema() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Return an array of contextual links for endpoint/object |
||
| 233 | * @link http://v2.wp-api.org/extending/linking/ |
||
| 234 | * @link http://www.iana.org/assignments/link-relations/link-relations.xhtml |
||
| 235 | * |
||
| 236 | * @since 2.2.0 |
||
| 237 | * |
||
| 238 | * @param mixed $object Object to build links from. |
||
| 239 | * |
||
| 240 | * @return array Array of links |
||
| 241 | */ |
||
| 242 | abstract protected function prepare_links( $object ); |
||
| 243 | |||
| 244 | } |
||
| 245 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.