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 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[] objects |
||
| 32 | * @since 2.2.0 |
||
| 33 | */ |
||
| 34 | protected static $boxes; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Whether metabox fields can be read via REST API |
||
| 38 | * @var bool |
||
| 39 | * @since 2.2.0 |
||
| 40 | */ |
||
| 41 | protected $can_read = true; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Array of readable field objects. |
||
| 45 | * @var CMB2_Field[] |
||
| 46 | * @since 2.2.0 |
||
| 47 | */ |
||
| 48 | protected static $read_fields = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Array of writeable field objects. |
||
| 52 | * @var CMB2_Field[] |
||
| 53 | * @since 2.2.0 |
||
| 54 | */ |
||
| 55 | protected static $write_fields = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Whether metabox fields can be written via REST API |
||
| 59 | * @var bool |
||
| 60 | * @since 2.2.0 |
||
| 61 | */ |
||
| 62 | protected $can_write = false; |
||
| 63 | |||
| 64 | public function __construct( CMB2 $cmb ) { |
||
| 72 | |||
| 73 | public function universal_hooks() { |
||
| 83 | |||
| 84 | public function init_routes() { |
||
| 93 | |||
| 94 | public static function register_fields() { |
||
| 112 | |||
| 113 | protected function prepare_read_write_fields() { |
||
| 125 | |||
| 126 | View Code Duplication | protected function maybe_add_read_field( $field_id, $show_in_rest ) { |
|
| 135 | |||
| 136 | View Code Duplication | protected function maybe_add_write_field( $field_id, $show_in_rest ) { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Handler for getting custom field data. |
||
| 148 | * @since 2.2.0 |
||
| 149 | * @param array $object The object from the response |
||
| 150 | * @param string $field_id Name of field |
||
| 151 | * @param WP_REST_Request $request Current request |
||
| 152 | * @return mixed |
||
| 153 | */ |
||
| 154 | public static function get_restable_field_values( $object, $field_id, $request ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Handler for updating custom field data. |
||
| 178 | * @since 2.2.0 |
||
| 179 | * @param mixed $value The value of the field |
||
| 180 | * @param object $object The object from the response |
||
| 181 | * @param string $field_id Name of field |
||
| 182 | * @return bool|int |
||
| 183 | */ |
||
| 184 | public static function update_restable_field_values( $values, $object, $field_id ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Filter whether a meta key is protected. |
||
| 247 | * @since 2.2.0 |
||
| 248 | * @param bool $protected Whether the key is protected. Default false. |
||
| 249 | * @param string $meta_key Meta key. |
||
| 250 | * @param string $meta_type Meta type. |
||
| 251 | */ |
||
| 252 | public function is_protected_meta( $protected, $meta_key, $meta_type ) { |
||
| 259 | |||
| 260 | public function field_can_update( $field_id ) { |
||
| 270 | |||
| 271 | protected static function get_object_data( $object ) { |
||
| 293 | |||
| 294 | } |
||
| 295 |
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.