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() { |
||
127 | |||
128 | protected function declare_read_edit_fields() { |
||
146 | |||
147 | protected function can_read( $show_in_rest ) { |
||
156 | |||
157 | protected function can_edit( $show_in_rest ) { |
||
166 | |||
167 | /** |
||
168 | * Handler for getting custom field data. |
||
169 | * |
||
170 | * @since 2.2.4 |
||
171 | * |
||
172 | * @param array $object The object data from the response |
||
173 | * @param string $field_name Name of field |
||
174 | * @param WP_REST_Request $request Current request |
||
175 | * @param string $object_type The request object type |
||
176 | * |
||
177 | * @return mixed |
||
178 | */ |
||
179 | public static function get_restable_field_values( $object, $field_name, $request, $object_type ) { |
||
202 | |||
203 | /** |
||
204 | * Handler for updating custom field data. |
||
205 | * |
||
206 | * @since 2.2.4 |
||
207 | * |
||
208 | * @param mixed $value The value of the field |
||
209 | * @param object $object The object from the response |
||
210 | * @param string $field_name Name of field |
||
211 | * @param WP_REST_Request $request Current request |
||
212 | * @param string $object_type The request object type |
||
213 | * |
||
214 | * @return bool|int |
||
215 | */ |
||
216 | public static function update_restable_field_values( $values, $object, $field_name, $request, $object_type ) { |
||
244 | |||
245 | /** |
||
246 | * Loop through box fields and sanitize the values. |
||
247 | * |
||
248 | * @since 2.2.o |
||
249 | * |
||
250 | * @param array $values Array of values being provided. |
||
251 | * @return array Array of updated/sanitized values. |
||
252 | */ |
||
253 | public function sanitize_box_values( array $values ) { |
||
266 | |||
267 | /** |
||
268 | * Handles returning a sanitized field value. |
||
269 | * |
||
270 | * @since 2.2.4 |
||
271 | * |
||
272 | * @param array $values Array of values being provided. |
||
273 | * @param string $field_id The id of the field to update. |
||
274 | * |
||
275 | * @return mixed The results of saving/sanitizing a field value. |
||
276 | */ |
||
277 | protected function sanitize_field_value( array $values, $field_id ) { |
||
297 | |||
298 | /** |
||
299 | * Handles returning a sanitized group field value. |
||
300 | * |
||
301 | * @since 2.2.4 |
||
302 | * |
||
303 | * @param array $values Array of values being provided. |
||
304 | * @param CMB2_Field $field CMB2_Field object. |
||
305 | * |
||
306 | * @return mixed The results of saving/sanitizing the group field value. |
||
307 | */ |
||
308 | protected function sanitize_group_value( array $values, CMB2_Field $field ) { |
||
318 | |||
319 | /** |
||
320 | * Filter whether a meta key is protected. |
||
321 | * |
||
322 | * @since 2.2.4 |
||
323 | * |
||
324 | * @param bool $protected Whether the key is protected. Default false. |
||
325 | * @param string $meta_key Meta key. |
||
326 | * @param string $meta_type Meta type. |
||
327 | */ |
||
328 | public function is_protected_meta( $protected, $meta_key, $meta_type ) { |
||
335 | |||
336 | protected static function get_object_data( $object ) { |
||
358 | |||
359 | public function field_can_read( $field_id, $return_object = false ) { |
||
362 | |||
363 | public function field_can_edit( $field_id, $return_object = false ) { |
||
366 | |||
367 | protected function field_can( $type = 'read_fields', $field_id, $return_object = false ) { |
||
374 | |||
375 | /** |
||
376 | * Get a CMB2_REST instance object from the registry by a CMB2 id. |
||
377 | * |
||
378 | * @since 2.2.4 |
||
379 | * |
||
380 | * @param string $cmb_id CMB2 config id |
||
381 | * |
||
382 | * @return CMB2_REST|false The CMB2_REST object or false. |
||
383 | */ |
||
384 | public static function get_rest_box( $cmb_id ) { |
||
387 | |||
388 | /** |
||
389 | * Remove a CMB2_REST instance object from the registry. |
||
390 | * |
||
391 | * @since 2.2.4 |
||
392 | * |
||
393 | * @param string $cmb_id A CMB2 instance id. |
||
394 | */ |
||
395 | public static function remove( $cmb_id ) { |
||
400 | |||
401 | /** |
||
402 | * Checks if given value is readable. |
||
403 | * |
||
404 | * Value is considered readable if it is not empty and if it does not match the editable blacklist. |
||
405 | * |
||
406 | * @since 2.2.4 |
||
407 | * |
||
408 | * @param mixed $value Value to check. |
||
409 | * |
||
410 | * @return boolean Whether value is considered readable. |
||
411 | */ |
||
412 | public static function is_readable( $value ) { |
||
419 | |||
420 | /** |
||
421 | * Checks if given value is editable. |
||
422 | * |
||
423 | * Value is considered editable if matches the editable whitelist. |
||
424 | * |
||
425 | * @since 2.2.4 |
||
426 | * |
||
427 | * @param mixed $value Value to check. |
||
428 | * |
||
429 | * @return boolean Whether value is considered editable. |
||
430 | */ |
||
431 | public static function is_editable( $value ) { |
||
437 | |||
438 | /** |
||
439 | * Magic getter for our object. |
||
440 | * |
||
441 | * @param string $field |
||
442 | * @throws Exception Throws an exception if the field is invalid. |
||
443 | * |
||
444 | * @return mixed |
||
445 | */ |
||
446 | public function __get( $field ) { |
||
457 | |||
458 | } |
||
459 |
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.