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 | function __construct( $product_name = 'jetpack' ) { |
||
| 21 | |||
| 22 | View Code Duplication | function enqueue_tracks_scripts() { |
|
| 23 | wp_enqueue_script( 'jptracks', plugins_url( '_inc/lib/tracks/tracks-ajax.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); |
||
| 24 | wp_localize_script( |
||
| 25 | 'jptracks', |
||
| 26 | 'jpTracksAJAX', |
||
| 27 | array( |
||
| 28 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
||
| 29 | 'jpTracksAJAX_nonce' => wp_create_nonce( 'jp-tracks-ajax-nonce' ), |
||
| 30 | ) |
||
| 31 | ); |
||
| 32 | } |
||
| 33 | |||
| 34 | function record_user_event( $event_type, $data = array(), $user = null ) { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Record an event in Tracks - this is the preferred way to record events from PHP. |
||
| 58 | * |
||
| 59 | * @param mixed $identity username, user_id, or WP_user object |
||
|
|
|||
| 60 | * @param string $event_name The name of the event |
||
| 61 | * @param array $properties Custom properties to send with the event |
||
| 62 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
||
| 63 | * |
||
| 64 | * @return bool true for success | \WP_Error if the event pixel could not be fired |
||
| 65 | */ |
||
| 66 | View Code Duplication | function tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Procedurally build a Tracks Event Object. |
||
| 84 | * NOTE: Use this only when the simpler Automattic\Jetpack\Tracking->jetpack_tracks_record_event() function won't work for you. |
||
| 85 | * |
||
| 86 | * @param $identity WP_user object |
||
| 87 | * @param string $event_name The name of the event |
||
| 88 | * @param array $properties Custom properties to send with the event |
||
| 89 | * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred |
||
| 90 | * |
||
| 91 | * @return \Jetpack_Tracks_Event|\WP_Error |
||
| 92 | */ |
||
| 93 | View Code Duplication | function tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) { |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Get the identity to send to tracks. |
||
| 120 | * |
||
| 121 | * @param int $user_id The user id of the local user |
||
| 122 | * |
||
| 123 | * @return array $identity |
||
| 124 | */ |
||
| 125 | function tracks_get_identity( $user_id ) { |
||
| 164 | } |
||
| 165 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.