Completed
Push — master ( 356eb6...32a6a0 )
by Gabriel
07:28
created

DeviceDetector   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 41.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.67%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 10
c 3
b 0
f 1
lcom 1
cbo 2
dl 31
loc 75
ccs 22
cts 24
cp 0.9167
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A detect() 0 10 3
A checkIpad() 9 9 2
A checkIphone() 9 9 2
A checkWindowsPhone() 13 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
        );
23
    }
24
25
    /**
26
     * Determine if the device is iPad.
27
     *
28
     * @param Device $device
29
     * @param UserAgent $userAgent
30
     * @return bool
31
     */
32 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...
33
    {
34 2
        if (stripos($userAgent->getUserAgentString(), 'ipad') !== false) {
35 1
            $device->setName(Device::IPAD);
36 1
            return true;
37
        }
38
39 2
        return false;
40
    }
41
42
    /**
43
     * Determine if the device is iPhone.
44
     *
45
     * @param Device $device
46
     * @param UserAgent $userAgent
47
     * @return bool
48
     */
49 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...
50
    {
51 2
        if (stripos($userAgent->getUserAgentString(), 'iphone;') !== false) {
52 1
            $device->setName(Device::IPHONE);
53 1
            return true;
54
        }
55
56 2
        return false;
57
    }
58
59
    /**
60
     * Determine if the device is Windows Phone.
61
     *
62
     * @param Device $device
63
     * @param UserAgent $userAgent
64
     * @return bool
65
     */
66 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...
67
    {
68 2
        if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) {
69 1
            if (preg_match('/Microsoft; (Lumia [^)]*)\)/', $userAgent->getUserAgentString(), $matches)) {
70 1
                $device->setName($matches[1]);
71 1
                return true;
72
            }
73
74
            $device->setName($device::WINDOWS_PHONE);
75
            return true;
76
        }
77 2
        return false;
78
    }
79
}
80