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
Push — master ( d31fa5...89d544 )
by Martin
04:29
created

WhatIsMyBrowserCom   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 222
Duplicated Lines 5.41 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 36
c 2
b 0
f 0
lcom 1
cbo 11
dl 12
loc 222
ccs 59
cts 59
cp 1
rs 8.8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getVersion() 0 4 1
C getResult() 12 68 17
B hasResult() 0 12 5
B hydrateBrowser() 0 10 5
B hydrateOperatingSystem() 0 10 5
B parse() 0 25 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\Http;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\Psr7\Request;
6
use stdClass;
7
use UserAgentParser\Exception;
8
use UserAgentParser\Model;
9
10
/**
11
 *
12
 * @see https://developers.whatismybrowser.com/reference
13
 */
14
class WhatIsMyBrowserCom extends AbstractHttpProvider
15
{
16
    /**
17
     * Name of the provider
18
     *
19
     * @var string
20
     */
21
    protected $name = 'WhatIsMyBrowserCom';
22
23
    /**
24
     * Homepage of the provider
25
     *
26
     * @var string
27
     */
28
    protected $homepage = 'https://www.whatismybrowser.com/';
29
30
    protected $detectionCapabilities = [
31
32
        'browser' => [
33
            'name'    => true,
34
            'version' => true,
35
        ],
36
37
        'renderingEngine' => [
38
            'name'    => false,
39
            'version' => false,
40
        ],
41
42
        'operatingSystem' => [
43
            'name'    => true,
44
            'version' => true,
45
        ],
46
47
        'device' => [
48
            'model' => false,
49
            'brand' => false,
50
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
    protected $defaultValues = [
64
        'Unknown Mobile Browser',
65
    ];
66
67
    private static $uri = 'http://api.whatismybrowser.com/api/v1/user_agent_parse';
68
69
    private $apiKey;
70
71 16
    public function __construct(Client $client, $apiKey)
72
    {
73 16
        parent::__construct($client);
74
75 16
        $this->apiKey = $apiKey;
76 16
    }
77
78 1
    public function getVersion()
79
    {
80 1
        return;
81
    }
82
83
    /**
84
     *
85
     * @param  string                     $userAgent
86
     * @param  array                      $headers
87
     * @return stdClass
88
     * @throws Exception\RequestException
89
     */
90 11
    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...
91
    {
92
        /*
93
         * an empty UserAgent makes no sense
94
         */
95 11
        if ($userAgent == '') {
96 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
97
        }
98
99
        $params = [
100 10
            'user_key'   => $this->apiKey,
101 10
            'user_agent' => $userAgent,
102 10
        ];
103
104 10
        $body = http_build_query($params, null, '&');
105
106 10
        $request = new Request('POST', self::$uri, [], $body);
107
108 10
        $response = $this->getResponse($request);
109
110
        /*
111
         * no json returned?
112
         */
113 10
        $contentType = $response->getHeader('Content-Type');
114 10 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...
115 1
            throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
116
        }
117
118 9
        $content = json_decode($response->getBody()->getContents());
119
120
        /*
121
         * No result
122
         */
123 9
        if (isset($content->message_code) && $content->message_code == 'no_user_agent') {
124 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
125
        }
126
127
        /*
128
         * Limit exceeded
129
         */
130 8 View Code Duplication
        if (isset($content->message_code) && $content->message_code == 'usage_limit_exceeded') {
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...
131 1
            throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API key "' . $this->apiKey . '" for ' . $this->getName());
132
        }
133
134
        /*
135
         * Error
136
         */
137 7
        if (isset($content->message_code) && $content->message_code == 'no_api_user_key') {
138 1
            throw new Exception\InvalidCredentialsException('Missing API key for ' . $this->getName());
139
        }
140
141 6 View Code Duplication
        if (isset($content->message_code) && $content->message_code == 'user_key_invalid') {
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...
142 1
            throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName());
143
        }
144
145 5 View Code Duplication
        if (!isset($content->result) || $content->result !== 'success') {
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...
146 1
            throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
147
        }
148
149
        /*
150
         * Missing data?
151
         */
152 4
        if (! $content instanceof stdClass || ! isset($content->parse) || ! $content->parse instanceof stdClass) {
153 1
            throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"');
154
        }
155
156 3
        return $content->parse;
157
    }
158
159
    /**
160
     *
161
     * @param stdClass $resultRaw
162
     *
163
     * @return bool
164
     */
165 3
    private function hasResult(stdClass $resultRaw)
166
    {
167 3
        if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name) === true) {
168 1
            return true;
169
        }
170
171 2
        if (isset($resultRaw->operating_system_name) && $this->isRealResult($resultRaw->operating_system_name) === true) {
172 1
            return true;
173
        }
174
175 1
        return false;
176
    }
177
178
    /**
179
     *
180
     * @param Model\Browser $browser
181
     * @param stdClass      $resultRaw
182
     */
183 2
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
184
    {
185 2
        if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name) === true) {
186 1
            $browser->setName($resultRaw->browser_name);
187
        }
188
189 2
        if (isset($resultRaw->browser_version_full) && $this->isRealResult($resultRaw->browser_version_full) === true) {
190 1
            $browser->getVersion()->setComplete($resultRaw->browser_version_full);
191
        }
192 2
    }
193
194
    /**
195
     *
196
     * @param Model\OperatingSystem $os
197
     * @param stdClass              $resultRaw
198
     */
199 2
    private function hydrateOperatingSystem(Model\OperatingSystem $os, $resultRaw)
200
    {
201 2
        if (isset($resultRaw->operating_system_name) && $this->isRealResult($resultRaw->operating_system_name) === true) {
202 1
            $os->setName($resultRaw->operating_system_name);
203
        }
204
205 2
        if (isset($resultRaw->operating_system_version_full) && $this->isRealResult($resultRaw->operating_system_version_full) === true) {
206 1
            $os->getVersion()->setComplete($resultRaw->operating_system_version_full);
207
        }
208 2
    }
209
210 11
    public function parse($userAgent, array $headers = [])
211
    {
212 11
        $resultRaw = $this->getResult($userAgent, $headers);
213
214
        /*
215
         * No result found?
216
         */
217 3
        if ($this->hasResult($resultRaw) !== true) {
218 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
219
        }
220
221
        /*
222
         * Hydrate the model
223
         */
224 2
        $result = new Model\UserAgent();
225 2
        $result->setProviderResultRaw($resultRaw);
226
227
        /*
228
         * hydrate the result
229
         */
230 2
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
231 2
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
232
233 2
        return $result;
234
    }
235
}
236