Completed
Push — add/implement_pre_connection_j... ( def69c...959dbf )
by
unknown
06:40
created

Engine   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 171
Duplicated Lines 3.51 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 6
loc 171
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A default_rules() 0 7 1
B preconnection_default_rules() 0 65 1
B get_top_messages() 0 34 6
A dismiss() 6 22 4

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * Jetpack's JITM Engine class.
4
 *
5
 * @package automattic/jetpack-jitm
6
 */
7
8
namespace Automattic\Jetpack\JITMS;
9
10
use Automattic\Jetpack\JITMS\Message;
11
12
/**
13
 * Class JITM\Engine
14
 *
15
 * Determines the rules of a JITM, which should display and when.
16
 */
17
class Engine {
18
19
	/**
20
	 * Is mobile browser
21
	 *
22
	 * @var bool $mobile_browser.
23
	 */
24
	private $mobile_browser;
25
26
	/**
27
	 * Returns the default rules
28
	 *
29
	 * @return array Default rules.
30
	 */
31
	public function default_rules() {
32
		$rules = array_merge(
33
			$this->preconnection_default_rules()
34
		);
35
36
		return apply_filters( 'jetpack_jitm_rules', $rules );
37
	}
38
39
	/**
40
	 * Returns the pre-connection JITMs default rules
41
	 *
42
	 * @return array Pre-connection JITMs default rules.
43
	 */
44
	private function preconnection_default_rules() {
45
46
		return array(
47
			( new Message( 'jpsetup-posts', 'pre-connect' ) )
48
				->user_is( 'administrator' )
49
				->with_icon()
50
				->message_path( '/wp:edit-post:admin_notices/' )
51
				->show(
52
					__( 'Do you know which of these posts gets the most traffic?', 'jetpack' ),
53
					__( 'Setup Jetpack to get in-depth stats about your content and visitors.', 'jetpack' )
54
				)
55
				->priority( 100 )
56
				->with_cta(
57
					__( 'Setup Jetpack', 'jetpack' ),
58
					'',
59
					function() {
0 ignored issues
show
Documentation introduced by
function () { return...ection-jitm-posts')); } is of type object<Closure>, 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...
60
						return esc_url( \Jetpack::init()->build_connect_url( true, false, 'pre-connection-jitm-posts' ) );
61
					}
62
				)
63
				->open_cta_in_same_window()
64
				->is_dismissible( true )
65
				->priority( 1000 )
66
				->is_hosted_with_partner( 'bluehost' ),
67
			( new Message( 'jpsetup-upload', 'pre-connect' ) )
68
				->user_is( 'administrator' )
69
				->with_icon()
70
				->message_path( '/wp:upload:admin_notices/' )
71
				->show(
72
					__( 'Do you want lightning-fast images?', 'jetpack' ),
73
					__( 'Setup Jetpack, enable Site Accelerator, and start serving your images lightning fast, for free.', 'jetpack' )
74
				)
75
				->priority( 100 )
76
				->with_cta(
77
					__( 'Setup Jetpack', 'jetpack' ),
78
					'',
79
					function() {
0 ignored issues
show
Documentation introduced by
function () { return...ction-jitm-upload')); } is of type object<Closure>, 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...
80
						return esc_url( \Jetpack::init()->build_connect_url( true, false, 'pre-connection-jitm-upload' ) );
81
					}
82
				)
83
				->open_cta_in_same_window()
84
				->is_dismissible( true )
85
				->priority( 1000 )
86
				->is_hosted_with_partner( 'bluehost' ),
87
			( new Message( 'jpsetup-widgets', 'pre-connect' ) )
88
				->user_is( 'administrator' )
89
				->with_icon()
90
				->message_path( '/wp:widgets:admin_notices/' )
91
				->show(
92
					__( 'Looking for even more widgets?', 'jetpack' ),
93
					__( 'Setup Jetpack for great additional widgets like business hours and maps.', 'jetpack' )
94
				)
95
				->priority( 100 )
96
				->with_cta(
97
					__( 'Setup Jetpack', 'jetpack' ),
98
					'',
99
					function() {
0 ignored issues
show
Documentation introduced by
function () { return...tion-jitm-widgets')); } is of type object<Closure>, 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...
100
						return esc_url( \Jetpack::init()->build_connect_url( true, false, 'pre-connection-jitm-widgets' ) );
101
					}
102
				)
103
				->open_cta_in_same_window()
104
				->is_dismissible( true )
105
				->priority( 1000 )
106
				->is_hosted_with_partner( 'bluehost' ),
107
		);
108
	}
109
110
	/**
111
	 * Gets the top messages
112
	 *
113
	 * @param string $message_path Message path.
114
	 * @param int    $user_id User ID.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $user_id not be integer|null?

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...
115
	 * @param string $user_level User level.
116
	 * @param string $query Message query.
117
	 * @param bool   $mobile_browser Uses mobile browser.
118
	 *
119
	 * @return array Rendered messages.
120
	 */
121
	public function get_top_messages( $message_path, $user_id = null, $user_level = '', $query = '', $mobile_browser = false ) {
122
		$rules = $this->default_rules();
123
124
		if ( is_string( $user_level ) ) {
125
			$user_level = explode( ',', $user_level );
126
		} else {
127
			$user_level = array();
128
		}
129
130
		$rules = array_reduce(
131
			$rules,
132
			function ( $return, $rule ) use ( $message_path, $user_id, $user_level, $query, $mobile_browser ) {
133
				$score = $rule->score( $message_path, $user_id, $user_level, $query, $mobile_browser );
134
				if ( $score > $return[0] ) {
135
					$return = array( $score, array( $rule->render() ), array( $rule ) );
136
				} elseif ( $score === $return[0] && $score > 0 ) {
137
					$return[1][] = $rule->render();
138
					$return[2][] = $rule;
139
				}
140
141
				return $return;
142
			},
143
			array( 0, array(), array() )
144
		);
145
146
		$rendered_rules = array();
147
148
		foreach ( $rules[2] as $rule ) {
149
			$rendered_rules[] = $rule->render();
150
		}
151
152
		// get the top message which hasn't been dismissed.
153
		return $rendered_rules;
154
	}
155
156
	/**
157
	 * Dismisses a JITM feature class so that it will no longer be shown
158
	 *
159
	 * @param string $id The id of the JITM that was dismissed.
160
	 * @param string $feature_class The feature class of the JITM that was dismissed.
161
	 *
162
	 * @return bool Always true
163
	 */
164
	public static function dismiss( $id, $feature_class ) {
165
		$hide_jitm = \Jetpack_Options::get_option( 'hide_jitm' );
166
		if ( ! is_array( $hide_jitm ) ) {
167
			$hide_jitm = array();
168
		}
169
170 View Code Duplication
		if ( empty( $hide_jitm[ $feature_class ] ) || ! is_array( $hide_jitm[ $feature_class ] ) ) {
171
			$hide_jitm[ $feature_class ] = array(
172
				'last_dismissal' => 0,
173
				'number'         => 0,
174
			);
175
		}
176
177
		$hide_jitm[ $feature_class ] = array(
178
			'last_dismissal' => time(),
179
			'number'         => intval( $hide_jitm[ $feature_class ]['number'] ) + 1,
180
		);
181
182
		\Jetpack_Options::update_option( 'hide_jitm', $hide_jitm );
183
184
		return true;
185
	}
186
187
}
188