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 | /** |
||
| 17 | * Slug of the product that we are tracking. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $product_name; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Connection manager object. |
||
| 25 | * |
||
| 26 | * @var Object |
||
| 27 | */ |
||
| 28 | private $connection; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Creates the Tracking object. |
||
| 32 | * |
||
| 33 | * @param String $product_name the slug of the product that we are tracking. |
||
| 34 | * @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
||
|
|
|||
| 35 | */ |
||
| 36 | public function __construct( $product_name = 'jetpack', $connection = null ) { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Enqueue script necessary for tracking. |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function enqueue_tracks_scripts() { |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Send an event in Tracks. |
||
| 62 | * |
||
| 63 | * @param string $event_type Type of the event. |
||
| 64 | * @param array $data Data to send with the event. |
||
| 65 | * @param mixed $user username, user_id, or WP_user object. |
||
| 66 | */ |
||
| 67 | public function record_user_event( $event_type, $data = array(), $user = null ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Record an event in Tracks - this is the preferred way to record events from PHP. |
||
| 91 | * |
||
| 92 | * @param mixed $user username, user_id, or WP_user object. |
||
| 93 | * @param string $event_name The name of the event. |
||
| 94 | * @param array $properties Custom properties to send with the event. |
||
| 95 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred. |
||
| 96 | * |
||
| 97 | * @return bool true for success | \WP_Error if the event pixel could not be fired |
||
| 98 | */ |
||
| 99 | public function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Procedurally build a Tracks Event Object. |
||
| 125 | * NOTE: Use this only when the simpler Automattic\Jetpack\Tracking->jetpack_tracks_record_event() function won't work for you. |
||
| 126 | * |
||
| 127 | * @param WP_user $user WP_user object. |
||
| 128 | * @param string $event_name The name of the event. |
||
| 129 | * @param array $properties Custom properties to send with the event. |
||
| 130 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred. |
||
| 131 | * |
||
| 132 | * @return \Jetpack_Tracks_Event|\WP_Error |
||
| 133 | */ |
||
| 134 | private function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the identity to send to tracks. |
||
| 161 | * |
||
| 162 | * @param int $user_id The user id of the local user. |
||
| 163 | * |
||
| 164 | * @return array $identity |
||
| 165 | */ |
||
| 166 | public function tracks_get_identity( $user_id ) { |
||
| 205 | } |
||
| 206 |
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.