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_Access 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_Access, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class CMB2_REST_Access extends CMB2_Hookup_Base { |
||
|
|||
15 | |||
16 | /** |
||
17 | * @var CMB2[] objects |
||
18 | * @since 2.2.0 |
||
19 | */ |
||
20 | protected static $boxes; |
||
21 | |||
22 | /** |
||
23 | * Whether metabox fields can be read via REST API |
||
24 | * @var bool |
||
25 | * @since 2.2.0 |
||
26 | */ |
||
27 | protected $can_read = true; |
||
28 | |||
29 | /** |
||
30 | * Array of readable field objects. |
||
31 | * @var CMB2_Field[] |
||
32 | * @since 2.2.0 |
||
33 | */ |
||
34 | protected static $read_fields = array(); |
||
35 | |||
36 | /** |
||
37 | * Array of writeable field objects. |
||
38 | * @var CMB2_Field[] |
||
39 | * @since 2.2.0 |
||
40 | */ |
||
41 | protected static $write_fields = array(); |
||
42 | |||
43 | /** |
||
44 | * Whether metabox fields can be written via REST API |
||
45 | * @var bool |
||
46 | * @since 2.2.0 |
||
47 | */ |
||
48 | protected $can_write = false; |
||
49 | |||
50 | public function __construct( CMB2 $cmb ) { |
||
58 | |||
59 | public function universal_hooks() { |
||
69 | |||
70 | public static function register_fields() { |
||
88 | |||
89 | protected function prepare_read_write_fields() { |
||
101 | |||
102 | View Code Duplication | protected function maybe_add_read_field( $field_id, $show_in_rest ) { |
|
111 | |||
112 | View Code Duplication | protected function maybe_add_write_field( $field_id, $show_in_rest ) { |
|
121 | |||
122 | /** |
||
123 | * Handler for getting custom field data. |
||
124 | * @since 2.2.0 |
||
125 | * @param array $object The object from the response |
||
126 | * @param string $field_id Name of field |
||
127 | * @param WP_REST_Request $request Current request |
||
128 | * @return mixed |
||
129 | */ |
||
130 | public static function get_restable_field_values( $object, $field_id, $request ) { |
||
151 | |||
152 | /** |
||
153 | * Handler for updating custom field data. |
||
154 | * @since 2.2.0 |
||
155 | * @param mixed $value The value of the field |
||
156 | * @param object $object The object from the response |
||
157 | * @param string $field_id Name of field |
||
158 | * @return bool|int |
||
159 | */ |
||
160 | public static function update_restable_field_values( $values, $object, $field_id ) { |
||
220 | |||
221 | /** |
||
222 | * Filter whether a meta key is protected. |
||
223 | * @since 2.2.0 |
||
224 | * @param bool $protected Whether the key is protected. Default false. |
||
225 | * @param string $meta_key Meta key. |
||
226 | * @param string $meta_type Meta type. |
||
227 | */ |
||
228 | public function is_protected_meta( $protected, $meta_key, $meta_type ) { |
||
235 | |||
236 | public function field_can_update( $field_id ) { |
||
246 | |||
247 | protected static function get_object_data( $object ) { |
||
269 | |||
270 | } |
||
271 |
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.