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 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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class CMB2_REST extends CMB2_Hookup_Base { |
||
|
|
|||
| 15 | |||
| 16 | /** |
||
| 17 | * The current CMB2 REST endpoint version |
||
| 18 | * @var string |
||
| 19 | * @since 2.2.4 |
||
| 20 | */ |
||
| 21 | const VERSION = '1'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The CMB2 REST base namespace (v should always be followed by $version) |
||
| 25 | * @var string |
||
| 26 | * @since 2.2.4 |
||
| 27 | */ |
||
| 28 | const NAME_SPACE = 'cmb2/v1'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var CMB2 object |
||
| 32 | * @since 2.2.4 |
||
| 33 | */ |
||
| 34 | public $cmb; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var CMB2_REST[] objects |
||
| 38 | * @since 2.2.4 |
||
| 39 | */ |
||
| 40 | protected static $boxes = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array Array of cmb ids for each type. |
||
| 44 | * @since 2.2.4 |
||
| 45 | */ |
||
| 46 | protected static $type_boxes = array( |
||
| 47 | 'post' => array(), |
||
| 48 | 'user' => array(), |
||
| 49 | 'comment' => array(), |
||
| 50 | 'term' => array(), |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Array of readable field objects. |
||
| 55 | * @var CMB2_Field[] |
||
| 56 | * @since 2.2.4 |
||
| 57 | */ |
||
| 58 | protected $read_fields = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Array of editable field objects. |
||
| 62 | * @var CMB2_Field[] |
||
| 63 | * @since 2.2.4 |
||
| 64 | */ |
||
| 65 | protected $edit_fields = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * whether CMB2 object is readable via the rest api. |
||
| 69 | * @var boolean |
||
| 70 | */ |
||
| 71 | protected $rest_read = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * whether CMB2 object is editable via the rest api. |
||
| 75 | * @var boolean |
||
| 76 | */ |
||
| 77 | protected $rest_edit = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor |
||
| 81 | * |
||
| 82 | * @since 2.2.4 |
||
| 83 | * |
||
| 84 | * @param CMB2 $cmb The CMB2 object to be registered for the API. |
||
| 85 | */ |
||
| 86 | 38 | public function __construct( CMB2 $cmb ) { |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Hooks to register on frontend and backend. |
||
| 98 | * |
||
| 99 | * @since 2.2.4 |
||
| 100 | * |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | 38 | public function universal_hooks() { |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Initiate the CMB2 Boxes and Fields routes |
||
| 118 | * |
||
| 119 | * @since 2.2.4 |
||
| 120 | * |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | 1 | public static function init_routes() { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Loop through REST boxes and call register_rest_field for each object type. |
||
| 135 | * |
||
| 136 | * @since 2.2.4 |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | 38 | public static function register_cmb2_fields() { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Wrapper for register_rest_field. |
||
| 194 | * |
||
| 195 | * @since 2.2.4 |
||
| 196 | * |
||
| 197 | * @param string|array $object_types Object(s) the field is being registered |
||
| 198 | * to, "post"|"term"|"comment" etc. |
||
| 199 | * @param string $object_types Canonical object type for callbacks. |
||
| 200 | * |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | 38 | protected static function register_rest_field( $object_types, $object_type ) { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Setup readable and editable fields. |
||
| 213 | * |
||
| 214 | * @since 2.2.4 |
||
| 215 | * |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | 38 | protected function declare_read_edit_fields() { |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Determines if a field is readable based on it's show_in_rest value |
||
| 239 | * and the box's show_in_rest value. |
||
| 240 | * |
||
| 241 | * @since 2.2.4 |
||
| 242 | * |
||
| 243 | * @param bool $show_in_rest Field's show_in_rest value. Default null. |
||
| 244 | * |
||
| 245 | * @return bool Whether field is readable. |
||
| 246 | */ |
||
| 247 | 38 | protected function can_read( $show_in_rest ) { |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Determines if a field is editable based on it's show_in_rest value |
||
| 259 | * and the box's show_in_rest value. |
||
| 260 | * |
||
| 261 | * @since 2.2.4 |
||
| 262 | * |
||
| 263 | * @param bool $show_in_rest Field's show_in_rest value. Default null. |
||
| 264 | * |
||
| 265 | * @return bool Whether field is editable. |
||
| 266 | */ |
||
| 267 | 38 | protected function can_edit( $show_in_rest ) { |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Handler for getting post custom field data. |
||
| 279 | * |
||
| 280 | * @since 2.2.4 |
||
| 281 | * |
||
| 282 | * @param array $object The object data from the response |
||
| 283 | * @param string $field_name Name of field |
||
| 284 | * @param WP_REST_Request $request Current request |
||
| 285 | * @param string $object_type The request object type |
||
| 286 | * |
||
| 287 | * @return mixed |
||
| 288 | */ |
||
| 289 | 4 | public static function get_post_rest_values( $object, $field_name, $request, $object_type ) { |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Handler for getting user custom field data. |
||
| 297 | * |
||
| 298 | * @since 2.2.4 |
||
| 299 | * |
||
| 300 | * @param array $object The object data from the response |
||
| 301 | * @param string $field_name Name of field |
||
| 302 | * @param WP_REST_Request $request Current request |
||
| 303 | * @param string $object_type The request object type |
||
| 304 | * |
||
| 305 | * @return mixed |
||
| 306 | */ |
||
| 307 | 2 | public static function get_user_rest_values( $object, $field_name, $request, $object_type ) { |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Handler for getting comment custom field data. |
||
| 315 | * |
||
| 316 | * @since 2.2.4 |
||
| 317 | * |
||
| 318 | * @param array $object The object data from the response |
||
| 319 | * @param string $field_name Name of field |
||
| 320 | * @param WP_REST_Request $request Current request |
||
| 321 | * @param string $object_type The request object type |
||
| 322 | * |
||
| 323 | * @return mixed |
||
| 324 | */ |
||
| 325 | 2 | public static function get_comment_rest_values( $object, $field_name, $request, $object_type ) { |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Handler for getting term custom field data. |
||
| 333 | * |
||
| 334 | * @since 2.2.4 |
||
| 335 | * |
||
| 336 | * @param array $object The object data from the response |
||
| 337 | * @param string $field_name Name of field |
||
| 338 | * @param WP_REST_Request $request Current request |
||
| 339 | * @param string $object_type The request object type |
||
| 340 | * |
||
| 341 | * @return mixed |
||
| 342 | */ |
||
| 343 | 2 | public static function get_term_rest_values( $object, $field_name, $request, $object_type ) { |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Handler for getting custom field data. |
||
| 351 | * |
||
| 352 | * @since 2.2.4 |
||
| 353 | * |
||
| 354 | * @param array $object The object data from the response |
||
| 355 | * @param WP_REST_Request $request Current request |
||
| 356 | * @param string $object_type The request object type |
||
| 357 | * @param string $main_object_type The cmb main object type |
||
| 358 | * |
||
| 359 | * @return mixed |
||
| 360 | */ |
||
| 361 | 10 | protected static function get_rest_values( $object, $request, $object_type, $main_object_type = 'post' ) { |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Handler for updating post custom field data. |
||
| 391 | * |
||
| 392 | * @since 2.2.4 |
||
| 393 | * |
||
| 394 | * @param mixed $value The value of the field |
||
| 395 | * @param object $object The object from the response |
||
| 396 | * @param string $field_name Name of field |
||
| 397 | * @param WP_REST_Request $request Current request |
||
| 398 | * @param string $object_type The request object type |
||
| 399 | * |
||
| 400 | * @return bool|int |
||
| 401 | */ |
||
| 402 | 2 | public static function update_post_rest_values( $values, $object, $field_name, $request, $object_type ) { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Handler for updating user custom field data. |
||
| 410 | * |
||
| 411 | * @since 2.2.4 |
||
| 412 | * |
||
| 413 | * @param mixed $value The value of the field |
||
| 414 | * @param object $object The object from the response |
||
| 415 | * @param string $field_name Name of field |
||
| 416 | * @param WP_REST_Request $request Current request |
||
| 417 | * @param string $object_type The request object type |
||
| 418 | * |
||
| 419 | * @return bool|int |
||
| 420 | */ |
||
| 421 | 1 | public static function update_user_rest_values( $values, $object, $field_name, $request, $object_type ) { |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Handler for updating comment custom field data. |
||
| 429 | * |
||
| 430 | * @since 2.2.4 |
||
| 431 | * |
||
| 432 | * @param mixed $value The value of the field |
||
| 433 | * @param object $object The object from the response |
||
| 434 | * @param string $field_name Name of field |
||
| 435 | * @param WP_REST_Request $request Current request |
||
| 436 | * @param string $object_type The request object type |
||
| 437 | * |
||
| 438 | * @return bool|int |
||
| 439 | */ |
||
| 440 | 1 | public static function update_comment_rest_values( $values, $object, $field_name, $request, $object_type ) { |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Handler for updating term custom field data. |
||
| 448 | * |
||
| 449 | * @since 2.2.4 |
||
| 450 | * |
||
| 451 | * @param mixed $value The value of the field |
||
| 452 | * @param object $object The object from the response |
||
| 453 | * @param string $field_name Name of field |
||
| 454 | * @param WP_REST_Request $request Current request |
||
| 455 | * @param string $object_type The request object type |
||
| 456 | * |
||
| 457 | * @return bool|int |
||
| 458 | */ |
||
| 459 | 1 | public static function update_term_rest_values( $values, $object, $field_name, $request, $object_type ) { |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Handler for updating custom field data. |
||
| 467 | * |
||
| 468 | * @since 2.2.4 |
||
| 469 | * |
||
| 470 | * @param mixed $value The value of the field |
||
| 471 | * @param object $object The object from the response |
||
| 472 | * @param WP_REST_Request $request Current request |
||
| 473 | * @param string $object_type The request object type |
||
| 474 | * @param string $main_object_type The cmb main object type |
||
| 475 | * |
||
| 476 | * @return bool|int |
||
| 477 | */ |
||
| 478 | 5 | protected static function update_rest_values( $values, $object, $request, $object_type, $main_object_type = 'post' ) { |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Loop through box fields and sanitize the values. |
||
| 511 | * |
||
| 512 | * @since 2.2.o |
||
| 513 | * |
||
| 514 | * @param array $values Array of values being provided. |
||
| 515 | * @return array Array of updated/sanitized values. |
||
| 516 | */ |
||
| 517 | 5 | public function sanitize_box_values( array $values ) { |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Handles returning a sanitized field value. |
||
| 533 | * |
||
| 534 | * @since 2.2.4 |
||
| 535 | * |
||
| 536 | * @param array $values Array of values being provided. |
||
| 537 | * @param string $field_id The id of the field to update. |
||
| 538 | * |
||
| 539 | * @return mixed The results of saving/sanitizing a field value. |
||
| 540 | */ |
||
| 541 | 5 | protected function sanitize_field_value( array $values, $field_id ) { |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Handles returning a sanitized group field value. |
||
| 564 | * |
||
| 565 | * @since 2.2.4 |
||
| 566 | * |
||
| 567 | * @param array $values Array of values being provided. |
||
| 568 | * @param CMB2_Field $field CMB2_Field object. |
||
| 569 | * |
||
| 570 | * @return mixed The results of saving/sanitizing the group field value. |
||
| 571 | */ |
||
| 572 | protected function sanitize_group_value( array $values, CMB2_Field $field ) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Filter whether a meta key is protected. |
||
| 585 | * |
||
| 586 | * @since 2.2.4 |
||
| 587 | * |
||
| 588 | * @param bool $protected Whether the key is protected. Default false. |
||
| 589 | * @param string $meta_key Meta key. |
||
| 590 | * @param string $meta_type Meta type. |
||
| 591 | */ |
||
| 592 | public function is_protected_meta( $protected, $meta_key, $meta_type ) { |
||
| 599 | |||
| 600 | 6 | protected static function get_object_id( $object, $object_type = 'post' ) { |
|
| 621 | |||
| 622 | 6 | public function field_can_read( $field_id, $return_object = false ) { |
|
| 625 | |||
| 626 | 4 | public function field_can_edit( $field_id, $return_object = false ) { |
|
| 629 | |||
| 630 | 8 | protected function field_can( $type = 'read_fields', $field_id, $return_object = false ) { |
|
| 637 | |||
| 638 | /** |
||
| 639 | * Get a CMB2_REST instance object from the registry by a CMB2 id. |
||
| 640 | * |
||
| 641 | * @since 2.2.4 |
||
| 642 | * |
||
| 643 | * @param string $cmb_id CMB2 config id |
||
| 644 | * |
||
| 645 | * @return CMB2_REST|false The CMB2_REST object or false. |
||
| 646 | */ |
||
| 647 | 13 | public static function get_rest_box( $cmb_id ) { |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Remove a CMB2_REST instance object from the registry. |
||
| 653 | * |
||
| 654 | * @since 2.2.4 |
||
| 655 | * |
||
| 656 | * @param string $cmb_id A CMB2 instance id. |
||
| 657 | */ |
||
| 658 | public static function remove( $cmb_id ) { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Retrieve all CMB2_REST instances from the registry. |
||
| 666 | * |
||
| 667 | * @since 2.2.4 |
||
| 668 | * @return CMB2[] Array of all registered CMB2_REST instances. |
||
| 669 | */ |
||
| 670 | 38 | public static function get_all() { |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Checks if given value is readable. |
||
| 676 | * |
||
| 677 | * Value is considered readable if it is not empty and if it does not match the editable blacklist. |
||
| 678 | * |
||
| 679 | * @since 2.2.4 |
||
| 680 | * |
||
| 681 | * @param mixed $value Value to check. |
||
| 682 | * |
||
| 683 | * @return boolean Whether value is considered readable. |
||
| 684 | */ |
||
| 685 | 38 | public static function is_readable( $value ) { |
|
| 692 | |||
| 693 | /** |
||
| 694 | * Checks if given value is editable. |
||
| 695 | * |
||
| 696 | * Value is considered editable if matches the editable whitelist. |
||
| 697 | * |
||
| 698 | * @since 2.2.4 |
||
| 699 | * |
||
| 700 | * @param mixed $value Value to check. |
||
| 701 | * |
||
| 702 | * @return boolean Whether value is considered editable. |
||
| 703 | */ |
||
| 704 | 38 | public static function is_editable( $value ) { |
|
| 710 | |||
| 711 | /** |
||
| 712 | * Magic getter for our object. |
||
| 713 | * |
||
| 714 | * @param string $field |
||
| 715 | * @throws Exception Throws an exception if the field is invalid. |
||
| 716 | * |
||
| 717 | * @return mixed |
||
| 718 | */ |
||
| 719 | 12 | public function __get( $field ) { |
|
| 730 | |||
| 731 | } |
||
| 732 |
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.