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 |
||
7 | class WC_Stats { |
||
8 | /** |
||
9 | * @var Jetpack |
||
10 | **/ |
||
11 | private $jetpack; |
||
12 | |||
13 | /** |
||
14 | * @var WC_Stats |
||
15 | **/ |
||
16 | private static $instance = null; |
||
17 | |||
18 | static function init() { |
||
28 | |||
29 | public function isActiveStore() { |
||
30 | // Tracking only Site pages |
||
31 | if ( is_admin() ) { |
||
32 | return false; |
||
33 | } |
||
34 | // Make sure Jetpack is installed and active |
||
35 | if ( ! Jetpack::is_active() ) { |
||
36 | return false; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Make sure WooCommerce is installed and active |
||
41 | * |
||
42 | * This action is documented in https://docs.woocommerce.com/document/create-a-plugin |
||
43 | */ |
||
44 | if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { |
||
45 | return false; |
||
46 | } |
||
47 | |||
48 | return true; |
||
49 | } |
||
50 | |||
51 | public function __construct() { |
||
52 | $this->jetpack = Jetpack::init(); |
||
53 | // add store analytics for product views and add-to-cart events |
||
54 | add_action( 'wp_enqueue_scripts', array( $this, 'product_and_add_to_cart_events' ), 1 ); |
||
55 | |||
56 | // add store analytics for purchase events |
||
57 | add_action( 'woocommerce_payment_complete', array( $this, 'handle_purchase_event' ), 10, 1 ); |
||
58 | |||
59 | // add s.js at the end |
||
60 | add_action( 'wp_enqueue_scripts', array( $this, 'register_s_script' ), 10 ); |
||
61 | |||
62 | add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart_from_product_page' ), 10, 6 ); |
||
63 | } |
||
64 | |||
65 | public function get_cart_ids( $result, $item ) { |
||
69 | |||
70 | public function get_cart_quantities( $result, $item ) { |
||
74 | |||
75 | public function get_session_id() { |
||
76 | // NOTE: one session can have multiple id's, this method is not sufficient |
||
81 | |||
82 | public function register_s_script() { |
||
88 | |||
89 | public function product_and_add_to_cart_events() { |
||
121 | |||
122 | // this is when added from product page post request |
||
123 | View Code Duplication | public function capture_add_to_cart_from_product_page() { |
|
137 | |||
138 | View Code Duplication | public function handle_purchase_event() { |
|
152 | } |
||
153 | |||
155 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.