Completed
Push — add/jetpack-mobile-package ( 2965e8...6108e0 )
by
unknown
40:21 queued 32:22
created

Jetpack_User_Agent_Info::isTierIphone()   D

Complexity

Conditions 19
Paths 18

Size

Total Lines 89

Duplication

Lines 48
Ratio 53.93 %

Importance

Changes 0
Metric Value
cc 19
nc 18
nop 0
dl 48
loc 89
rs 4.5166
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
 * Device detection for Jetpack.
4
 *
5
 * @package automattic/jetpack-device-detection
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Automattic\Jetpack\Device_Detection\User_Agent_Info;
11
12
/**
13
 * Class Device_Detection
14
 *
15
 * Determine if the current User Agent matches the passed $kind.
16
 */
17
class Device_Detection {
18
19
	/**
20
	 * Returns information about the current device accessing the page.
21
	 *
22
	 * @param string $ua (Optional) User-Agent string.
23
	 *
24
	 * @return array Device information.
25
	 *
26
	 * array(
27
	 *  'is_phone'            => (bool) Whether the current device is a mobile phone.
28
	 *  'is_smartphone'       => (bool) Whether the current device is a smartphone.
29
	 *  'is_tablet'           => (bool) Whether the current device is a tablet device.
30
	 *  'is_handheld'         => (bool) Whether the current device is a handheld device.
31
	 *  'is_desktop'          => (bool) Whether the current device is a laptop / desktop device.
32
	 *  'platform'            => (string) Detected platform.
33
	 *  'is_phone_matched_ua' => (string) Matched UA.
34
	 * );
35
	 */
36
	public static function get_info( $ua = '' ) {
37
		$ua_info = new User_Agent_Info( $ua );
38
39
		$info = array(
40
			'is_phone'            => self::is_mobile( 'any', false, $ua_info ),
41
			'is_phone_matched_ua' => self::is_mobile( 'any', true, $ua_info ),
42
			'is_smartphone'       => self::is_mobile( 'smart', false, $ua_info ),
43
			'is_tablet'           => $ua_info->is_tablet(),
44
			'platform'            => $ua_info->get_platform(),
45
		);
46
47
		$info['is_handheld'] = $info['is_phone'] || $info['is_tablet'];
48
		$info['is_desktop']  = ! $info['is_handheld'];
49
50
		if ( function_exists( 'apply_filters' ) ) {
51
			$info = apply_filters( 'jetpack_device_detection_get_info', $info, $ua, $ua_info );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $ua.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
52
		}
53
		return $info;
54
	}
55
56
	/**
57
	 * Detects phone devices.
58
	 *
59
	 * @param string $ua User-Agent string.
60
	 *
61
	 * @return bool
62
	 */
63
	public static function is_phone( $ua = '' ) {
64
		$device_info = self::get_info( $ua );
65
		return true === $device_info['is_phone'];
66
	}
67
68
	/**
69
	 * Detects smartphone devices.
70
	 *
71
	 * @param string $ua User-Agent string.
72
	 *
73
	 * @return bool
74
	 */
75
	public static function is_smartphone( $ua = '' ) {
76
		$device_info = self::get_info( $ua );
77
		return true === $device_info['is_smartphone'];
78
	}
79
80
	/**
81
	 * Detects tablet devices.
82
	 *
83
	 * @param string $ua User-Agent string.
84
	 *
85
	 * @return bool
86
	 */
87
	public static function is_tablet( $ua = '' ) {
88
		$device_info = self::get_info( $ua );
89
		return true === $device_info['is_tablet'];
90
	}
91
92
	/**
93
	 * Detects desktop devices.
94
	 *
95
	 * @param string $ua User-Agent string.
96
	 *
97
	 * @return bool
98
	 */
99
	public static function is_desktop( $ua = '' ) {
100
		$device_info = self::get_info( $ua );
101
		return true === $device_info['is_desktop'];
102
	}
103
104
	/**
105
	 * Detects handheld (i.e. phone + tablet) devices.
106
	 *
107
	 * @param string $ua User-Agent string.
108
	 *
109
	 * @return bool
110
	 */
111
	public static function is_handheld( $ua = '' ) {
112
		$device_info = self::get_info( $ua );
113
		return true === $device_info['is_handheld'];
114
	}
115
116
	/**
117
	 * Determine if the current User Agent matches the passed $kind.
118
	 *
119
	 * @param string          $kind                 Category of mobile device to check for. Either: any, dumb, smart.
120
	 * @param bool            $return_matched_agent Boolean indicating if the UA should be returned.
121
	 * @param User_Agent_Info $ua_info              Boolean indicating if the UA should be returned.
122
	 *
123
	 * @return bool|string Boolean indicating if current UA matches $kind. If `$return_matched_agent` is true, returns the UA string.
124
	 */
125
	private static function is_mobile( $kind = 'any', $return_matched_agent = false, $ua_info ) {
126
		$kinds         = array(
127
			'smart' => false,
128
			'dumb'  => false,
129
			'any'   => false,
130
		);
131
		$first_run     = true;
132
		$matched_agent = '';
133
134
		// If an invalid kind is passed in, reset it to default.
135
		if ( ! isset( $kinds[ $kind ] ) ) {
136
				$kind = 'any';
137
		}
138
139
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) || strpos( strtolower( $_SERVER['HTTP_USER_AGENT'] ), 'ipad' ) ) {
140
			return false;
141
		}
