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 WC_REST_Controller 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 WC_REST_Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class WC_REST_Controller extends WP_REST_Controller { |
||
17 | |||
18 | /** |
||
19 | * Endpoint namespace. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $namespace = 'wc/v1'; |
||
24 | |||
25 | /** |
||
26 | * Route base. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $rest_base = ''; |
||
31 | |||
32 | /** |
||
33 | * Add the schema from additional fields to an schema array. |
||
34 | * |
||
35 | * The type of object is inferred from the passed schema. |
||
36 | * |
||
37 | * @param array $schema Schema array. |
||
38 | */ |
||
39 | protected function add_additional_fields_schema( $schema ) { |
||
40 | if ( empty( $schema['title'] ) ) { |
||
41 | return $schema; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Can't use $this->get_object_type otherwise we cause an inf loop. |
||
46 | */ |
||
47 | $object_type = $schema['title']; |
||
48 | |||
49 | $additional_fields = $this->get_additional_fields( $object_type ); |
||
50 | |||
51 | View Code Duplication | foreach ( $additional_fields as $field_name => $field_options ) { |
|
|
|||
52 | if ( ! $field_options['schema'] ) { |
||
53 | continue; |
||
54 | } |
||
55 | |||
56 | $schema['properties'][ $field_name ] = $field_options['schema']; |
||
57 | } |
||
58 | |||
59 | $schema['properties'] = apply_filters( 'woocommerce_rest_' . $object_type . '_schema', $schema['properties'] ); |
||
60 | |||
61 | return $schema; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get normalized rest base. |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | protected function get_normalized_rest_base() { |
||
72 | |||
73 | /** |
||
74 | * Check batch limit. |
||
75 | * |
||
76 | * @param array $items Request items. |
||
77 | * @return bool|WP_Error |
||
78 | */ |
||
79 | protected function check_batch_limit( $items ) { |
||
101 | |||
102 | /** |
||
103 | * Bulk create, update and delete items. |
||
104 | * |
||
105 | * @param WP_REST_Request $request Full details about the request. |
||
106 | * @return array Of WP_Error or WP_REST_Response. |
||
107 | */ |
||
108 | public function batch_items( $request ) { |
||
187 | |||
188 | /** |
||
189 | * Validate a text value for a text based setting. |
||
190 | * |
||
191 | * @since 2.7.0 |
||
192 | * @param string $value |
||
193 | * @param array $setting |
||
194 | * @return string |
||
195 | */ |
||
196 | public function validate_setting_text_field( $value, $setting ) { |
||
201 | |||
202 | /** |
||
203 | * Validate select based settings. |
||
204 | * |
||
205 | * @since 2.7.0 |
||
206 | * @param string $value |
||
207 | * @param array $setting |
||
208 | * @return string|WP_Error |
||
209 | */ |
||
210 | public function validate_setting_select_field( $value, $setting ) { |
||
217 | |||
218 | /** |
||
219 | * Validate multiselect based settings. |
||
220 | * |
||
221 | * @since 2.7.0 |
||
222 | * @param array $values |
||
223 | * @param array $setting |
||
224 | * @return string|WP_Error |
||
225 | */ |
||
226 | public function validate_setting_multiselect_field( $values, $setting ) { |
||
244 | |||
245 | /** |
||
246 | * Validate image_width based settings. |
||
247 | * |
||
248 | * @since 2.7.0 |
||
249 | * @param array $value |
||
250 | * @param array $setting |
||
251 | * @return string|WP_Error |
||
252 | */ |
||
253 | public function validate_setting_image_width_field( $values, $setting ) { |
||
270 | |||
271 | /** |
||
272 | * Validate radio based settings. |
||
273 | * |
||
274 | * @since 2.7.0 |
||
275 | * @param string $value |
||
276 | * @param array $setting |
||
277 | * @return string|WP_Error |
||
278 | */ |
||
279 | public function validate_setting_radio_field( $value, $setting ) { |
||
282 | |||
283 | /** |
||
284 | * Validate checkbox based settings. |
||
285 | * |
||
286 | * @since 2.7.0 |
||
287 | * @param string $value |
||
288 | * @param array $setting |
||
289 | * @return string|WP_Error |
||
290 | */ |
||
291 | public function validate_setting_checkbox_field( $value, $setting ) { |
||
301 | |||
302 | /** |
||
303 | * Validate textarea based settings. |
||
304 | * |
||
305 | * @since 2.7.0 |
||
306 | * @param string $value |
||
307 | * @param array $setting |
||
308 | * @return string |
||
309 | */ |
||
310 | View Code Duplication | public function validate_setting_textarea_field( $value, $setting ) { |
|
321 | |||
322 | /** |
||
323 | * Get the batch schema, conforming to JSON Schema. |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | public function get_public_batch_schema() { |
||
353 | } |
||
354 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.