Complex classes like CMB2_REST_Controller_Fields 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_Controller_Fields, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 17 | class CMB2_REST_Controller_Fields extends CMB2_REST_Controller_Boxes { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 18 | |||
| 19 | /**  | 
            ||
| 20 | * Register the routes for the objects of the controller.  | 
            ||
| 21 | *  | 
            ||
| 22 | * @since 2.2.4  | 
            ||
| 23 | */  | 
            ||
| 24 | 	public function register_routes() { | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * Get all public CMB2 box fields.  | 
            ||
| 60 | *  | 
            ||
| 61 | * @since 2.2.4  | 
            ||
| 62 | *  | 
            ||
| 63 | * @param WP_REST_Request $request Full data about the request.  | 
            ||
| 64 | * @return WP_Error|WP_REST_Response  | 
            ||
| 65 | */  | 
            ||
| 66 | 	public function get_items( $request ) { | 
            ||
| 87 | |||
| 88 | /**  | 
            ||
| 89 | * Get one CMB2 field from the collection.  | 
            ||
| 90 | *  | 
            ||
| 91 | * @since 2.2.4  | 
            ||
| 92 | *  | 
            ||
| 93 | * @param WP_REST_Request $request Full data about the request.  | 
            ||
| 94 | * @return WP_Error|WP_REST_Response  | 
            ||
| 95 | */  | 
            ||
| 96 | 	public function get_item( $request ) { | 
            ||
| 97 | $this->initiate_rest_read_box( $request, 'field_read' );  | 
            ||
| 98 | |||
| 99 | 		if ( is_wp_error( $this->rest_box ) ) { | 
            ||
| 100 | return $this->rest_box;  | 
            ||
| 101 | }  | 
            ||
| 102 | |||
| 103 | $field = $this->get_rest_field( $this->request->get_param( 'field_id' ) );  | 
            ||
| 104 | |||
| 105 | 		if ( is_wp_error( $field ) ) { | 
            ||
| 106 | return $field;  | 
            ||
| 107 | }  | 
            ||
| 108 | |||
| 109 | return $this->prepare_item( $field );  | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * Update CMB2 field value.  | 
            ||
| 114 | *  | 
            ||
| 115 | * @since 2.2.4  | 
            ||
| 116 | *  | 
            ||
| 117 | * @param WP_REST_Request $request Full data about the request.  | 
            ||
| 118 | * @return WP_Error|WP_REST_Response  | 
            ||
| 119 | */  | 
            ||
| 120 | 	public function update_field_value( $request ) { | 
            ||
| 131 | |||
| 132 | /**  | 
            ||
| 133 | * Delete CMB2 field value.  | 
            ||
| 134 | *  | 
            ||
| 135 | * @since 2.2.4  | 
            ||
| 136 | *  | 
            ||
| 137 | * @param WP_REST_Request $request Full data about the request.  | 
            ||
| 138 | * @return WP_Error|WP_REST_Response  | 
            ||
| 139 | */  | 
            ||
| 140 | 	public function delete_field_value( $request ) { | 
            ||
| 147 | |||
| 148 | /**  | 
            ||
| 149 | * Modify CMB2 field value.  | 
            ||
| 150 | *  | 
            ||
| 151 | * @since 2.2.4  | 
            ||
| 152 | *  | 
            ||
| 153 | * @param string $activity The modification activity (updated or deleted).  | 
            ||
| 154 | * @param CMB2_Field $field The field object.  | 
            ||
| 155 | * @return WP_Error|WP_REST_Response  | 
            ||
| 156 | */  | 
            ||
| 157 | 	public function modify_field_value( $activity, $field ) { | 
            ||
| 158 | |||
| 159 | 		if ( ! $this->request['object_id'] && ! $this->request['object_type'] ) { | 
            ||
| 160 | return new WP_Error( 'cmb2_rest_modify_field_value_error', __( 'CMB2 Field value cannot be modified without the object_id and object_type parameters specified.', 'cmb2' ), array( 'status' => 400 ) );  | 
            ||
| 161 | }  | 
            ||
| 162 | |||
| 163 | 		if ( is_wp_error( $this->rest_box ) ) { | 
            ||
| 164 | return $this->rest_box;  | 
            ||
| 165 | }  | 
            ||
| 166 | |||
| 167 | 		if ( ! $field ) { | 
            ||
| 168 | return new WP_Error( 'cmb2_rest_error', __( 'No field found by that id.', 'cmb2' ), array( 'status' => 403 ) );  | 
            ||
| 169 | }  | 
            ||
| 170 | |||
| 171 | 		$field->args["value_{$activity}"] = (bool) 'deleted' === $activity | 
            ||
| 172 | ? $field->remove_data()  | 
            ||
| 173 | : $field->save_field( $this->request['value'] );  | 
            ||
| 174 | |||
| 175 | // If options page, save the $activity options  | 
            ||
| 176 | 		if ( 'options-page' == $this->request['object_type'] ) { | 
            ||
| 177 | 			$field->args["value_{$activity}"] = cmb2_options( $this->request['object_id'] )->set(); | 
            ||
| 178 | }  | 
            ||
| 179 | |||
| 180 | $field_data = $this->get_rest_field( $field );  | 
            ||
| 181 | |||
| 182 | 		if ( is_wp_error( $field_data ) ) { | 
            ||
| 183 | return $field_data;  | 
            ||
| 184 | }  | 
            ||
| 185 | |||
| 186 | return $this->prepare_item( $field_data );  | 
            ||
| 187 | }  | 
            ||
| 188 | |||
| 189 | /**  | 
            ||
| 190 | * Get a specific field  | 
            ||
| 191 | *  | 
            ||
| 192 | * @since 2.2.4  | 
            ||
| 193 | *  | 
            ||
| 194 | * @param string Field id  | 
            ||
| 195 | * @return array|WP_Error  | 
            ||
| 196 | */  | 
            ||
| 197 | 	public function get_rest_field( $field_id ) { | 
            ||
| 211 | |||
| 212 | /**  | 
            ||
| 213 | * Prepare the field data array for JSON.  | 
            ||
| 214 | *  | 
            ||
| 215 | * @since 2.2.4  | 
            ||
| 216 | *  | 
            ||
| 217 | * @param CMB2_Field $field field object.  | 
            ||
| 218 | *  | 
            ||
| 219 | * @return array Array of field data.  | 
            ||
| 220 | */  | 
            ||
| 221 | 	protected function prepare_field_data( CMB2_Field $field ) { | 
            ||
| 271 | |||
| 272 | /**  | 
            ||
| 273 | * Return an array of contextual links for field/fields.  | 
            ||
| 274 | *  | 
            ||
| 275 | * @since 2.2.4  | 
            ||
| 276 | *  | 
            ||
| 277 | * @param CMB2_Field $field Field object to build links from.  | 
            ||
| 278 | *  | 
            ||
| 279 | * @return array Array of links  | 
            ||
| 280 | */  | 
            ||
| 281 | 	protected function prepare_links( $field ) { | 
            ||
| 304 | |||
| 305 | }  | 
            ||
| 306 | 
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.