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_Payment_Gateways_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_Payment_Gateways_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class WC_REST_Payment_Gateways_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 = 'payment_gateways'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Register the route for /payment_gateways and /payment_gateways/<id> |
||
| 39 | */ |
||
| 40 | public function register_routes() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Check whether a given request has permission to view payment gateways. |
||
| 71 | * |
||
| 72 | * @param WP_REST_Request $request Full details about the request. |
||
| 73 | * @return WP_Error|boolean |
||
| 74 | */ |
||
| 75 | public function get_items_permissions_check( $request ) { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Check if a given request has access to read a payment gateway. |
||
| 84 | * |
||
| 85 | * @param WP_REST_Request $request Full details about the request. |
||
| 86 | * @return WP_Error|boolean |
||
| 87 | */ |
||
| 88 | public function get_item_permissions_check( $request ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Check whether a given request has permission to edit payment gateways. |
||
| 97 | * |
||
| 98 | * @param WP_REST_Request $request Full details about the request. |
||
| 99 | * @return WP_Error|boolean |
||
| 100 | */ |
||
| 101 | public function update_items_permissions_check( $request ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get payment gateways. |
||
| 110 | * |
||
| 111 | * @param WP_REST_Request $request Full details about the request. |
||
| 112 | * @return WP_Error|WP_REST_Response |
||
| 113 | */ |
||
| 114 | public function get_items( $request ) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get a single payment gateway. |
||
| 129 | * |
||
| 130 | * @param WP_REST_Request $request |
||
| 131 | * @return WP_REST_Response|WP_Error |
||
| 132 | */ |
||
| 133 | public function get_item( $request ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Update A Single Shipping Zone Method. |
||
| 147 | * |
||
| 148 | * @param WP_REST_Request $request |
||
| 149 | * @return WP_REST_Response|WP_Error |
||
| 150 | */ |
||
| 151 | public function update_item( $request ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get a gateway based on the current request object. |
||
| 194 | * |
||
| 195 | * @param WC_Payment_Gateway $gateway Payment gateway object. |
||
| 196 | * @return WP_REST_Response|null |
||
| 197 | */ |
||
| 198 | public function get_gateway( $request ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Prepare a payment gateway for response. |
||
| 213 | * |
||
| 214 | * @param WC_Payment_Gateway $gateway Payment gateway object. |
||
| 215 | * @param WP_REST_Request $request Request object. |
||
| 216 | * @return WP_REST_Response $response Response data. |
||
| 217 | */ |
||
| 218 | public function prepare_item_for_response( $gateway, $request ) { |
||
| 219 | $order = (array) get_option( 'woocommerce_gateway_order' ); |
||
| 220 | $item = array( |
||
| 221 | 'id' => $gateway->id, |
||
| 222 | 'title' => $gateway->title, |
||
| 223 | 'description' => $gateway->description, |
||
| 224 | 'order' => isset( $order[ $gateway->id ] ) ? $order[ $gateway->id ] : '', |
||
| 225 | 'enabled' => ( 'yes' === $gateway->enabled ), |
||
| 226 | 'method_title' => $gateway->get_method_title(), |
||
| 227 | 'method_description' => $gateway->get_method_description(), |
||
| 228 | 'settings' => $this->get_settings( $gateway ), |
||
| 229 | ); |
||
| 230 | |||
| 231 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
||
| 232 | $data = $this->add_additional_fields_to_object( $item, $request ); |
||
| 233 | $data = $this->filter_response_by_context( $item, $context ); |
||
| 234 | |||
| 235 | $response = rest_ensure_response( $item ); |
||
| 236 | $response->add_links( $this->prepare_links( $gateway, $request ) ); |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Filter payment gateway objects returned from the REST API. |
||
| 240 | * |
||
| 241 | * @param WP_REST_Response $response The response object. |
||
| 242 | * @param WC_Payment_Gateway $gateway Payment gateway object. |
||
| 243 | * @param WP_REST_Request $request Request object. |
||
| 244 | */ |
||
| 245 | return apply_filters( 'woocommerce_rest_prepare_payment_gateway', $response, $gateway, $request ); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Return settings associated with this payment gateway. |
||
| 250 | */ |
||
| 251 | public function get_settings( $gateway ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Some form fields (like COD) have a setting to limit to specific shipping |
||
| 283 | * methods. Some of the code for loading these into settings is behind an |
||
| 284 | * is_admin check. To work correctly with methods that do this, we can |
||
| 285 | * define the constant here and act as wp-admin (since these settings are |
||
| 286 | * shown to managers and admins only anyway). |
||
| 287 | */ |
||
| 288 | protected function maybe_define_wp_admin() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Prepare links for the request. |
||
| 296 | * |
||
| 297 | * @param WC_Payment_Gateway $gateway Payment gateway object. |
||
| 298 | * @param WP_REST_Request $request Request object. |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | protected function prepare_links( $gateway, $request ) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the payment gateway schema, conforming to JSON Schema. |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | public function get_item_schema() { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get any query params needed. |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | public function get_collection_params() { |
||
| 384 | |||
| 385 | } |
||
| 386 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.