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 | class CMB2_REST_Controller_Boxes extends CMB2_REST_Controller { |
||
|
|
|||
| 18 | |||
| 19 | /** |
||
| 20 | * The base of this controller's route. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $rest_base = 'boxes'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The combined $namespace and $rest_base for these routes. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $namespace_base = ''; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Constructor |
||
| 35 | * @since 2.2.3 |
||
| 36 | */ |
||
| 37 | public function __construct( WP_REST_Server $wp_rest_server ) { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Register the routes for the objects of the controller. |
||
| 44 | * |
||
| 45 | * @since 2.2.3 |
||
| 46 | */ |
||
| 47 | public function register_routes() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Check if a given request has access to get boxes. |
||
| 89 | * |
||
| 90 | * @since 2.2.3 |
||
| 91 | * |
||
| 92 | * @param WP_REST_Request $request Full data about the request. |
||
| 93 | * @return WP_Error|boolean |
||
| 94 | */ |
||
| 95 | public function get_items_permissions_check( $request ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get all public CMB2 boxes. |
||
| 111 | * |
||
| 112 | * @since 2.2.3 |
||
| 113 | * |
||
| 114 | * @param WP_REST_Request $request Full data about the request. |
||
| 115 | * @return WP_Error|WP_REST_Response |
||
| 116 | */ |
||
| 117 | public function get_items( $request ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Check if a given request has access to a box. |
||
| 147 | * By default, no special permissions needed, but filtering return value. |
||
| 148 | * |
||
| 149 | * @since 2.2.3 |
||
| 150 | * |
||
| 151 | * @param WP_REST_Request $request Full details about the request. |
||
| 152 | * @return WP_Error|boolean |
||
| 153 | */ |
||
| 154 | public function get_item_permissions_check( $request ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Check by filter if a given request has access to a box. |
||
| 162 | * By default, no special permissions needed, but filtering return value. |
||
| 163 | * |
||
| 164 | * @since 2.2.3 |
||
| 165 | * |
||
| 166 | * @param bool $can_access Whether the current request has access to view the box by default. |
||
| 167 | * @return WP_Error|boolean |
||
| 168 | */ |
||
| 169 | public function get_item_permissions_check_filter( $can_access = true ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get one CMB2 box from the collection. |
||
| 185 | * |
||
| 186 | * @since 2.2.3 |
||
| 187 | * |
||
| 188 | * @param WP_REST_Request $request Full data about the request. |
||
| 189 | * @return WP_Error|WP_REST_Response |
||
| 190 | */ |
||
| 191 | public function get_item( $request ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get a CMB2 box prepared for REST |
||
| 203 | * |
||
| 204 | * @since 2.2.3 |
||
| 205 | * |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | public function get_rest_box() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return an array of contextual links for box/boxes. |
||
| 242 | * |
||
| 243 | * @since 2.2.3 |
||
| 244 | * |
||
| 245 | * @param CMB2_REST $cmb CMB2_REST object to build links from. |
||
| 246 | * |
||
| 247 | * @return array Array of links |
||
| 248 | */ |
||
| 249 | protected function prepare_links( $cmb ) { |
||
| 269 | |||
| 270 | } |
||
| 271 |
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.