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 (#59)
by Martin
02:49
created

BrowscapPhp   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 280
Duplicated Lines 7.14 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 16
Bugs 0 Features 1
Metric Value
wmc 47
c 16
b 0
f 1
lcom 1
cbo 11
dl 20
loc 280
ccs 94
cts 94
cp 1
rs 8.439

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getVersion() 0 6 1
A getUpdateDate() 0 4 1
A getParser() 0 4 1
A hasResult() 0 8 3
A isBot() 0 8 3
B hydrateBot() 0 14 7
B hydrateBrowser() 10 10 5
B hydrateRenderingEngine() 3 10 5
B hydrateOperatingSystem() 0 10 5
C hydrateDevice() 7 23 12
B parse() 0 39 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like BrowscapPhp often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BrowscapPhp, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace UserAgentParser\Provider;
3
4
use BrowscapPHP\Browscap;
5
use stdClass;
6
use UserAgentParser\Exception\NoResultFoundException;
7
use UserAgentParser\Model;
8
9
class BrowscapPhp extends AbstractProvider
10
{
11
    /**
12
     * Name of the provider
13
     *
14
     * @var string
15
     */
16
    protected $name = 'BrowscapPhp';
17
18
    /**
19
     * Homepage of the provider
20
     *
21
     * @var string
22
     */
23
    protected $homepage = 'https://github.com/browscap/browscap-php';
24
25
    /**
26
     * Composer package name
27
     *
28
     * @var string
29
     */
30
    protected $packageName = 'browscap/browscap-php';
31
32
    protected $detectionCapabilities = [
33
34
        'browser' => [
35
            'name'    => true,
36
            'version' => true,
37
        ],
38
39
        'renderingEngine' => [
40
            'name'    => true,
41
            'version' => true,
42
        ],
43
44
        'operatingSystem' => [
45
            'name'    => true,
46
            'version' => true,
47
        ],
48
49
        'device' => [
50
            'model'    => true,
51
            'brand'    => true,
52
            'type'     => true,
53
            'isMobile' => true,
54
            'isTouch'  => true,
55
        ],
56
57
        'bot' => [
58
            'isBot' => true,
59
            'name'  => true,
60
            'type'  => true,
61
        ],
62
    ];
63
64
    protected $defaultValues = [
65
66
        'general' => [
67
            '/^unknown$/i',
68
        ],
69
70
        'browser' => [
71
            'name' => [
72
                '/^Default Browser$/i',
73
            ],
74
        ],
75
76
        'device' => [
77 17
            'model' => [
78
                '/^general/i',
79 17
                '/desktop$/i',
80 17
            ],
81
        ],
82 1
83
        'bot' => [
84 1
            'name' => [
85 1
                '/^General Crawlers/i',
86 1
            ],
87
        ],
88
89 1
    ];
90
91 1
    /**
92
     *
93
     * @var Browscap
94
     */
95
    private $parser;
96
97
    public function __construct(Browscap $parser)
98 12
    {
99
        $this->parser = $parser;
100 12
    }
101
102
    public function getVersion()
103
    {
104
        return $this->getParser()
105
            ->getCache()
106
            ->getVersion();
107 1
    }
108
109
    public function getUpdateDate()
110 1
    {
111 1
        return;
112 1
    }
113 1
114 1
    /**
115
     *
116
     * @return Browscap
117
     */
118
    public function getParser()
119
    {
120
        return $this->parser;
121
    }
122
123 10
    /**
124
     *
125 10
     * @param stdClass $resultRaw
126 1
     *
127
     * @return bool
128
     */
129 9
    private function hasResult(stdClass $resultRaw)
130 4
    {
131
        if (! isset($resultRaw->browser) || $this->isRealResult($resultRaw->browser, 'browser', 'name') !== true) {
132
            return false;
133 5
        }
134
135
        return true;
136
    }
137
138
    /**
139
     *
140
     * @param  stdClass $resultRaw
141 5
     * @return boolean
142
     */
143 5
    private function isBot(stdClass $resultRaw)
144 2
    {
145
        if (! isset($resultRaw->crawler) || $resultRaw->crawler !== true) {
146
            return false;
147 3
        }
148
149
        return true;
150
    }
151
152
    /**
153
     *
154
     * @param Model\Bot $bot
155 3
     * @param stdClass  $resultRaw
156
     */
157 3
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
158
    {
159 3
        $bot->setIsBot(true);
160 3
161 3
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'bot', 'name') === true) {
162
            $bot->setName($resultRaw->browser);
163 3
        }
164 1
165 3
        if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) {
166 2
            $bot->setType('RSS');
167 2
        } elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) {
168 3
            $bot->setType($resultRaw->browser_type);
169
        }
