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 ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get one CMB2 field from the collection. |
||
| 182 | * |
||
| 183 | * @since 2.2.3 |
||
| 184 | * |
||
| 185 | * @param WP_REST_Request $request Full data about the request. |
||
| 186 | * @return WP_Error|WP_REST_Response |
||
| 187 | */ |
||
| 188 | public function get_item( $request ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Check if a given request has access to update a field value. |
||
| 200 | * By default, requires 'edit_others_posts' capability, but filtering return value. |
||
| 201 | * |
||
| 202 | * @since 2.2.3 |
||
| 203 | * |
||
| 204 | * @param WP_REST_Request $request Full details about the request. |
||
| 205 | * @return WP_Error|boolean |
||
| 206 | */ |
||
| 207 | View Code Duplication | public function update_item_permissions_check( $request ) { |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Update CMB2 field value. |
||
| 228 | * |
||
| 229 | * @since 2.2.3 |
||
| 230 | * |
||
| 231 | * @param WP_REST_Request $request Full data about the request. |
||
| 232 | * @return WP_Error|WP_REST_Response |
||
| 233 | */ |
||
| 234 | public function update_item( $request ) { |
||
| 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) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get a response object for a specific field ID. |
||
| 327 | * |
||
| 328 | * @since 2.2.3 |
||
| 329 | * |
||
| 330 | * @param string\CMB2_Field Field id or Field object. |
||
| 331 | * @return array|WP_Error Response array or WP_Error object. |
||
| 332 | */ |
||
| 333 | public function prepare_read_field( $field ) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get a specific field response. |
||
| 345 | * |
||
| 346 | * @since 2.2.3 |
||
| 347 | * |
||
| 348 | * @param CMB2_Field Field object. |
||
| 349 | * @return array Response array. |
||
| 350 | */ |
||
| 351 | public function prepare_field_response() { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Prepare the field data array for JSON. |
||
| 362 | * |
||
| 363 | * @since 2.2.3 |
||
| 364 | * |
||
| 365 | * @param CMB2_Field $field field object. |
||
| 366 | * |
||
| 367 | * @return array Array of field data. |
||
| 368 | */ |
||
| 369 | protected function prepare_field_data( CMB2_Field $field ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Return an array of contextual links for field/fields. |
||
| 422 | * |
||
| 423 | * @since 2.2.3 |
||
| 424 | * |
||
| 425 | * @param CMB2_Field $field Field object to build links from. |
||
| 426 | * |
||
| 427 | * @return array Array of links |
||
| 428 | */ |
||
| 429 | protected function prepare_links( $field ) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Checks if the CMB2 box or field has any registered callback parameters for the given filter. |
||
| 451 | * |
||
| 452 | * The registered handlers will have a property name which matches the filter, except: |
||
| 453 | * - The 'cmb2_api' prefix will be removed |
||
| 454 | * - A '_cb' suffix will be added (to stay inline with other '*_cb' parameters). |
||
| 455 | * |
||
| 456 | * @since 2.2.3 |
||
| 457 | * |
||
| 458 | * @param string $filter The filter name. |
||
| 459 | * @param bool $default_val The default filter value. |
||
| 460 | * |
||
| 461 | * @return bool The possibly-modified filter value (if the _cb param is a non-callable). |
||
| 462 | */ |
||
| 463 | public function maybe_hook_registered_callback( $filter, $default_val ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Unhooks any CMB2 box or field registered callback parameters for the given filter. |
||
| 480 | * |
||
| 481 | * @since 2.2.3 |
||
| 482 | * |
||
| 483 | * @param string $filter The filter name. |
||
| 484 | * |
||
| 485 | * @return void |
||
| 486 | */ |
||
| 487 | public function maybe_unhook_registered_callback( $filter ) { |
||
| 495 | |||
| 496 | } |
||
| 497 |
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.