Completed
Push — add/implement_pre_connection_j... ( 959dbf...83993b )
by
unknown
06:09
created

Pre_Connection_JITM::get_messages()   D

Complexity

Conditions 16
Paths 142

Size

Total Lines 90

Duplication

Lines 12
Ratio 13.33 %

Importance

Changes 0
Metric Value
cc 16
nc 142
nop 3
dl 12
loc 90
rs 4.3915
c 0
b 0
f 0

How to fix   Long Method    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
 * Jetpack's Pre-Connection JITM class.
4
 *
5
 * @package automattic/jetpack-jitm
6
 */
7
8
namespace Automattic\Jetpack\JITMS;
9
10
use Automattic\Jetpack\Connection\Client;
11
use Automattic\Jetpack\Partner;
12
use Automattic\Jetpack\JITMS\JITM;
13
use Automattic\Jetpack\JITMS\Engine;
14
15
/**
16
 * Jetpack just in time messaging through out the admin
17
 *
18
 * @since 5.6.0
19
 */
20
class Pre_Connection_JITM extends JITM {
21
22
	const PACKAGE_VERSION = '1.0'; // TODO: Keep in sync with version specified in composer.json.
23
24
	/**
25
	 * Determines if JITMs are enabled.
26
	 *
27
	 * @return bool Enable JITMs.
28
	 */
29
	public function register() {
30
		add_action( 'current_screen', array( $this, 'prepare_jitms' ) );
31
		return true;
32
	}
33
34
	/**
35
	 * Retrieve the current message to display keyed on query string and message path
36
	 *
37
	 * @param string $message_path The message path to ask for.
38
	 * @param string $query The query string originally from the front end.
39
	 * @param bool   $full_jp_logo_exists If there is a full Jetpack logo already on the page.
40
	 *
41
	 * @return array The JITM's to show, or an empty array if there is nothing to show
42
	 */
43
	public function get_messages( $message_path, $query, $full_jp_logo_exists ) {
44
		$jitm_engine = new Engine();
45
46
		$query_string = array();
47
		if ( isset( $query ) ) {
48
			foreach ( explode( ',', $query ) as $query_item ) {
49
				$query_item                     = explode( '=', $query_item );
50
				$query_string[ $query_item[0] ] = isset( $query_item[1] ) ? $query_item[1] : null;
51
			}
52
			unset( $query_item );
53
		}
54
55
		$mobile_browser = jetpack_is_mobile( 'smart' );
56
		$user           = wp_get_current_user();
57
58
		// Unauthenticated or invalid requests just bail.
59
		if ( ! $user ) {
60
			return array();
61
		}
62
63
		$user_roles = implode( ',', $user->roles );
64
65
		$envelopes = $jitm_engine->get_top_messages( $message_path, $user->ID, $user_roles, $query_string, $mobile_browser );
0 ignored issues
show
Documentation introduced by
$query_string is of type array, but the function expects a string.

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...
Bug introduced by
It seems like $mobile_browser defined by jetpack_is_mobile('smart') on line 55 can also be of type string; however, Automattic\Jetpack\JITMS...ine::get_top_messages() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
66
67
		if ( ! is_array( $envelopes ) ) {
68
			return array();
69
		}
70
71
		$hidden_jitms = \Jetpack_Options::get_option( 'hide_jitm' );
72
73
		unset( $envelopes['last_response_time'] );
74
75
		/**
76
		 * Allow adding your own custom JITMs after a set of JITMs has been received.
77
		 *
78
		 * @since 6.9.0
79
		 * @since 8.3.0 - Added Message path.
80
		 *
81
		 * @param array  $envelopes    array of existing JITMs.
82
		 * @param string $message_path The message path to ask for.
83
		 */
84
		$envelopes = apply_filters( 'jetpack_jitm_received_envelopes', $envelopes, $message_path );
85
86
		foreach ( $envelopes as $idx => &$envelope ) {
87
88
			$dismissed_feature = isset( $hidden_jitms[ $envelope->feature_class ] ) && is_array( $hidden_jitms[ $envelope->feature_class ] ) ? $hidden_jitms[ $envelope->feature_class ] : null;
89
90
			// If the this feature class has been dismissed and the request has not passed the ttl, skip it as it's been dismissed.
91 View Code Duplication
			if ( is_array( $dismissed_feature ) && ( time() - $dismissed_feature['last_dismissal'] < $envelope->expires || $dismissed_feature['number'] >= $envelope->max_dismissal ) ) {
92
				unset( $envelopes[ $idx ] );
93
				continue;
94
			}
95
96
			$normalized_site_url = \Jetpack::build_raw_urls( get_home_url() );
97
98
			$url_params = array(
99
				'source' => "jitm-$envelope->id",
100
				'site'   => $normalized_site_url,
101
				'u'      => $user->ID,
102
			);
103
104
			// Get affiliate code and add it to the array of URL parameters.
105
			$aff = Partner::init()->get_partner_code( Partner::AFFILIATE_CODE );
106
			if ( '' !== $aff ) {
107
				$url_params['aff'] = $aff;
108
			}
109
110
			$envelope->url = add_query_arg( $url_params, 'https://jetpack.com/redirect/' );
111
112 View Code Duplication
			if ( $envelope->cta['hook'] ) {
113
				$envelope->url = apply_filters( 'jitm_' . $envelope->cta['hook'], $envelope->url );
114
				unset( $envelope->cta['hook'] );
115
			}
116
117 View Code Duplication
			if ( isset( $envelope->content['hook'] ) ) {
118
				$envelope->content = apply_filters( 'jitm_' . $envelope->content['hook'], $envelope->content );
119
				unset( $envelope->content['hook'] );
120
			}
121
122
			// No point in showing an empty message.
123
			if ( empty( $envelope->content['message'] ) ) {
124
				unset( $envelopes[ $idx ] );
125
				continue;
126
			}
127
128
			$envelope->content['icon'] = $this->generate_icon( $envelope->content['icon'], $full_jp_logo_exists );
129
		}
130
131
		return $envelopes;
132
	}
133
134
}
135