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 Jetpack_WooCommerce_Analytics_Universal 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 Jetpack_WooCommerce_Analytics_Universal, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Jetpack_WooCommerce_Analytics_Universal { |
||
| 21 | /** |
||
| 22 | * Jetpack_WooCommerce_Analytics_Universal constructor. |
||
| 23 | */ |
||
| 24 | public function __construct() { |
||
| 25 | // loading _wca |
||
| 26 | add_action( 'wp_head', array( $this, 'wp_head_top' ), 1 ); |
||
| 27 | |||
| 28 | // add to carts from non-product pages or lists (search, store etc.) |
||
| 29 | add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 ); |
||
| 30 | |||
| 31 | // loading s.js |
||
| 32 | add_action( 'wp_head', array( $this, 'wp_head_bottom' ), 999999 ); |
||
| 33 | |||
| 34 | // Capture cart events |
||
| 35 | add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 ); |
||
| 36 | add_action( 'woocommerce_bundled_item_before_add_to_cart', array( $this, 'capture_bundle_add_to_cart' ), 10, 6 ); |
||
| 37 | |||
| 38 | // single product page view |
||
| 39 | add_action( 'woocommerce_after_single_product', array( $this, 'capture_product_view' ) ); |
||
| 40 | |||
| 41 | add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) ); |
||
| 42 | add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) ); |
||
| 43 | add_action( 'wcct_before_cart_widget', array( $this, 'remove_from_cart' ) ); |
||
| 44 | add_filter( 'woocommerce_cart_item_remove', array( $this, 'remove_from_cart_attributes' ), 10, 2 ); |
||
| 45 | |||
| 46 | // cart checkout |
||
| 47 | add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) ); |
||
| 48 | |||
| 49 | // order confirmed |
||
| 50 | add_action( 'woocommerce_thankyou', array( $this, 'order_process' ), 10, 1 ); |
||
| 51 | add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart_via_quantity' ), 10, 1 ); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Make _wca available to queue events |
||
| 56 | */ |
||
| 57 | public function wp_head_top() { |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Place script to call s.js, Store Analytics |
||
| 69 | */ |
||
| 70 | public function wp_head_bottom() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * On product lists or other non-product pages, add an event listener to "Add to Cart" button click |
||
| 78 | */ |
||
| 79 | public function loop_session_events() { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * On the cart page, add an event listener for removal of product click |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function remove_from_cart() { |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Adds the product ID to the remove product link (for use by remove_from_cart above) if not present |
||
| 139 | * |
||
| 140 | * @param string $url url. |
||
| 141 | * @param string $key key. |
||
| 142 | * @return mixed. |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function remove_from_cart_attributes( $url, $key ) { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Gather relevant product information |
||
| 164 | * |
||
| 165 | * @param array $product product |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | public function get_product_details( $product ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Track a product page view |
||
| 179 | */ |
||
| 180 | public function capture_product_view() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * On the Checkout page, trigger an event for each product in the cart |
||
| 201 | */ |
||
| 202 | public function checkout_process() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * After the checkout process, fire an event for each item in the order |
||
| 237 | * |
||
| 238 | * @param string $order_id Order Id. |
||
| 239 | */ |
||
| 240 | public function order_process( $order_id ) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Listen for clicks on the "Update Cart" button to know if an item has been removed by |
||
| 269 | * updating its quantity to zero |
||
| 270 | */ |
||
| 271 | View Code Duplication | public function remove_from_cart_via_quantity() { |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Get the current user id |
||
| 295 | * |
||
| 296 | * @return int |
||
| 297 | */ |
||
| 298 | public function get_user_id() { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param $cart_item_key |
||
| 309 | * @param $product_id |
||
| 310 | * @param $quantity |
||
| 311 | * @param $variation_id |
||
| 312 | * @param $variation |
||
| 313 | * @param $cart_item_data |
||
| 314 | */ |
||
| 315 | public function capture_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param $product_id |
||
| 321 | * @param $quantity |
||
| 322 | * @param $variation_id |
||
| 323 | * @param $variations |
||
| 324 | * @param $bundled_item_cart_data |
||
| 325 | */ |
||
| 326 | public function capture_bundle_add_to_cart( $product_id, $quantity, $variation_id, $variations, $bundled_item_cart_data ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param $product_id |
||
| 332 | * @param $quantity |
||
| 333 | * @param $event |
||
| 334 | */ |
||
| 335 | public function capture_cart_event ( $product_id, $quantity ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param $product_id |
||
| 349 | * @param $quantity |
||
| 350 | * @param $event |
||
| 351 | */ |
||
| 352 | public function capture_event_in_session_data( $product_id, $quantity, $event ) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Gets product categories or varation attributes as a formatted concatenated string |
||
| 382 | * |
||
| 383 | * @param object $product WC_Product. |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | View Code Duplication | public function get_product_categories_concatenated( $product ) { |
|
| 407 | |||
| 408 | } |
||
| 409 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.