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 | private $product_name; |
||
| 15 | private $connection; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Creates the Tracking object. |
||
| 19 | * |
||
| 20 | * @param String $product_name the slug of the product that we are tracking. |
||
| 21 | * @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
||
|
|
|||
| 22 | */ |
||
| 23 | function __construct( $product_name = 'jetpack', $connection = null ) { |
||
| 31 | |||
| 32 | View Code Duplication | function enqueue_tracks_scripts() { |
|
| 43 | |||
| 44 | function record_user_event( $event_type, $data = array(), $user = null ) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Record an event in Tracks - this is the preferred way to record events from PHP. |
||
| 68 | * |
||
| 69 | * @param mixed $identity username, user_id, or WP_user object |
||
| 70 | * @param string $event_name The name of the event |
||
| 71 | * @param array $properties Custom properties to send with the event |
||
| 72 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
||
| 73 | * |
||
| 74 | * @return bool true for success | \WP_Error if the event pixel could not be fired |
||
| 75 | */ |
||
| 76 | function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 77 | |||
| 78 | // We don't want to track user events during unit tests/CI runs. |
||
| 79 | if ( $user instanceof \WP_User && 'wptests_capabilities' === $user->cap_key ) { |
||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | $event_obj = $this->tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis ); |
||
| 84 | |||
| 85 | if ( is_wp_error( $event_obj->error ) ) { |
||
| 86 | return $event_obj->error; |
||
| 87 | } |
||
| 88 | |||
| 89 | return $event_obj->record(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Procedurally build a Tracks Event Object. |
||
| 94 | * NOTE: Use this only when the simpler Automattic\Jetpack\Tracking->jetpack_tracks_record_event() function won't work for you. |
||
| 95 | * |
||
| 96 | * @param $identity WP_user object |
||
| 97 | * @param string $event_name The name of the event |
||
| 98 | * @param array $properties Custom properties to send with the event |
||
| 99 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
||
| 100 | * |
||
| 101 | * @return \Jetpack_Tracks_Event|\WP_Error |
||
| 102 | */ |
||
| 103 | function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 104 | $identity = $this->tracks_get_identity( $user->ID ); |
||
| 105 | |||
| 106 | $properties['user_lang'] = $user->get( 'WPLANG' ); |
||
| 107 | |||
| 108 | $blog_details = array( |
||
| 109 | 'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' ), |
||
| 110 | ); |
||
| 111 | |||
| 112 | $timestamp = ( $event_timestamp_millis !== false ) ? $event_timestamp_millis : round( microtime( true ) * 1000 ); |
||
| 113 | $timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' ); |
||
| 114 | |||
| 115 | return new \Jetpack_Tracks_Event( |
||
| 116 | array_merge( |
||
| 117 | $blog_details, |
||
| 118 | (array) $properties, |
||
| 119 | $identity, |
||
| 120 | array( |
||
| 121 | '_en' => $event_name, |
||
| 122 | '_ts' => $timestamp_string, |
||
| 123 | ) |
||
| 124 | ) |
||
| 125 | ); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the identity to send to tracks. |
||
| 130 | * |
||
| 131 | * @param int $user_id The user id of the local user |
||
| 132 | * |
||
| 133 | * @return array $identity |
||
| 134 | */ |
||
| 135 | function tracks_get_identity( $user_id ) { |
||
| 174 | } |
||
| 175 |
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.