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:
Complex classes like CMB2_REST_Controller_Fields often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CMB2_REST_Controller_Fields, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class CMB2_REST_Controller_Fields extends CMB2_REST_Controller_Boxes { |
||
|
|
|||
| 18 | |||
| 19 | /** |
||
| 20 | * Register the routes for the objects of the controller. |
||
| 21 | * |
||
| 22 | * @since 2.2.3 |
||
| 23 | */ |
||
| 24 | public function register_routes() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Check if a given request has access to get fields. |
||
| 82 | * By default, no special permissions needed, but filtering return value. |
||
| 83 | * |
||
| 84 | * @since 2.2.3 |
||
| 85 | * |
||
| 86 | * @param WP_REST_Request $request Full data about the request. |
||
| 87 | * @return WP_Error|boolean |
||
| 88 | */ |
||
| 89 | public function get_items_permissions_check( $request ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get all public CMB2 box fields. |
||
| 106 | * |
||
| 107 | * @since 2.2.3 |
||
| 108 | * |
||
| 109 | * @param WP_REST_Request $request Full data about the request. |
||
| 110 | * @return WP_Error|WP_REST_Response |
||
| 111 | */ |
||
| 112 | public function get_items( $request ) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Check if a given request has access to a field. |
||
| 141 | * By default, no special permissions needed, but filtering return value. |
||
| 142 | * |
||
| 143 | * @since 2.2.3 |
||
| 144 | * |
||
| 145 | * @param WP_REST_Request $request Full details about the request. |
||
| 146 | * @return WP_Error|boolean |
||
| 147 | */ |
||
| 148 | public function get_item_permissions_check( $request ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Check by filter if a given request has access to a field. |
||
| 159 | * By default, no special permissions needed, but filtering return value. |
||
| 160 | * |
||
| 161 | * @since 2.2.3 |
||
| 162 | * |
||
| 163 | * @param bool $can_access Whether the current request has access to view the field by default. |
||
| 164 | * @return WP_Error|boolean |
||
| 165 | */ |
||
| 166 | public function get_item_permissions_check_filter( $can_access = true ) { |
||
| 167 | /** |
||
| 168 | * By default, no special permissions needed. |
||
| 169 | * |
||
| 170 | * @since 2.2.3 |
||
| 171 | * |
||
| 172 | * @param bool $can_access Whether this CMB2 endpoint can be accessed. |
||
| 173 | * @param object $controller This CMB2_REST_Controller object. |
||
| 174 | */ |
||
| 175 | return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_get_field_permissions_check', $can_access ); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get one CMB2 field from the collection. |
||
| 180 | * |
||
| 181 | * @since 2.2.3 |
||
| 182 | * |
||
| 183 | * @param WP_REST_Request $request Full data about the request. |
||
| 184 | * @return WP_Error|WP_REST_Response |
||
| 185 | */ |
||
| 186 | public function get_item( $request ) { |
||
| 187 | $this->initiate_rest_read_box( $request, 'field_read' ); |
||
| 188 | |||
| 189 | if ( is_wp_error( $this->rest_box ) ) { |
||
| 190 | return $this->rest_box; |
||
| 191 | } |
||
| 192 | |||
| 193 | return $this->prepare_read_field( $this->request->get_param( 'field_id' ) ); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Check if a given request has access to update a field value. |
||
| 198 | * By default, requires 'edit_others_posts' capability, but filtering return value. |
||
| 199 | * |
||
| 200 | * @since 2.2.3 |
||
| 201 | * |
||
| 202 | * @param WP_REST_Request $request Full details about the request. |
||
| 203 | * @return WP_Error|boolean |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function update_item_permissions_check( $request ) { |
|
| 206 | $this->initiate_rest_read_box( $request, 'field_value_update' ); |
||
| 207 | if ( ! is_wp_error( $this->rest_box ) ) { |
||
| 208 | $this->field = $this->rest_box->field_can_edit( $this->request->get_param( 'field_id' ), true ); |
||
| 209 | } |
||
| 210 | |||
| 211 | $can_update = current_user_can( 'edit_others_posts' ); |
||
| 212 | |||
| 213 | /** |
||
| 214 | * By default, 'edit_others_posts' is required capability. |
||
| 215 | * |
||
| 216 | * @since 2.2.3 |
||
| 217 | * |
||
| 218 | * @param bool $can_update Whether this CMB2 endpoint can be accessed. |
||
| 219 | * @param object $controller This CMB2_REST_Controller object. |
||
| 220 | */ |
||
| 221 | return $this->maybe_hook_callback_and_apply_filters( 'cmb2_api_update_field_value_permissions_check', $can_update ); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Update CMB2 field value. |
||
| 226 | * |
||
| 227 | * @since 2.2.3 |
||
| 228 | * |
||
| 229 | * @param WP_REST_Request $request Full data about the request. |
||
| 230 | * @return WP_Error|WP_REST_Response |
||
| 231 | */ |
||
| 232 | public function update_item( $request ) { |
||
| 233 | $this->initiate_rest_read_box( $request, 'field_value_update' ); |
||
| 234 | |||
| 235 | if ( ! $this->request['value'] ) { |
||
| 236 | return new WP_Error( 'cmb2_rest_update_field_error', __( 'CMB2 Field value cannot be updated without the value parameter specified.', 'cmb2' ), array( |
||
| 237 | 'status' => 400, |
||
| 238 | ) ); |
||
| 239 | } |
||
| 240 | |||
| 241 | return $this->modify_field_value( 'updated' ); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Check if a given request has access to delete a field value. |
||
| 246 | * By default, requires 'delete_others_posts' capability, but filtering return value. |
||
| 247 | * |
||
| 248 | * @since 2.2.3 |
||
| 249 | * |
||
| 250 | * @param WP_REST_Request $request Full details about the request. |
||
| 251 | * @return WP_Error|boolean |
||
| 252 | */ |
||
| 253 | View Code Duplication | public function delete_item_permissions_check( $request ) { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Delete CMB2 field value. |
||
| 274 | * |
||
| 275 | * @since 2.2.3 |
||
| 276 | * |
||
| 277 | * @param WP_REST_Request $request Full data about the request. |
||
| 278 | * @return WP_Error|WP_REST_Response |
||
| 279 | */ |
||
| 280 | public function delete_item( $request ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Modify CMB2 field value. |
||
| 288 | * |
||
| 289 | * @since 2.2.3 |
||
| 290 | * |
||
| 291 | * @param string $activity The modification activity (updated or deleted). |
||
| 292 | * @return WP_Error|WP_REST_Response |
||
| 293 | */ |
||
| 294 | public function modify_field_value( $activity ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get a response object for a specific field ID. |
||
| 331 | * |
||
| 332 | * @since 2.2.3 |
||
| 333 | * |
||
| 334 | * @param string\CMB2_Field Field id or Field object. |
||
| 335 | * @return WP_Error|WP_REST_Response |
||
| 336 | */ |
||
| 337 | public function prepare_read_field( $field ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get a specific field response. |
||
| 351 | * |
||
| 352 | * @since 2.2.3 |
||
| 353 | * |
||
| 354 | * @param CMB2_Field Field object. |
||
| 355 | * @return array Response array. |
||
| 356 | */ |
||
| 357 | public function prepare_field_response() { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Prepare the field data array for JSON. |
||
| 368 | * |
||
| 369 | * @since 2.2.3 |
||
| 370 | * |
||
| 371 | * @param CMB2_Field $field field object. |
||
| 372 | * |
||
| 373 | * @return array Array of field data. |
||
| 374 | */ |
||
| 375 | protected function prepare_field_data( CMB2_Field $field ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Return an array of contextual links for field/fields. |
||
| 428 | * |
||
| 429 | * @since 2.2.3 |
||
| 430 | * |
||
| 431 | * @param CMB2_Field $field Field object to build links from. |
||
| 432 | * |
||
| 433 | * @return array Array of links |
||
| 434 | */ |
||
| 435 | protected function prepare_links( $field ) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Checks if the CMB2 box or field has any registered callback parameters for the given filter. |
||
| 457 | * |
||
| 458 | * The registered handlers will have a property name which matches the filter, except: |
||
| 459 | * - The 'cmb2_api' prefix will be removed |
||
| 460 | * - A '_cb' suffix will be added (to stay inline with other '*_cb' parameters). |
||
| 461 | * |
||
| 462 | * @since 2.2.3 |
||
| 463 | * |
||
| 464 | * @param string $filter The filter name. |
||
| 465 | * @param bool $default_val The default filter value. |
||
| 466 | * |
||
| 467 | * @return bool The possibly-modified filter value (if the _cb param is a non-callable). |
||
| 468 | */ |
||
| 469 | public function maybe_hook_registered_callback( $filter, $default_val ) { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Unhooks any CMB2 box or field registered callback parameters for the given filter. |
||
| 486 | * |
||
| 487 | * @since 2.2.3 |
||
| 488 | * |
||
| 489 | * @param string $filter The filter name. |
||
| 490 | * |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | public function maybe_unhook_registered_callback( $filter ) { |
||
| 501 | |||
| 502 | } |
||
| 503 |
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.