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 |
||
| 13 | class Tracking { |
||
| 14 | /** |
||
| 15 | * The assets version. |
||
| 16 | * |
||
| 17 | * @since 9.4.0 |
||
| 18 | * |
||
| 19 | * @var string Assets version. |
||
| 20 | */ |
||
| 21 | const ASSETS_VERSION = '1.0.0'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Slug of the product that we are tracking. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $product_name; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Connection manager object. |
||
| 32 | * |
||
| 33 | * @var Object |
||
| 34 | */ |
||
| 35 | private $connection; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Creates the Tracking object. |
||
| 39 | * |
||
| 40 | * @param String $product_name the slug of the product that we are tracking. |
||
| 41 | * @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
||
|
|
|||
| 42 | */ |
||
| 43 | public function __construct( $product_name = 'jetpack', $connection = null ) { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Universal method for for all tracking events triggered via the JavaScript client. |
||
| 54 | * |
||
| 55 | * @access public |
||
| 56 | */ |
||
| 57 | public function ajax_tracks() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Enqueue script necessary for tracking. |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function enqueue_tracks_scripts() { |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Send an event in Tracks. |
||
| 114 | * |
||
| 115 | * @param string $event_type Type of the event. |
||
| 116 | * @param array $data Data to send with the event. |
||
| 117 | * @param mixed $user username, user_id, or WP_user object. |
||
| 118 | */ |
||
| 119 | public function record_user_event( $event_type, $data = array(), $user = null ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Record an event in Tracks - this is the preferred way to record events from PHP. |
||
| 143 | * |
||
| 144 | * @param mixed $user username, user_id, or WP_user object. |
||
| 145 | * @param string $event_name The name of the event. |
||
| 146 | * @param array $properties Custom properties to send with the event. |
||
| 147 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred. |
||
| 148 | * |
||
| 149 | * @return bool true for success | \WP_Error if the event pixel could not be fired |
||
| 150 | */ |
||
| 151 | public function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Determines whether tracking should be enabled. |
||
| 175 | * |
||
| 176 | * @param Automattic\Jetpack\Terms_Of_Service $terms_of_service A Terms_Of_Service object. |
||
| 177 | * @param Automattic\Jetpack\Status $status A Status object. |
||
| 178 | * |
||
| 179 | * @return boolean True if tracking should be enabled, else false. |
||
| 180 | */ |
||
| 181 | public function should_enable_tracking( $terms_of_service, $status ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Procedurally build a Tracks Event Object. |
||
| 191 | * NOTE: Use this only when the simpler Automattic\Jetpack\Tracking->jetpack_tracks_record_event() function won't work for you. |
||
| 192 | * |
||
| 193 | * @param WP_user $user WP_user object. |
||
| 194 | * @param string $event_name The name of the event. |
||
| 195 | * @param array $properties Custom properties to send with the event. |
||
| 196 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred. |
||
| 197 | * |
||
| 198 | * @return \Jetpack_Tracks_Event|\WP_Error |
||
| 199 | */ |
||
| 200 | private function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get the identity to send to tracks. |
||
| 227 | * |
||
| 228 | * @param int $user_id The user id of the local user. |
||
| 229 | * |
||
| 230 | * @return array $identity |
||
| 231 | */ |
||
| 232 | public function tracks_get_identity( $user_id ) { |
||
| 271 | } |
||
| 272 |
This check looks for
@paramannotations 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.