142
143
		// Remove Samsung Galaxy tablets (SCH-I800) from being mobile devices
144
		if ( strpos( strtolower( $_SERVER['HTTP_USER_AGENT'] ), 'sch-i800' ) ) {
145
			return false;
146
		}
147
148
		if ( $ua_info->is_android_tablet() && false === $ua_info->is_kindle_touch() ) {
149
			return false;
150
		}
151
152
		if ( $ua_info->is_blackberry_tablet() ) {
153
			return false;
154
		}
155
156
		if ( $first_run ) {
157
			$first_run = false;
0 ignored issues
show
Unused Code introduced by
$first_run is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158
159
			// checks for iPhoneTier devices & RichCSS devices
160
			if ( $ua_info->isTierIphone() || $ua_info->isTierRichCSS() ) {
161
				$kinds['smart'] = true;
162
				$matched_agent  = $ua_info->matched_agent;
163
			}
164
165
			if ( ! $kinds['smart'] ) {
166
				// if smart, we are not dumb so no need to check
167
				$dumb_agents = $ua_info->dumb_agents;
168
				$agent       = strtolower( $_SERVER['HTTP_USER_AGENT'] );
169
170
				foreach ( $dumb_agents as $dumb_agent ) {
171
					if ( false !== strpos( $agent, $dumb_agent ) ) {
172
						$kinds['dumb'] = true;
173
						$matched_agent = $dumb_agent;
174
175
						break;
176
					}
177
				}
178
179
				if ( ! $kinds['dumb'] ) {
180
					if ( isset( $_SERVER['HTTP_X_WAP_PROFILE'] ) ) {
181
						$kinds['dumb'] = true;
182
						$matched_agent = 'http_x_wap_profile';
183
					} elseif ( isset( $_SERVER['HTTP_ACCEPT'] ) && ( preg_match( '/wap\.|\.wap/i', $_SERVER['HTTP_ACCEPT'] ) || false !== strpos( strtolower( $_SERVER['HTTP_ACCEPT'] ), 'application/vnd.wap.xhtml+xml' ) ) ) {
184
						$kinds['dumb'] = true;
185
						$matched_agent = 'vnd.wap.xhtml+xml';
186
					}
187
				}
188
			}
189
190
			if ( $kinds['dumb'] || $kinds['smart'] ) {
191
				$kinds['any'] = true;
192
			}
193
		}
194
195
		$value = $kinds[ $kind ];
196
197
		if ( $return_matched_agent ) {
198
			$value = $matched_agent;
199
		}
200
		return $value;
201
	}
202
}
203