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:
1 | <?php |
||
23 | class WC_REST_Tax_Classes_Controller extends WC_REST_Controller { |
||
24 | |||
25 | /** |
||
26 | * Endpoint namespace. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $namespace = 'wc/v1'; |
||
31 | |||
32 | /** |
||
33 | * Route base. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $rest_base = 'taxes/classes'; |
||
38 | |||
39 | /** |
||
40 | * Register the routes for tax classes. |
||
41 | */ |
||
42 | public function register_routes() { |
||
74 | |||
75 | /** |
||
76 | * Check whether a given request has permission to read tax classes. |
||
77 | * |
||
78 | * @param WP_REST_Request $request Full details about the request. |
||
79 | * @return WP_Error|boolean |
||
80 | */ |
||
81 | View Code Duplication | public function get_items_permissions_check( $request ) { |
|
88 | |||
89 | /** |
||
90 | * Check if a given request has access create tax classes. |
||
91 | * |
||
92 | * @param WP_REST_Request $request Full details about the request. |
||
93 | * @return boolean |
||
94 | */ |
||
95 | View Code Duplication | public function create_item_permissions_check( $request ) { |
|
102 | |||
103 | /** |
||
104 | * Check if a given request has access delete a tax. |
||
105 | * |
||
106 | * @param WP_REST_Request $request Full details about the request. |
||
107 | * @return boolean |
||
108 | */ |
||
109 | View Code Duplication | public function delete_item_permissions_check( $request ) { |
|
116 | |||
117 | /** |
||
118 | * Get all tax classes. |
||
119 | * |
||
120 | * @param WP_REST_Request $request |
||
121 | * @return array |
||
122 | */ |
||
123 | public function get_items( $request ) { |
||
150 | |||
151 | /** |
||
152 | * Create a single tax. |
||
153 | * |
||
154 | * @param WP_REST_Request $request Full details about the request. |
||
155 | * @return WP_Error|WP_REST_Response |
||
156 | */ |
||
157 | public function create_item( $request ) { |
||
202 | |||
203 | /** |
||
204 | * Delete a single tax class. |
||
205 | * |
||
206 | * @param WP_REST_Request $request Full details about the request. |
||
207 | * @return WP_Error|WP_REST_Response |
||
208 | */ |
||
209 | public function delete_item( $request ) { |
||
210 | global $wpdb; |
||
211 | |||
212 | $force = isset( $request['force'] ) ? (bool) $request['force'] : false; |
||
213 | |||
214 | // We don't support trashing for this type, error out. |
||
215 | if ( ! $force ) { |
||
216 | return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Taxes do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) ); |
||
217 | } |
||
218 | |||
219 | $tax_class = array( |
||
220 | 'slug' => sanitize_title( $request['slug'] ), |
||
221 | 'name' => '', |
||
222 | ); |
||
223 | $classes = WC_Tax::get_tax_classes(); |
||
224 | $deleted = false; |
||
225 | |||
226 | foreach ( $classes as $key => $class ) { |
||
227 | if ( sanitize_title( $class ) === $tax_class['slug'] ) { |
||
228 | $tax_class['name'] = $class; |
||
229 | unset( $classes[ $key ] ); |
||
230 | $deleted = true; |
||
231 | break; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | if ( ! $deleted ) { |
||
236 | return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
237 | } |
||
238 | |||
239 | update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) ); |
||
240 | |||
241 | // Delete tax rate locations locations from the selected class. |
||
242 | $wpdb->query( $wpdb->prepare( " |
||
243 | DELETE locations.* |
||
244 | FROM {$wpdb->prefix}woocommerce_tax_rate_locations AS locations |
||
245 | INNER JOIN |
||
246 | {$wpdb->prefix}woocommerce_tax_rates AS rates |
||
247 | ON rates.tax_rate_id = locations.tax_rate_id |
||
248 | WHERE rates.tax_rate_class = '%s' |
||
249 | ", $tax_class['slug'] ) ); |
||
250 | |||
251 | // Delete tax rates in the selected class. |
||
252 | $wpdb->delete( $wpdb->prefix . 'woocommerce_tax_rates', array( 'tax_rate_class' => $tax_class['slug'] ), array( '%s' ) ); |
||
253 | |||
254 | $request->set_param( 'context', 'edit' ); |
||
255 | $response = $this->prepare_item_for_response( $tax_class, $request ); |
||
256 | |||
257 | /** |
||
258 | * Fires after a tax class is deleted via the REST API. |
||
259 | * |
||
260 | * @param stdClass $tax_class The tax data. |
||
261 | * @param WP_REST_Response $response The response returned from the API. |
||
262 | * @param WP_REST_Request $request The request sent to the API. |
||
263 | */ |
||
264 | do_action( 'woocommerce_rest_delete_tax', (object) $tax_class, $response, $request ); |
||
265 | |||
266 | return $response; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Prepare a single tax class output for response. |
||
271 | * |
||
272 | * @param array $tax_class Tax class data. |
||
273 | * @param WP_REST_Request $request Request object. |
||
274 | * @return WP_REST_Response $response Response data. |
||
275 | */ |
||
276 | public function prepare_item_for_response( $tax_class, $request ) { |
||
297 | |||
298 | /** |
||
299 | * Prepare links for the request. |
||
300 | * |
||
301 | * @return array Links for the given tax class. |
||
302 | */ |
||
303 | protected function prepare_links() { |
||
312 | |||
313 | /** |
||
314 | * Get the Tax Classes schema, conforming to JSON Schema |
||
315 | * |
||
316 | * @return array |
||
317 | */ |
||
318 | public function get_item_schema() { |
||
344 | |||
345 | /** |
||
346 | * Get the query params for collections. |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | public function get_collection_params() { |
||
355 | } |
||
356 |
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.