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 | * Slug of the product that we are tracking. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | private $product_name; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Connection manager object. |
||
| 23 | * |
||
| 24 | * @var Object |
||
| 25 | */ |
||
| 26 | private $connection; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Creates the Tracking object. |
||
| 30 | * |
||
| 31 | * @param String $product_name the slug of the product that we are tracking. |
||
| 32 | * @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
||
|
|
|||
| 33 | */ |
||
| 34 | public function __construct( $product_name = 'jetpack', $connection = null ) { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Enqueue script necessary for tracking. |
||
| 45 | */ |
||
| 46 | View Code Duplication | public function enqueue_tracks_scripts() { |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Send an event in Tracks. |
||
| 60 | * |
||
| 61 | * @param string $event_type Type of the event. |
||
| 62 | * @param array $data Data to send with the event. |
||
| 63 | * @param mixed $user username, user_id, or WP_user object. |
||
| 64 | */ |
||
| 65 | public function record_user_event( $event_type, $data = array(), $user = null ) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Record an event in Tracks - this is the preferred way to record events from PHP. |
||
| 89 | * |
||
| 90 | * @param mixed $user username, user_id, or WP_user object. |
||
| 91 | * @param string $event_name The name of the event. |
||
| 92 | * @param array $properties Custom properties to send with the event. |
||
| 93 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred. |
||
| 94 | * |
||
| 95 | * @return bool true for success | \WP_Error if the event pixel could not be fired |
||
| 96 | */ |
||
| 97 | public function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Determines whether tracking should be enabled. |
||
| 121 | * |
||
| 122 | * @param Automattic\Jetpack\Terms_Of_Service $terms_of_service A Terms_Of_Service object. |
||
| 123 | * @param Automattic\Jetpack\Status $status A Status object. |
||
| 124 | * |
||
| 125 | * @return boolean True if tracking should be enabled, else false. |
||
| 126 | */ |
||
| 127 | public function should_enable_tracking( $terms_of_service, $status ) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Procedurally build a Tracks Event Object. |
||
| 137 | * NOTE: Use this only when the simpler Automattic\Jetpack\Tracking->jetpack_tracks_record_event() function won't work for you. |
||
| 138 | * |
||
| 139 | * @param WP_user $user WP_user object. |
||
| 140 | * @param string $event_name The name of the event. |
||
| 141 | * @param array $properties Custom properties to send with the event. |
||
| 142 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred. |
||
| 143 | * |
||
| 144 | * @return \Jetpack_Tracks_Event|\WP_Error |
||
| 145 | */ |
||
| 146 | private function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the identity to send to tracks. |
||
| 173 | * |
||
| 174 | * @param int $user_id The user id of the local user. |
||
| 175 | * |
||
| 176 | * @return array $identity |
||
| 177 | */ |
||
| 178 | public function tracks_get_identity( $user_id ) { |
||
| 217 | } |
||
| 218 |
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.