Test Failed
Pull Request — master (#92)
by
unknown
03:16
created

DeviceDetector   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 116
Duplicated Lines 15.52 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 18
loc 116
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A detect() 0 12 5
A checkIpad() 9 9 2
A checkIphone() 9 9 2
A checkWindowsPhone() 0 13 3
A checkSamsungPhone() 0 9 2
A checkAndroidPhone() 0 13 3

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
namespace Sinergi\BrowserDetector;
4
5
class DeviceDetector implements DetectorInterface
6
{
7
    /**
8
     * Determine the user's device.
9
     *
10
     * @param Device $device
11
     * @param UserAgent $userAgent
12
     * @return bool
13
     */
14 2
    public static function detect(Device $device, UserAgent $userAgent)
15
    {
16 2
        $device->setName($device::UNKNOWN);
17
18
        return (
19 2
            self::checkIpad($device, $userAgent) ||
20 2
            self::checkIphone($device, $userAgent) ||
21 2
            self::checkWindowsPhone($device, $userAgent) ||
22 2
            self::checkSamsungPhone($device, $userAgent) ||
23 2
            self::checkAndroidPhone($device, $userAgent)
24
        );
25
    }
26
27
    /**
28
     * Determine if the device is iPad.
29
     *
30
     * @param Device $device
31
     * @param UserAgent $userAgent
32
     * @return bool
33 2
     */
34 View Code Duplication
    private static function checkIpad(Device $device, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35 2
    {
36 1
        if (stripos($userAgent->getUserAgentString(), 'ipad') !== false) {
37 1
            $device->setName(Device::IPAD);
38
            return true;
39
        }
40 2
41
        return false;
42
    }
43
44
    /**
45
     * Determine if the device is iPhone.
46
     *
47
     * @param Device $device
48
     * @param UserAgent $userAgent
49
     * @return bool
50 2
     */
51 View Code Duplication
    private static function checkIphone(Device $device, UserAgent $userAgent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52 2
    {
53 1
        if (stripos($userAgent->getUserAgentString(), 'iphone;') !== false) {
54 1
            $device->setName(Device::IPHONE);
55
            return true;
56
        }
57 2
58
        return false;
59
    }
60
61
    /**
62
     * Determine if the device is Windows Phone.
63
     *
64
     * @param Device $device
65
     * @param UserAgent $userAgent
66
     * @return bool
67 2
     */
68
    private static function checkWindowsPhone(Device $device, UserAgent $userAgent)
69 2
    {
70 1
        if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) {
71 1
            if (preg_match('/Microsoft; (Lumia [^)]*)\)/', $userAgent->getUserAgentString(), $matches)) {
72 1
                $device->setName($matches[1]);
73
                return true;
74
            }
75
76
            $device->setName($device::WINDOWS_PHONE);
77
            return true;
78 2
        }
79
        return false;
80
    }
81
82
    /**
83
     * Determine if the device is Samsung Phone.
84
     *
85
     * @param Device $device
86
     * @param UserAgent $userAgent
87
     * @return bool
88 2
     */
89
    private static function checkSamsungPhone(Device $device, UserAgent $userAgent)
90 2
    {
91 1
            if (preg_match('/SAMSUNG SM-([^ ]*)/i', $userAgent->getUserAgentString(), $matches)) {
92 1
                $device->setName(str_ireplace('SAMSUNG', 'Samsung', $matches[0]));
93
                return true;
94 2
            }
95
96
        return false;
97
    }
98
99
100
    /**
101
     * Determine if the device is an Android Phone if Chrome is in use, and returns the Model-Code in $device setName
102
     *
103
     * @param Device $device
104
     * @param UserAgent $userAgent
105
     * @return bool
106
     */
107
    private static function checkAndroidPhone(Device $device, UserAgent $userAgent)
108
    {
109
        if (stripos($userAgent->getUserAgentString(), 'Android') !== false) {
110
            if (preg_match('/Linux; (Android [0-9\.]*); (?<modell>.*)\) AppleWebKit/', $userAgent->getUserAgentString(), $matches)) {
111
                $device->setName("Android_".preg_replace('/ Build.*$/', '', $matches["modell"]));
112
                return true;
113
            }
114
115
            $device->setName($device::ANDROID_PHONE);
116
            return true;
117
        }
118
        return false;
119
    }
120
}
121