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_Webhooks_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_Webhooks_Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class WC_REST_Webhooks_Controller extends WC_REST_Posts_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 = 'webhooks'; |
||
38 | |||
39 | /** |
||
40 | * Post type. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $post_type = 'shop_webhook'; |
||
45 | |||
46 | /** |
||
47 | * Initialize Webhooks actions. |
||
48 | */ |
||
49 | public function __construct() { |
||
52 | |||
53 | /** |
||
54 | * Register the routes for webhooks. |
||
55 | */ |
||
56 | public function register_routes() { |
||
57 | register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
||
58 | array( |
||
59 | 'methods' => WP_REST_Server::READABLE, |
||
60 | 'callback' => array( $this, 'get_items' ), |
||
61 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
62 | 'args' => $this->get_collection_params(), |
||
63 | ), |
||
64 | array( |
||
65 | 'methods' => WP_REST_Server::CREATABLE, |
||
66 | 'callback' => array( $this, 'create_item' ), |
||
67 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
||
68 | 'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array( |
||
69 | 'topic' => array( |
||
70 | 'required' => true, |
||
71 | ), |
||
72 | 'delivery_url' => array( |
||
73 | 'required' => true, |
||
74 | ), |
||
75 | ) ), |
||
76 | ), |
||
77 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
78 | ) ); |
||
79 | |||
80 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( |
||
81 | array( |
||
82 | 'methods' => WP_REST_Server::READABLE, |
||
83 | 'callback' => array( $this, 'get_item' ), |
||
84 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||
85 | 'args' => array( |
||
86 | 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
||
87 | ), |
||
88 | ), |
||
89 | array( |
||
90 | 'methods' => WP_REST_Server::EDITABLE, |
||
91 | 'callback' => array( $this, 'update_item' ), |
||
92 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
||
93 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
94 | ), |
||
95 | array( |
||
96 | 'methods' => WP_REST_Server::DELETABLE, |
||
97 | 'callback' => array( $this, 'delete_item' ), |
||
98 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
||
99 | 'args' => array( |
||
100 | 'force' => array( |
||
101 | 'default' => false, |
||
102 | 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ), |
||
103 | ), |
||
104 | ), |
||
105 | ), |
||
106 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
107 | ) ); |
||
108 | |||
109 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( |
||
110 | array( |
||
111 | 'methods' => WP_REST_Server::EDITABLE, |
||
112 | 'callback' => array( $this, 'batch_items' ), |
||
113 | 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
||
114 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
115 | ), |
||
116 | 'schema' => array( $this, 'get_public_batch_schema' ), |
||
117 | ) ); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Create a single webhook. |
||
122 | * |
||
123 | * @param WP_REST_Request $request Full details about the request. |
||
124 | * @return WP_Error|WP_REST_Response |
||
125 | */ |
||
126 | public function create_item( $request ) { |
||
202 | |||
203 | /** |
||
204 | * Update a single webhook. |
||
205 | * |
||
206 | * @param WP_REST_Request $request Full details about the request. |
||
207 | * @return WP_Error|WP_REST_Response |
||
208 | */ |
||
209 | public function update_item( $request ) { |
||
283 | |||
284 | /** |
||
285 | * Delete a single webhook. |
||
286 | * |
||
287 | * @param WP_REST_Request $request Full details about the request. |
||
288 | * @return WP_REST_Response|WP_Error |
||
289 | */ |
||
290 | public function delete_item( $request ) { |
||
328 | |||
329 | /** |
||
330 | * Prepare a single webhook for create or update. |
||
331 | * |
||
332 | * @param WP_REST_Request $request Request object. |
||
333 | * @return WP_Error|stdClass $data Post object. |
||
334 | */ |
||
335 | protected function prepare_item_for_database( $request ) { |
||
336 | global $wpdb; |
||
337 | |||
338 | $data = new stdClass; |
||
339 | |||
340 | // Post ID. |
||
341 | if ( isset( $request['id'] ) ) { |
||
342 | $data->ID = absint( $request['id'] ); |
||
343 | } |
||
344 | |||
345 | // Validate required POST fields. |
||
346 | if ( 'POST' === $request->get_method() && empty( $data->ID ) ) { |
||
347 | $data->post_title = ! empty( $request['name'] ) ? $request['name'] : sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) ); |
||
348 | |||
349 | // Post author. |
||
350 | $data->post_author = get_current_user_id(); |
||
351 | |||
352 | // Post password. |
||
353 | $password = strlen( uniqid( 'webhook_' ) ); |
||
354 | $data->post_password = $password > 20 ? substr( $password, 0, 20 ) : $password; |
||
355 | |||
356 | // Post status. |
||
357 | $data->post_status = 'publish'; |
||
358 | } else { |
||
359 | |||
360 | // Allow edit post title. |
||
361 | if ( ! empty( $request['name'] ) ) { |
||
362 | $data->post_title = $request['name']; |
||
363 | } |
||
364 | } |
||
365 | |||
366 | // Comment status. |
||
367 | $data->comment_status = 'closed'; |
||
368 | |||
369 | // Ping status. |
||
370 | $data->ping_status = 'closed'; |
||
371 | |||
372 | /** |
||
373 | * Filter the query_vars used in `get_items` for the constructed query. |
||
374 | * |
||
375 | * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being |
||
376 | * prepared for insertion. |
||
377 | * |
||
378 | * @param stdClass $data An object representing a single item prepared |
||
379 | * for inserting or updating the database. |
||
380 | * @param WP_REST_Request $request Request object. |
||
381 | */ |
||
382 | return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $data, $request ); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Prepare a single webhook output for response. |
||
387 | * |
||
388 | * @param WP_REST_Request $request Request object. |
||
389 | * @return WP_REST_Response $response Response data. |
||
390 | */ |
||
391 | public function prepare_item_for_response( $post, $request ) { |
||
425 | |||
426 | /** |
||
427 | * Query args. |
||
428 | * |
||
429 | * @param array $args |
||
430 | * @param WP_REST_Request $request |
||
431 | * @return array |
||
432 | */ |
||
433 | public function query_args( $args, $request ) { |
||
452 | |||
453 | /** |
||
454 | * Get the Webhook's schema, conforming to JSON Schema. |
||
455 | * |
||
456 | * @return array |
||
457 | */ |
||
458 | public function get_item_schema() { |
||
459 | $schema = array( |
||
460 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
||
461 | 'title' => 'webhook', |
||
462 | 'type' => 'object', |
||
463 | 'properties' => array( |
||
464 | 'id' => array( |
||
465 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
||
466 | 'type' => 'integer', |
||
467 | 'context' => array( 'view', 'edit' ), |
||
468 | 'readonly' => true, |
||
469 | ), |
||
470 | 'name' => array( |
||
471 | 'description' => __( 'A friendly name for the webhook.', 'woocommerce' ), |
||
472 | 'type' => 'string', |
||
473 | 'context' => array( 'view', 'edit' ), |
||
474 | ), |
||
475 | 'status' => array( |
||
476 | 'description' => __( 'Webhook status.', 'woocommerce' ), |
||
477 | 'type' => 'string', |
||
478 | 'default' => 'active', |
||
479 | 'enum' => array( 'active', 'paused', 'disabled' ), |
||
480 | 'context' => array( 'view', 'edit' ), |
||
481 | 'arg_options' => array( |
||
482 | 'sanitize_callback' => 'wc_is_webhook_valid_topic', |
||
483 | ), |
||
484 | ), |
||
485 | 'topic' => array( |
||
486 | 'description' => __( 'Webhook topic.', 'woocommerce' ), |
||
487 | 'type' => 'string', |
||
488 | 'context' => array( 'view', 'edit' ), |
||
489 | ), |
||
490 | 'resource' => array( |
||
491 | 'description' => __( 'Webhook resource.', 'woocommerce' ), |
||
492 | 'type' => 'string', |
||
493 | 'context' => array( 'view', 'edit' ), |
||
494 | 'readonly' => true, |
||
495 | ), |
||
496 | 'event' => array( |
||
497 | 'description' => __( 'Webhook event.', 'woocommerce' ), |
||
498 | 'type' => 'string', |
||
499 | 'context' => array( 'view', 'edit' ), |
||
500 | 'readonly' => true, |
||
501 | ), |
||
502 | 'hooks' => array( |
||
503 | 'description' => __( 'WooCommerce action names associated with the webhook.', 'woocommerce' ), |
||
504 | 'type' => 'array', |
||
505 | 'context' => array( 'view', 'edit' ), |
||
506 | 'readonly' => true, |
||
507 | ), |
||
508 | 'delivery_url' => array( |
||
509 | 'description' => __( 'The URL where the webhook payload is delivered.', 'woocommerce' ), |
||
510 | 'type' => 'string', |
||
511 | 'format' => 'uri', |
||
512 | 'context' => array( 'view', 'edit' ), |
||
513 | 'readonly' => true, |
||
514 | ), |
||
515 | 'secret' => array( |
||
516 | 'description' => __( "Secret key used to generate a hash of the delivered webhook and provided in the request headers. This will default is a MD5 hash from the current user's ID|username if not provided.", 'woocommerce' ), |
||
517 | 'type' => 'string', |
||
518 | 'format' => 'uri', |
||
519 | 'context' => array( 'edit' ), |
||
520 | ), |
||
521 | 'date_created' => array( |
||
522 | 'description' => __( "The date the webhook was created, in the site's timezone.", 'woocommerce' ), |
||
523 | 'type' => 'date-time', |
||
524 | 'context' => array( 'view', 'edit' ), |
||
525 | 'readonly' => true, |
||
526 | ), |
||
527 | 'date_modified' => array( |
||
528 | 'description' => __( "The date the webhook was last modified, in the site's timezone.", 'woocommerce' ), |
||
529 | 'type' => 'date-time', |
||
530 | 'context' => array( 'view', 'edit' ), |
||
531 | 'readonly' => true, |
||
532 | ), |
||
533 | ), |
||
534 | ); |
||
535 | |||
536 | return $this->add_additional_fields_schema( $schema ); |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Get the query params for collections of attachments. |
||
541 | * |
||
542 | * @return array |
||
543 | */ |
||
544 | public function get_collection_params() { |
||
558 | } |
||
559 |
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.