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.0 |
||
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.0 |
||
27 | */ |
||
28 | const BASE = 'cmb2/v1'; |
||
29 | |||
30 | /** |
||
31 | * @var CMB2 object |
||
32 | * @since 2.2.0 |
||
33 | */ |
||
34 | public $cmb; |
||
35 | |||
36 | /** |
||
37 | * @var CMB2_REST[] objects |
||
38 | * @since 2.2.0 |
||
39 | */ |
||
40 | public static $boxes; |
||
41 | |||
42 | /** |
||
43 | * Array of readable field objects. |
||
44 | * @var CMB2_Field[] |
||
45 | * @since 2.2.0 |
||
46 | */ |
||
47 | protected $read_fields = array(); |
||
48 | |||
49 | /** |
||
50 | * Array of writeable field objects. |
||
51 | * @var CMB2_Field[] |
||
52 | * @since 2.2.0 |
||
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 readable via the rest api |
||
64 | * @var boolean |
||
65 | */ |
||
66 | protected $rest_write = false; |
||
67 | |||
68 | /** |
||
69 | * Constructor |
||
70 | * @since 2.2.0 |
||
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() { |
||
91 | |||
92 | public function init_routes() { |
||
101 | |||
102 | public static function register_appended_fields() { |
||
120 | |||
121 | protected function declare_read_write_fields() { |
||
139 | |||
140 | protected function can_read( $show_in_rest ) { |
||
145 | |||
146 | protected function can_write( $show_in_rest ) { |
||
151 | |||
152 | /** |
||
153 | * Handler for getting custom field data. |
||
154 | * @since 2.2.0 |
||
155 | * @param array $object The object from the response |
||
156 | * @param string $field_id Name of field |
||
157 | * @param WP_REST_Request $request Current request |
||
158 | * @return mixed |
||
159 | */ |
||
160 | public static function get_restable_field_values( $object, $field_id, $request ) { |
||
182 | |||
183 | /** |
||
184 | * Handler for updating custom field data. |
||
185 | * @since 2.2.0 |
||
186 | * @param mixed $value The value of the field |
||
187 | * @param object $object The object from the response |
||
188 | * @param string $field_id Name of field |
||
189 | * @return bool|int |
||
190 | */ |
||
191 | public static function update_restable_field_values( $values, $object, $field_id ) { |
||
217 | |||
218 | /** |
||
219 | * Loop through box fields and sanitize the values. |
||
220 | * @since 2.2.o |
||
221 | * @param array $values Array of values being provided. |
||
222 | * @return array Array of updated/sanitized values. |
||
223 | */ |
||
224 | public function sanitize_box_values( array $values ) { |
||
237 | |||
238 | /** |
||
239 | * Handles returning a sanitized field value. |
||
240 | * @since 2.2.0 |
||
241 | * @param array $values Array of values being provided. |
||
242 | * @param string $field_id The id of the field to update. |
||
243 | * @return mixed The results of saving/sanitizing a field value. |
||
244 | */ |
||
245 | protected function sanitize_field_value( array $values, $field_id ) { |
||
265 | |||
266 | /** |
||
267 | * Handles returning a sanitized group field value. |
||
268 | * @since 2.2.0 |
||
269 | * @param array $values Array of values being provided. |
||
270 | * @param CMB2_Field $field CMB2_Field object. |
||
271 | * @return mixed The results of saving/sanitizing the group field value. |
||
272 | */ |
||
273 | protected function sanitize_group_value( array $values, CMB2_Field $field ) { |
||
283 | |||
284 | /** |
||
285 | * Filter whether a meta key is protected. |
||
286 | * @since 2.2.0 |
||
287 | * @param bool $protected Whether the key is protected. Default false. |
||
288 | * @param string $meta_key Meta key. |
||
289 | * @param string $meta_type Meta type. |
||
290 | */ |
||
291 | public function is_protected_meta( $protected, $meta_key, $meta_type ) { |
||
298 | |||
299 | protected static function get_object_data( $object ) { |
||
321 | |||
322 | public function field_can_read( $field_id, $return_object = false ) { |
||
325 | |||
326 | public function field_can_write( $field_id, $return_object = false ) { |
||
329 | |||
330 | protected function field_can( $type = 'read_fields', $field_id, $return_object = false ) { |
||
337 | |||
338 | /** |
||
339 | * Get an instance of this class by a CMB2 id |
||
340 | * |
||
341 | * @since 2.2.0 |
||
342 | * |
||
343 | * @param string $cmb_id CMB2 config id |
||
344 | * |
||
345 | * @return CMB2_REST|false The CMB2_REST object or false. |
||
346 | */ |
||
347 | public static function get_rest_box( $cmb_id ) { |
||
350 | |||
351 | /** |
||
352 | * Magic getter for our object. |
||
353 | * @param string $field |
||
354 | * @throws Exception Throws an exception if the field is invalid. |
||
355 | * @return mixed |
||
356 | */ |
||
357 | public function __get( $field ) { |
||
368 | |||
369 | } |
||
370 |
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.