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 | public static $boxes; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Array of readable field objects. |
||
| 44 | * @var CMB2_Field[] |
||
| 45 | * @since 2.2.4 |
||
| 46 | */ |
||
| 47 | protected $read_fields = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Array of editable field objects. |
||
| 51 | * @var CMB2_Field[] |
||
| 52 | * @since 2.2.4 |
||
| 53 | */ |
||
| 54 | protected $edit_fields = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * whether CMB2 object is readable via the rest api. |
||
| 58 | * @var boolean |
||
| 59 | */ |
||
| 60 | protected $rest_read = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * whether CMB2 object is editable via the rest api. |
||
| 64 | * @var boolean |
||
| 65 | */ |
||
| 66 | protected $rest_edit = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Constructor |
||
| 70 | * |
||
| 71 | * @since 2.2.4 |
||
| 72 | * |
||
| 73 | * @param CMB2 $cmb The CMB2 object to be registered for the API. |
||
| 74 | */ |
||
| 75 | public function __construct( CMB2 $cmb ) { |
||
| 84 | |||
| 85 | public function universal_hooks() { |
||
| 97 | |||
| 98 | public function init_routes() { |
||
| 107 | |||
| 108 | public static function register_appended_fields() { |
||
| 126 | |||
| 127 | protected function declare_read_edit_fields() { |
||
| 145 | |||
| 146 | protected function can_read( $show_in_rest ) { |
||
| 155 | |||
| 156 | protected function can_edit( $show_in_rest ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Handler for getting custom field data. |
||
| 168 | * |
||
| 169 | * @since 2.2.4 |
||
| 170 | * |
||
| 171 | * @param array $data The data from the response |
||
| 172 | * @param string $field_name Name of field |
||
| 173 | * @param WP_REST_Request $request Current request |
||
| 174 | * |
||
| 175 | * @return mixed |
||
| 176 | */ |
||
| 177 | public static function get_restable_field_values( $data, $field_name, $request ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Handler for updating custom field data. |
||
| 202 | * |
||
| 203 | * @since 2.2.4 |
||
| 204 | * |
||
| 205 | * @param mixed $value The value of the field |
||
| 206 | * @param object $object The object from the response |
||
| 207 | * @param string $field_name Name of field |
||
| 208 | * |
||
| 209 | * @return bool|int |
||
| 210 | */ |
||
| 211 | public static function update_restable_field_values( $values, $object, $field_name ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Loop through box fields and sanitize the values. |
||
| 240 | * |
||
| 241 | * @since 2.2.o |
||
| 242 | * |
||
| 243 | * @param array $values Array of values being provided. |
||
| 244 | * @return array Array of updated/sanitized values. |
||
| 245 | */ |
||
| 246 | public function sanitize_box_values( array $values ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Handles returning a sanitized field value. |
||
| 262 | * |
||
| 263 | * @since 2.2.4 |
||
| 264 | * |
||
| 265 | * @param array $values Array of values being provided. |
||
| 266 | * @param string $field_id The id of the field to update. |
||
| 267 | * |
||
| 268 | * @return mixed The results of saving/sanitizing a field value. |
||
| 269 | */ |
||
| 270 | protected function sanitize_field_value( array $values, $field_id ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Handles returning a sanitized group field value. |
||
| 293 | * |
||
| 294 | * @since 2.2.4 |
||
| 295 | * |
||
| 296 | * @param array $values Array of values being provided. |
||
| 297 | * @param CMB2_Field $field CMB2_Field object. |
||
| 298 | * |
||
| 299 | * @return mixed The results of saving/sanitizing the group field value. |
||
| 300 | */ |
||
| 301 | protected function sanitize_group_value( array $values, CMB2_Field $field ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Filter whether a meta key is protected. |
||
| 314 | * |
||
| 315 | * @since 2.2.4 |
||
| 316 | * |
||
| 317 | * @param bool $protected Whether the key is protected. Default false. |
||
| 318 | * @param string $meta_key Meta key. |
||
| 319 | * @param string $meta_type Meta type. |
||
| 320 | */ |
||
| 321 | public function is_protected_meta( $protected, $meta_key, $meta_type ) { |
||
| 328 | |||
| 329 | protected static function get_object_data( $object ) { |
||
| 351 | |||
| 352 | public function field_can_read( $field_id, $return_object = false ) { |
||
| 355 | |||
| 356 | public function field_can_edit( $field_id, $return_object = false ) { |
||
| 359 | |||
| 360 | protected function field_can( $type = 'read_fields', $field_id, $return_object = false ) { |
||
| 361 | if ( ! in_array( $field_id instanceof CMB2_Field ? $field_id->id() : $field_id, $this->{$type}, true ) ) { |
||
| 362 | return false; |
||
| 363 | } |
||
| 364 | |||
| 365 | return $return_object ? $this->cmb->get_field( $field_id ) : true; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get an instance of this class by a CMB2 id |
||
| 370 | * |
||
| 371 | * @since 2.2.4 |
||
| 372 | * |
||
| 373 | * @param string $cmb_id CMB2 config id |
||
| 374 | * |
||
| 375 | * @return CMB2_REST|false The CMB2_REST object or false. |
||
| 376 | */ |
||
| 377 | public static function get_rest_box( $cmb_id ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Checks if given value is readable. |
||
| 383 | * |
||
| 384 | * Value is considered readable if it is not empty and if it does not match the editable blacklist. |
||
| 385 | * |
||
| 386 | * @since 2.2.4 |
||
| 387 | * |
||
| 388 | * @param mixed $value Value to check. |
||
| 389 | * |
||
| 390 | * @return boolean Whether value is considered readable. |
||
| 391 | */ |
||
| 392 | public static function is_readable( $value ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Checks if given value is editable. |
||
| 402 | * |
||
| 403 | * Value is considered editable if matches the editable whitelist. |
||
| 404 | * |
||
| 405 | * @since 2.2.4 |
||
| 406 | * |
||
| 407 | * @param mixed $value Value to check. |
||
| 408 | * |
||
| 409 | * @return boolean Whether value is considered editable. |
||
| 410 | */ |
||
| 411 | public static function is_editable( $value ) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Magic getter for our object. |
||
| 420 | * |
||
| 421 | * @param string $field |
||
| 422 | * @throws Exception Throws an exception if the field is invalid. |
||
| 423 | * |
||
| 424 | * @return mixed |
||
| 425 | */ |
||
| 426 | public function __get( $field ) { |
||
| 437 | |||
| 438 | } |
||
| 439 |
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.