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 (#44)
by Martin
05:39
created

UserAgentStringCom   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 224
Duplicated Lines 24.55 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 30
c 2
b 0
f 0
lcom 1
cbo 10
dl 55
loc 224
ccs 56
cts 56
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getVersion() 0 4 1
B getResult() 3 34 5
A hasResult() 8 8 3
A isBot() 0 8 3
B hydrateBot() 0 12 5
B hydrateBrowser() 10 10 5
B hydrateOperatingSystem() 0 10 5
B parse() 34 34 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
namespace UserAgentParser\Provider\Http;
3
4
use GuzzleHttp\Psr7\Request;
5
use stdClass;
6
use UserAgentParser\Exception;
7
use UserAgentParser\Model;
8
9
/**
10
 *
11
 * @see http://www.useragentstring.com/pages/api.php
12
 */
13
class UserAgentStringCom extends AbstractHttpProvider
14
{
15
    /**
16
     * Name of the provider
17
     *
18
     * @var string
19
     */
20
    protected $name = 'UserAgentStringCom';
21
22
    /**
23
     * Homepage of the provider
24
     *
25
     * @var string
26
     */
27
    protected $homepage = 'http://www.useragentstring.com/';
28
29
    protected $detectionCapabilities = [
30
31
        'browser' => [
32
            'name'    => true,
33
            'version' => true,
34
        ],
35
36
        'renderingEngine' => [
37
            'name'    => false,
38
            'version' => false,
39
        ],
40
41
        'operatingSystem' => [
42
            'name'    => true,
43
            'version' => true,
44
        ],
45
46
        'device' => [
47
            'model'    => false,
48
            'brand'    => false,
49
            'type'     => false,
50
            'isMobile' => false,
51
            'isTouch'  => false,
52
        ],
53
54
        'bot' => [
55
            'isBot' => true,
56
            'name'  => true,
57
            'type'  => true,
58
        ],
59
    ];
60
61
    protected $defaultValues = [
62
        'Null',
63
        'unknown',
64
    ];
65
66
    private $botTypes = [
67
        'Crawler',
68
        'Cloud Platform',
69
        'Feed Reader',
70
        'LinkChecker',
71
        'Validator',
72
    ];
73
74
    private static $uri = 'http://www.useragentstring.com/';
75
76 1
    public function getVersion()
77
    {
78 1
        return;
79
    }
80
81
    /**
82
     *
83
     * @param  string                     $userAgent
84
     * @param  array                      $headers
85
     * @return stdClass
86
     * @throws Exception\RequestException
87
     */
88 7
    protected function getResult($userAgent, array $headers)
0 ignored issues
show
Unused Code introduced by
The parameter $headers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
        /*
91
         * an empty UserAgent makes no sense
92
         */
93 7
        if ($userAgent == '') {
94 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
95
        }
96
97 6
        $parameters = 'uas=' . urlencode($userAgent);
98 6
        $parameters .= '&getJSON=all';
99
100 6
        $uri = self::$uri . '?' . $parameters;
101
102 6
        $request = new Request('GET', $uri);
103
104 6
        $response = $this->getResponse($request);
105
106
        /*
107
         * no json returned?
108
         */
109 6
        $contentType = $response->getHeader('Content-Type');
110 6 View Code Duplication
        if (! isset($contentType[0]) || $contentType[0] != 'application/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...
111 1
            throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
112
        }
113
114 5
        $content = json_decode($response->getBody()->getContents());
115
116 5
        if (! $content instanceof stdClass) {
117 1
            throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
118
        }
119
120 4
        return $content;
121
    }
122
123
    /**
124
     *
125
     * @param stdClass $resultRaw
126
     *
127
     * @return bool
128
     */
129 4 View Code Duplication
    private function hasResult(stdClass $resultRaw)
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...
130
    {
131 4
        if (isset($resultRaw->agent_type) && $this->isRealResult($resultRaw->agent_type)) {
132 3
            return true;
133
        }
134
135 1
        return false;
136
    }
137
138
    /**
139
     *
140
     * @param  stdClass $resultRaw
141
     * @return boolean
142
     */
143 3
    private function isBot(stdClass $resultRaw)
144
    {
145 3
        if (isset($resultRaw->agent_type) && in_array($resultRaw->agent_type, $this->botTypes)) {
146 1
            return true;
147
        }
148
149 2
        return false;
150
    }
151
152
    /**
153
     *
154
     * @param Model\Bot $bot
155
     * @param stdClass  $resultRaw
156
     */
157 1
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
158
    {
159 1
        $bot->setIsBot(true);
160
161 1
        if (isset($resultRaw->agent_name) && $this->isRealResult($resultRaw->agent_name) === true) {
162 1
            $bot->setName($resultRaw->agent_name);
163
        }
164
165 1
        if (isset($resultRaw->agent_type) && $this->isRealResult($resultRaw->agent_type) === true) {
166 1
            $bot->setType($resultRaw->agent_type);
167
        }
168 1
    }
169
170
    /**
171
     *
172
     * @param Model\Browser $browser
173
     * @param stdClass      $resultRaw
174
     */
175 2 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
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...
176
    {
177 2
        if (isset($resultRaw->agent_name) && $this->isRealResult($resultRaw->agent_name) === true) {
178 1
            $browser->setName($resultRaw->agent_name);
179
        }
180
181 2
        if (isset($resultRaw->agent_version) && $this->isRealResult($resultRaw->agent_version) === true) {
182 1
            $browser->getVersion()->setComplete($resultRaw->agent_version);
183
        }
184 2
    }
185
186
    /**
187
     *
188
     * @param Model\OperatingSystem $os
189
     * @param stdClass              $resultRaw
190
     */
191 2
    private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
192
    {
193 2
        if (isset($resultRaw->os_name) && $this->isRealResult($resultRaw->os_name) === true) {
194 1
            $os->setName($resultRaw->os_name);
195
        }
196
197 2
        if (isset($resultRaw->os_versionNumber) && $this->isRealResult($resultRaw->os_versionNumber) === true) {
198 1
            $os->getVersion()->setComplete($resultRaw->os_versionNumber);
199
        }
200 2
    }
201
202 7 View Code Duplication
    public function parse($userAgent, array $headers = [])
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...
203
    {
204 7
        $resultRaw = $this->getResult($userAgent, $headers);
205
206
        /*
207
         * No result found?
208
         */
209 4
        if ($this->hasResult($resultRaw) !== true) {
210 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
211
        }
212
213
        /*
214
         * Hydrate the model
215
         */
216 3
        $result = new Model\UserAgent();
217 3
        $result->setProviderResultRaw($resultRaw);
218
219
        /*
220
         * Bot detection
221
         */
222 3
        if ($this->isBot($resultRaw) === true) {
223 1
            $this->hydrateBot($result->getBot(), $resultRaw);
224
225 1
            return $result;
226
        }
227
228
        /*
229
         * hydrate the result
230
         */
231 2
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
232 2
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
233
234 2
        return $result;
235
    }
236
}
237