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 |
||
| 21 | class WC_REST_Shipping_Methods_Controller extends WC_REST_Controller { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Endpoint namespace. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $namespace = 'wc/v1'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Route base. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $rest_base = 'shipping_methods'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Register the route for /shipping_methods and /shipping_methods/<method> |
||
| 39 | */ |
||
| 40 | View Code Duplication | public function register_routes() { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Check whether a given request has permission to view system status. |
||
| 65 | * |
||
| 66 | * @param WP_REST_Request $request Full details about the request. |
||
| 67 | * @return WP_Error|boolean |
||
| 68 | */ |
||
| 69 | public function get_items_permissions_check( $request ) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Check if a given request has access to read a shipping method. |
||
| 78 | * |
||
| 79 | * @param WP_REST_Request $request Full details about the request. |
||
| 80 | * @return WP_Error|boolean |
||
| 81 | */ |
||
| 82 | public function get_item_permissions_check( $request ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get shipping methods. |
||
| 91 | * |
||
| 92 | * @param WP_REST_Request $request Full details about the request. |
||
| 93 | * @return WP_Error|WP_REST_Response |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function get_items( $request ) { |
|
| 96 | $wc_shipping = WC_Shipping::instance(); |
||
| 97 | $response = array(); |
||
| 98 | foreach ( $wc_shipping->get_shipping_methods() as $id => $shipping_method ) { |
||
| 99 | $method = $this->prepare_item_for_response( $shipping_method, $request ); |
||
| 100 | $method = $this->prepare_response_for_collection( $method ); |
||
| 101 | $response[] = $method; |
||
| 102 | } |
||
| 103 | return rest_ensure_response( $response ); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get a single Shipping Method. |
||
| 108 | * |
||
| 109 | * @param WP_REST_Request $request |
||
| 110 | * @return WP_REST_Response|WP_Error |
||
| 111 | */ |
||
| 112 | public function get_item( $request ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Prepare a shipping method for response. |
||
| 127 | * |
||
| 128 | * @param WP_Comment $method Shipping method object. |
||
| 129 | * @param WP_REST_Request $request Request object. |
||
| 130 | * @return WP_REST_Response $response Response data. |
||
| 131 | */ |
||
| 132 | public function prepare_item_for_response( $method, $request ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Prepare links for the request. |
||
| 160 | * |
||
| 161 | * @param Object $method Shipping method object. |
||
| 162 | * @param WP_REST_Request $request Request object. |
||
| 163 | * @return array Links for the given product review. |
||
| 164 | */ |
||
| 165 | protected function prepare_links( $method, $request ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the shipping method schema, conforming to JSON Schema. |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function get_item_schema() { |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Get any query params needed. |
||
| 212 | * |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function get_collection_params() { |
||
| 220 | |||
| 221 | } |
||
| 222 |
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.