170
    }
171
172
    /**
173
     *
174
     * @param Model\Browser $browser
175 2
     * @param stdClass      $resultRaw
176
     */
177 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...
178 2
    {
179 2
        if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'browser', 'name') === true) {
180
            $browser->setName($resultRaw->browser);
181 2
        }
182 2
183 2
        if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) {
184 2
            $browser->getVersion()->setComplete($resultRaw->version);
185
        }
186
    }
187
188
    /**
189
     *
190
     * @param Model\RenderingEngine $engine
191 2
     * @param stdClass              $resultRaw
192
     */
193 2
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw)
194 1
    {
195 1 View Code Duplication
        if (isset($resultRaw->renderingengine_name) && $this->isRealResult($resultRaw->renderingengine_name) === true) {
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...
196
            $engine->setName($resultRaw->renderingengine_name);
197 2
        }
198 1
199 1
        if (isset($resultRaw->renderingengine_version) && $this->isRealResult($resultRaw->renderingengine_version) === true) {
200 2
            $engine->getVersion()->setComplete($resultRaw->renderingengine_version);
201
        }
202
    }
203
204
    /**
205
     *
206
     * @param Model\OperatingSystem $os
207 2
     * @param stdClass              $resultRaw
208
     */
209 2
    private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
210 1
    {
211 1
        if (isset($resultRaw->platform) && $this->isRealResult($resultRaw->platform) === true) {
212
            $os->setName($resultRaw->platform);
213 2
        }
214 1
215 1
        if (isset($resultRaw->platform_version) && $this->isRealResult($resultRaw->platform_version) === true) {
216 2
            $os->getVersion()->setComplete($resultRaw->platform_version);
217
        }
218
    }
219
220
    /**
221
     *
222
     * @param Model\UserAgent $device
223 2
     * @param stdClass        $resultRaw
224
     */
225 2
    private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
226 1
    {
227 1
        if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name, 'device', 'model') === true) {
228
            $device->setModel($resultRaw->device_name);
229 2
        }
230 1
231 1 View Code Duplication
        if (isset($resultRaw->device_brand_name) && $this->isRealResult($resultRaw->device_brand_name) === true) {
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...
232
            $device->setBrand($resultRaw->device_brand_name);
233 2
        }
234
235 1 View Code Duplication
        if (isset($resultRaw->device_type) && $this->isRealResult($resultRaw->device_type) === true) {
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...
236 1
            // @todo convert to a common set of types (over all vendors)
237
            $device->setType($resultRaw->device_type);
238 2
        }
239 1
240 1
        if (isset($resultRaw->ismobiledevice) && $this->isRealResult($resultRaw->ismobiledevice) === true && $resultRaw->ismobiledevice === true) {
241
            $device->setIsMobile(true);
242 2
        }
243 1
244 1
        if (isset($resultRaw->device_pointing_method) && $resultRaw->device_pointing_method == 'touchscreen') {
245 2
            $device->setIsTouch(true);
246
        }
247 10
    }
248
249 10
    public function parse($userAgent, array $headers = [])
250
    {
251
        $parser = $this->getParser();
252 10
253
        /* @var $resultRaw \stdClass */
254
        $resultRaw = $parser->getBrowser($userAgent);
255
256
        /*
257 10
         * No result found?
258 5
         */
259
        if ($this->hasResult($resultRaw) !== true) {
260
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
261
        }
262
263
        /*
264 5
         * Hydrate the model
265 5
         */
266
        $result = new Model\UserAgent();
267
        $result->setProviderResultRaw($resultRaw);
268
269
        /*
270 5
         * Bot detection (does only work with full_php_browscap.ini)
271 3
         */
272
        if ($this->isBot($resultRaw) === true) {
273 3
            $this->hydrateBot($result->getBot(), $resultRaw);
274
275
            return $result;
276
        }
277
278
        /*
279 2
         * hydrate the result
280 2
         */
281 2
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
282 2
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw);
283
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
284 2
        $this->hydrateDevice($result->getDevice(), $resultRaw);
285
286
        return $result;
287
    }
288
}
289