GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#57)
by Martin
08:10
created

DonatjUAParser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 2.46 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 0 Features 1
Metric Value
wmc 9
c 11
b 0
f 1
lcom 1
cbo 6
dl 3
loc 122
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 3 6 2
A hasResult() 0 8 2
A hydrateBrowser() 0 10 3
B parse() 0 28 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
namespace UserAgentParser\Provider;
3
4
use UserAgentParser\Exception\NoResultFoundException;
5
use UserAgentParser\Exception\PackageNotLoadedException;
6
use UserAgentParser\Model;
7
8
class DonatjUAParser extends AbstractProvider
9
{
10
    /**
11
     * Name of the provider
12
     *
13
     * @var string
14
     */
15
    protected $name = 'DonatjUAParser';
16
17
    /**
18
     * Homepage of the provider
19
     *
20
     * @var string
21
     */
22
    protected $homepage = 'https://github.com/donatj/PhpUserAgent';
23
24
    /**
25
     * Composer package name
26
     *
27
     * @var string
28
     */
29
    protected $packageName = 'donatj/phpuseragentparser';
30
31
    protected $detectionCapabilities = [
32
33
        'browser' => [
34
            'name'    => true,
35
            'version' => true,
36
        ],
37
38
        'renderingEngine' => [
39
            'name'    => false,
40
            'version' => false,
41
        ],
42
43
        'operatingSystem' => [
44
            'name'    => false,
45
            'version' => false,
46
        ],
47
48
        'device' => [
49
            'model'    => false,
50
            'brand'    => false,
51
            'type'     => false,
52
            'isMobile' => false,
53
            'isTouch'  => false,
54
        ],
55
56
        'bot' => [
57
            'isBot' => false,
58
            'name'  => false,
59
            'type'  => false,
60
        ],
61
    ];
62
63 9
    public function __construct()
64
    {
65 9 View Code Duplication
        if (! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66 1
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
67
        }
68 8
    }
69
70
    /**
71
     *
72
     * @param array $resultRaw
73
     *
74
     * @return bool
75
     */
76 2
    private function hasResult(array $resultRaw)
77
    {
78 2
        if ($this->isRealResult($resultRaw['browser'])) {
79 1
            return true;
80
        }
81
82 1
        return false;
83
    }
84
85
    /**
86
     *
87
     * @param Model\Browser $browser
88
     * @param array         $resultRaw
89
     */
90 1
    private function hydrateBrowser(Model\Browser $browser, array $resultRaw)
91
    {
92 1
        if ($this->isRealResult($resultRaw['browser']) === true) {
93 1
            $browser->setName($resultRaw['browser']);
94 1
        }
95
96 1
        if ($this->isRealResult($resultRaw['version']) === true) {
97 1
            $browser->getVersion()->setComplete($resultRaw['version']);
98 1
        }
99 1
    }
100
101 2
    public function parse($userAgent, array $headers = [])
102
    {
103 2
        $resultRaw = parse_user_agent($userAgent);
104
105 2
        if ($this->hasResult($resultRaw) !== true) {
106 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
107
        }
108
109
        /*
110
         * Hydrate the model
111
         */
112 1
        $result = new Model\UserAgent();
113 1
        $result->setProviderResultRaw($resultRaw);
114
115
        /*
116
         * Bot detection - is currently not possible!
117
         */
118
119
        /*
120
         * hydrate the result
121
         */
122 1
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
123
        // renderingEngine not available
124
        // os is mixed with device informations
125
        // device is mixed with os
126
127 1
        return $result;
128
    }
129
}
130