Completed
Push — master ( 8f2f13...bf6127 )
by Gabriel
07:49
created

DeviceDetector   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 2
dl 31
loc 93
ccs 28
cts 30
cp 0.9333
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A detect() 0 11 4
A checkIpad() 9 9 2
A checkIphone() 9 9 2
A checkWindowsPhone() 13 13 3
A checkSamsungPhone() 0 9 2

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
        );
24
    }
25
26
    /**
27
     * Determine if the device is iPad.
28
     *
29
     * @param Device $device
30
     * @param UserAgent $userAgent
31
     * @return bool
32
     */
33 2 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...
34
    {
35 2
        if (stripos($userAgent->getUserAgentString(), 'ipad') !== false) {
36 1
            $device->setName(Device::IPAD);
37 1
            return true;
38
        }
39
40 2
        return false;
41
    }
42
43
    /**
44
     * Determine if the device is iPhone.
45
     *
46
     * @param Device $device
47
     * @param UserAgent $userAgent
48
     * @return bool
49
     */
50 2 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...
51
    {
52 2
        if (stripos($userAgent->getUserAgentString(), 'iphone;') !== false) {
53 1
            $device->setName(Device::IPHONE);
54 1
            return true;
55
        }
56
57 2
        return false;
58
    }
59
60
    /**
61
     * Determine if the device is Windows Phone.
62
     *
63
     * @param Device $device
64
     * @param UserAgent $userAgent
65
     * @return bool
66
     */
67 2 View Code Duplication
    private static function checkWindowsPhone(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...
68
    {
69 2
        if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) {
70 1
            if (preg_match('/Microsoft; (Lumia [^)]*)\)/', $userAgent->getUserAgentString(), $matches)) {
71 1
                $device->setName($matches[1]);
72 1
                return true;
73
            }
74
75
            $device->setName($device::WINDOWS_PHONE);
76
            return true;
77
        }
78 2
        return false;
79
    }
80
81
    /**
82
     * Determine if the device is Windows Phone.
83
     *
84
     * @param Device $device
85
     * @param UserAgent $userAgent
86
     * @return bool
87
     */
88 2
    private static function checkSamsungPhone(Device $device, UserAgent $userAgent)
89
    {
90 2
            if (preg_match('/SAMSUNG SM-([^ ]*)/i', $userAgent->getUserAgentString(), $matches)) {
91 1
                $device->setName(str_ireplace('SAMSUNG', 'Samsung', $matches[0]));
92 1
                return true;
93
            }
94
95 2
        return false;
96
    }
97
}
98