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 NAMESPACE = '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 writeable field objects. |
||
| 51 | * @var CMB2_Field[] |
||
| 52 | * @since 2.2.4 |
||
| 53 | */ |
||
| 54 | protected $write_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 writeable via the rest api. |
||
| 64 | * @var boolean |
||
| 65 | */ |
||
| 66 | protected $rest_write = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Constructor |
||
| 70 | * @since 2.2.4 |
||
| 71 | * @param CMB2 $cmb The CMB2 object to be registered for the API. |
||
| 72 | */ |
||
| 73 | public function __construct( CMB2 $cmb ) { |
||
| 81 | |||
| 82 | public function universal_hooks() { |
||
| 94 | |||
| 95 | public function init_routes() { |
||
| 104 | |||
| 105 | public static function register_appended_fields() { |
||
| 123 | |||
| 124 | protected function declare_read_write_fields() { |
||
| 142 | |||
| 143 | protected function can_read( $show_in_rest ) { |
||
| 148 | |||
| 149 | protected function can_write( $show_in_rest ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Handler for getting custom field data. |
||
| 157 | * @since 2.2.4 |
||
| 158 | * @param array $object The object from the response |
||
| 159 | * @param string $field_id Name of field |
||
| 160 | * @param WP_REST_Request $request Current request |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | public static function get_restable_field_values( $object, $field_id, $request ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Handler for updating custom field data. |
||
| 188 | * @since 2.2.4 |
||
| 189 | * @param mixed $value The value of the field |
||
| 190 | * @param object $object The object from the response |
||
| 191 | * @param string $field_id Name of field |
||
| 192 | * @return bool|int |
||
| 193 | */ |
||
| 194 | public static function update_restable_field_values( $values, $object, $field_id ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Loop through box fields and sanitize the values. |
||
| 223 | * @since 2.2.o |
||
| 224 | * @param array $values Array of values being provided. |
||
| 225 | * @return array Array of updated/sanitized values. |
||
| 226 | */ |
||
| 227 | public function sanitize_box_values( array $values ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Handles returning a sanitized field value. |
||
| 243 | * @since 2.2.4 |
||
| 244 | * @param array $values Array of values being provided. |
||
| 245 | * @param string $field_id The id of the field to update. |
||
| 246 | * @return mixed The results of saving/sanitizing a field value. |
||
| 247 | */ |
||
| 248 | protected function sanitize_field_value( array $values, $field_id ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Handles returning a sanitized group field value. |
||
| 271 | * @since 2.2.4 |
||
| 272 | * @param array $values Array of values being provided. |
||
| 273 | * @param CMB2_Field $field CMB2_Field object. |
||
| 274 | * @return mixed The results of saving/sanitizing the group field value. |
||
| 275 | */ |
||
| 276 | protected function sanitize_group_value( array $values, CMB2_Field $field ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Filter whether a meta key is protected. |
||
| 289 | * @since 2.2.4 |
||
| 290 | * @param bool $protected Whether the key is protected. Default false. |
||
| 291 | * @param string $meta_key Meta key. |
||
| 292 | * @param string $meta_type Meta type. |
||
| 293 | */ |
||
| 294 | public function is_protected_meta( $protected, $meta_key, $meta_type ) { |
||
| 301 | |||
| 302 | protected static function get_object_data( $object ) { |
||
| 324 | |||
| 325 | public function field_can_read( $field_id, $return_object = false ) { |
||
| 328 | |||
| 329 | public function field_can_write( $field_id, $return_object = false ) { |
||
| 332 | |||
| 333 | protected function field_can( $type = 'read_fields', $field_id, $return_object = false ) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get an instance of this class by a CMB2 id |
||
| 343 | * |
||
| 344 | * @since 2.2.4 |
||
| 345 | * |
||
| 346 | * @param string $cmb_id CMB2 config id |
||
| 347 | * |
||
| 348 | * @return CMB2_REST|false The CMB2_REST object or false. |
||
| 349 | */ |
||
| 350 | public static function get_rest_box( $cmb_id ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Magic getter for our object. |
||
| 356 | * @param string $field |
||
| 357 | * @throws Exception Throws an exception if the field is invalid. |
||
| 358 | * @return mixed |
||
| 359 | */ |
||
| 360 | public function __get( $field ) { |
||
| 371 | |||
| 372 | } |
||
| 373 |
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.