Completed
Push — add/jetpack-mobile-package ( d1f5cd )
by
unknown
07:53
created

Jetpack_User_Agent_Info   F

Complexity

Total Complexity 340

Size/Duplication

Total Lines 1525
Duplicated Lines 20.13 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 307
loc 1525
rs 0.8
c 0
b 0
f 0
wmc 340
lcom 1
cbo 0

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Jetpack_User_Agent_Info often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_User_Agent_Info, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
use Automattic\Jetpack\Mobile;
4
5
/**
6
 * Determine if the current User Agent matches the passed $kind
7
 *
8
 * @param string $kind Category of mobile device to check for.
9
 *                         Either: any, dumb, smart.
10
 * @param bool   $return_matched_agent Boolean indicating if the UA should be returned
11
 *
12
 * @return bool|string Boolean indicating if current UA matches $kind. If
13
 *                              $return_matched_agent is true, returns the UA string
14
 */
15
function jetpack_is_mobile( $kind = 'any', $return_matched_agent = false ) {
16
	return Mobile::is_mobile( $kind, $return_matched_agent );
17
}
18