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 |
||
15 | class Tracking { |
||
16 | private $product_name; |
||
17 | private $connection; |
||
18 | |||
19 | /** |
||
20 | * Creates the Tracking object. |
||
21 | * |
||
22 | * @param String $product_name the slug of the product that we are tracking. |
||
23 | * @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
||
|
|||
24 | */ |
||
25 | function __construct( $product_name = 'jetpack', $connection = null ) { |
||
26 | $this->product_name = $product_name; |
||
27 | $this->connection = $connection; |
||
28 | if ( is_null( $this->connection ) ) { |
||
29 | // TODO We should always pass a Connection. |
||
30 | $this->connection = new Connection\Manager(); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | View Code Duplication | function enqueue_tracks_scripts() { |
|
35 | wp_enqueue_script( 'jptracks', plugins_url( '_inc/lib/tracks/tracks-ajax.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); |
||
36 | wp_localize_script( |
||
37 | 'jptracks', |
||
38 | 'jpTracksAJAX', |
||
39 | array( |
||
40 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
||
41 | 'jpTracksAJAX_nonce' => wp_create_nonce( 'jp-tracks-ajax-nonce' ), |
||
42 | ) |
||
43 | ); |
||
44 | } |
||
45 | |||
46 | function record_user_event( $event_type, $data = array(), $user = null ) { |
||
47 | if ( ! $user ) { |
||
48 | $user = wp_get_current_user(); |
||
49 | } |
||
50 | $site_url = get_option( 'siteurl' ); |
||
51 | |||
52 | $data['_via_ua'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
||
53 | $data['_via_ip'] = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : ''; |
||
54 | $data['_lg'] = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''; |
||
55 | $data['blog_url'] = $site_url; |
||
56 | $data['blog_id'] = \Jetpack_Options::get_option( 'id' ); |
||
57 | |||
58 | // Top level events should not be namespaced |
||
59 | if ( '_aliasUser' != $event_type ) { |
||
60 | $event_type = $this->product_name . '_' . $event_type; |
||
61 | } |
||
62 | |||
63 | $data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '0'; |
||
64 | |||
65 | return $this->tracks_record_event( $user, $event_type, $data ); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Record an event in Tracks - this is the preferred way to record events from PHP. |
||
70 | * |
||
71 | * @param mixed $identity username, user_id, or WP_user object |
||
72 | * @param string $event_name The name of the event |
||
73 | * @param array $properties Custom properties to send with the event |
||
74 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
||
75 | * |
||
76 | * @return bool true for success | \WP_Error if the event pixel could not be fired |
||
77 | */ |
||
78 | function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
79 | |||
80 | // We don't want to track user events during unit tests/CI runs. |
||
81 | if ( $user instanceof \WP_User && 'wptests_capabilities' === $user->cap_key ) { |
||
82 | return false; |
||
83 | } |
||
84 | |||
85 | // Don't track users who have opted our, or not agreed to the TOS, or sites in development mode |
||
86 | $status = new Status(); |
||
87 | if ( ! \Jetpack::jetpack_tos_agreed() || ! empty( $_COOKIE['tk_opt-out'] ) || $status->is_development_mode() ) { |
||
88 | return false; |
||
89 | } |
||
90 | |||
91 | $event_obj = $this->tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis ); |
||
92 | |||
93 | if ( is_wp_error( $event_obj->error ) ) { |
||
94 | return $event_obj->error; |
||
95 | } |
||
96 | |||
97 | return $event_obj->record(); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Procedurally build a Tracks Event Object. |
||
102 | * NOTE: Use this only when the simpler Automattic\Jetpack\Tracking->jetpack_tracks_record_event() function won't work for you. |
||
103 | * |
||
104 | * @param $identity WP_user object |
||
105 | * @param string $event_name The name of the event |
||
106 | * @param array $properties Custom properties to send with the event |
||
107 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
||
108 | * |
||
109 | * @return \Jetpack_Tracks_Event|\WP_Error |
||
110 | */ |
||
111 | function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
112 | $identity = $this->tracks_get_identity( $user->ID ); |
||
113 | |||
114 | $properties['user_lang'] = $user->get( 'WPLANG' ); |
||
115 | |||
116 | $blog_details = array( |
||
117 | 'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' ), |
||
118 | ); |
||
119 | |||
120 | $timestamp = ( $event_timestamp_millis !== false ) ? $event_timestamp_millis : round( microtime( true ) * 1000 ); |
||
121 | $timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' ); |
||
122 | |||
123 | return new \Jetpack_Tracks_Event( |
||
124 | array_merge( |
||
125 | $blog_details, |
||
126 | (array) $properties, |
||
127 | $identity, |
||
128 | array( |
||
129 | '_en' => $event_name, |
||
130 | '_ts' => $timestamp_string, |
||
131 | ) |
||
132 | ) |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Get the identity to send to tracks. |
||
138 | * |
||
139 | * @param int $user_id The user id of the local user |
||
140 | * |
||
141 | * @return array $identity |
||
142 | */ |
||
143 | function tracks_get_identity( $user_id ) { |
||
182 | } |
||
183 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.