Completed
Push — fix/nosara ( 93ba64...aec843 )
by
unknown
19:14 queued 09:54
created

client.php ➔ jetpack_tracks_get_identity()   C

Complexity

Conditions 10
Paths 11

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 25
nc 11
nop 1
dl 0
loc 47
rs 5.1578
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PHP Tracks Client
4
 * @autounit nosara tracks-client
5
 * Example Usage:
6
 *
7
```php
8
	include( plugin_dir_path( __FILE__ ) . 'lib/tracks/client.php');
9
	$result = jetpack_tracks_record_event( $user, $event_name, $properties );
10
11
	if ( is_wp_error( $result ) ) {
12
		// Handle the error in your app
13
	}
14
```
15
 */
16
17
// Load the client classes
18
require_once( dirname(__FILE__) . '/class.tracks-event.php' );
19
require_once( dirname(__FILE__) . '/class.tracks-client.php' );
20
21
// Now, let's export a sprinkling of syntactic sugar!
22
23
/**
24
 * Procedurally (vs. Object-oriented), track an event object (or flat array)
25
 * NOTE: Use this only when the simpler jetpack_tracks_record_event() function won't work for you.
26
 * @param \Jetpack_Tracks_Event $event The event object.
27
 * @return \Jetpack_Tracks_Event|\WP_Error
28
 */
29
function jetpack_tracks_record_event_raw( $event ) {
30
	return Jetpack_Tracks_Client::record_event( $event );
31
}
32
33
/**
34
 * Procedurally build a Tracks Event Object.
35
 * NOTE: Use this only when the simpler jetpack_tracks_record_event() function won't work for you.
36
 * @param $identity WP_user object
37
 * @param string $event_name The name of the event
38
 * @param array $properties Custom properties to send with the event
39
 * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred
0 ignored issues
show
Documentation introduced by
Should the type for parameter $event_timestamp_millis not be false|integer?

This check looks for @param annotations 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.

Loading history...
40
 * @return \Jetpack_Tracks_Event|\WP_Error
41
 */
42
function jetpack_tracks_build_event_obj( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
43
44
	$identity = jetpack_tracks_get_identity( $user->ID );
45
46
	$properties['user_lang'] = $user->get( 'WPLANG' );
47
48
	$blog_details = array(
49
		'blog_lang' => isset( $properties['blog_lang'] ) ? $properties['blog_lang'] : get_bloginfo( 'language' )
50
	);
51
52
	$timestamp = ( $event_timestamp_millis !== false ) ? $event_timestamp_millis : round( microtime( true ) * 1000 );
53
	$timestamp_string = is_string( $timestamp ) ? $timestamp : number_format( $timestamp, 0, '', '' );
54
55
	return new Jetpack_Tracks_Event( array_merge( $blog_details, (array) $properties, $identity, array(
56
		'_en' => $event_name,
57
		'_ts' => $timestamp_string
58
	) ) );
59
}
60
61
/*
62
 * Get the identity to send to tracks.
63
 *
64
 * @param int $user_id The user id of the local user
65
 * @return array $identity
66
 */
67
function jetpack_tracks_get_identity( $user_id ) {
68
69
	// Meta is set, and user is still connected.  Use WPCOM ID
70
	$wpcom_id = get_user_meta( $user_id, 'jetpack_tracks_wpcom_id', true );
71
	if ( $wpcom_id && Jetpack::is_user_connected( $user_id ) ) {
0 ignored issues
show
Documentation introduced by
$user_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
		return array(
73
			'_ut' => 'wpcom:user_id',
74
			'_ui' => $wpcom_id
75
		);
76
	}
77
78
	// User is connected, but no meta is set yet.  Use WPCOM ID and set meta.
79
	if ( Jetpack::is_user_connected( $user_id ) ) {
0 ignored issues
show
Documentation introduced by
$user_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
		$wpcom_user_data = Jetpack::get_connected_user_data( $user_id );
81
		add_user_meta( $user_id, 'jetpack_tracks_wpcom_id', $wpcom_user_data['ID'], true );
82
83
		return array(
84
			'_ut' => 'wpcom:user_id',
85
			'_ui' => $wpcom_user_data['ID']
86
		);
87
	}
88
89
	if ( 0 === $user_id ) {
90
		return array();
91
	}
92
93
	// User isn't linked at all.  Fall back to anonymous ID.
94
	$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
95
	if ( ! $anon_id ) {
96
		$anon_id = Jetpack_Tracks_Client::get_anon_id();
97
		update_user_meta( $user_id, 'jetpack_tracks_anon_id', $anon_id );
98
	}
99
100
	if ( isset( $_COOKIE['tk_ai'] ) && $anon_id !== $_COOKIE['tk_ai'] ) {
101
		unset( $_COOKIE['tk_ai'] );
102
	}
103
104
	if ( ! isset( $_COOKIE[ 'tk_ai' ] ) && ! headers_sent() ) {
105
		setcookie( 'tk_ai', $anon_id, 0, '/' );
106
	}
107
108
	return array(
109
		'_ut' => 'anon',
110
		'_ui' => $anon_id
111
	);
112
113
}
114
115
/**
116
 * Record an event in Tracks - this is the preferred way to record events from PHP.
117
 *
118
 * @param mixed $identity username, user_id, or WP_user object
0 ignored issues
show
Bug introduced by
There is no parameter named $identity. Was it maybe removed?

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 $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
119
 * @param string $event_name The name of the event
120
 * @param array $properties Custom properties to send with the event
121
 * @param int $event_timestamp_millis The time in millis since 1970-01-01 00:00:00 when the event occurred
0 ignored issues
show
Documentation introduced by
Should the type for parameter $event_timestamp_millis not be false|integer?

This check looks for @param annotations 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.

Loading history...
122
 * @return bool true for success | \WP_Error if the event pixel could not be fired
123
 */
124
function jetpack_tracks_record_event( $user, $event_name, $properties = array(), $event_timestamp_millis = false ) {
125
126
	// We don't want to track user events during unit tests/CI runs.
127
	if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) {
0 ignored issues
show
Bug introduced by
The class WP_User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
128
		return false;
129
	}
130
131
	$event_obj = jetpack_tracks_build_event_obj( $user, $event_name, $properties, $event_timestamp_millis );
132
133
	if ( is_wp_error( $event_obj->error ) ) {
134
		return $event_obj->error;
135
	}
136
137
	return $event_obj->record();
138
}
139