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 (#42)
by Martin
10:53 queued 03:22
created

WhichBrowser   B

Complexity

Total Complexity 29

Size/Duplication

Total Lines 213
Duplicated Lines 18.31 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 100%

Importance

Changes 23
Bugs 0 Features 0
Metric Value
wmc 29
c 23
b 0
f 0
lcom 1
cbo 16
dl 39
loc 213
ccs 63
cts 63
cp 1
rs 8.4615

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 3 6 2
A getName() 0 4 1
A getComposerPackageName() 0 4 1
A getParser() 0 8 2
A hydrateBot() 0 8 2
C hydrateBrowser() 16 25 7
A hydrateRenderingEngine() 10 10 3
A hydrateOperatingSystem() 10 10 3
B hydrateDevice() 0 18 5
B parse() 0 38 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;
3
4
use UserAgentParser\Exception;
5
use UserAgentParser\Model;
6
use WhichBrowser\Parser as WhichBrowserParser;
7
8
class WhichBrowser extends AbstractProvider
9
{
10
    protected $detectionCapabilities = [
11
12
        'browser' => [
13
            'name'    => true,
14
            'version' => true,
15
        ],
16
17
        'renderingEngine' => [
18
            'name'    => true,
19
            'version' => true,
20
        ],
21
22
        'operatingSystem' => [
23
            'name'    => true,
24
            'version' => true,
25
        ],
26
27
        'device' => [
28
            'model'    => true,
29
            'brand'    => true,
30
            'type'     => true,
31
            'isMobile' => true,
32
            'isTouch'  => false,
33
        ],
34
35
        'bot' => [
36
            'isBot' => true,
37
            'name'  => true,
38
            'type'  => false,
39
        ],
40
    ];
41
42
    /**
43
     * Used for unitTests mocking
44
     *
45
     * @var WhichBrowserParser
46
     */
47
    private $parser;
48
49 1
    public function __construct()
50
    {
51 1 View Code Duplication
        if (! class_exists('WhichBrowser\Parser', 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...
52
            throw new Exception\PackageNotLoaded('You need to install ' . $this->getComposerPackageName() . ' to use this provider');
53
        }
54 2
    }
55
56 2
    public function getName()
57
    {
58
        return 'WhichBrowser';
59
    }
60
61
    public function getComposerPackageName()
62
    {
63
        return 'whichbrowser/parser';
64 8
    }
65
66 8
    /**
67 7
     *
68
     * @param  array              $headers
69
     * @return WhichBrowserParser
70 1
     */
71
    public function getParser(array $headers)
72
    {
73
        if ($this->parser !== null) {
74
            return $this->parser;
75
        }
76
77
        return new WhichBrowserParser($headers);
78 1
    }
79
80 1
    /**
81
     *
82 1
     * @param Model\Bot                   $bot
83 1
     * @param \WhichBrowser\Model\Browser $browserRaw
84
     */
85 1
    private function hydrateBot(Model\Bot $bot, \WhichBrowser\Model\Browser $browserRaw)
86
    {
87
        $bot->setIsBot(true);
88
89
        if ($this->isRealResult($browserRaw->getName()) === true) {
90
            $bot->setName($browserRaw->getName());
91
        }
92 5
    }
93
94 5
    /**
95 1
     *
96
     * @param Model\Browser               $browser
97 1
     * @param \WhichBrowser\Model\Browser $browserRaw
98 1
     */
99
    private function hydrateBrowser(Model\Browser $browser, \WhichBrowser\Model\Browser $browserRaw)
100
    {
101 1 View Code Duplication
        if ($this->isRealResult($browserRaw->getName()) === 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...
102
            $browser->setName($browserRaw->getName());
103
104 4
            if ($this->isRealResult($browserRaw->getVersion()) === true) {
105
                $browser->getVersion()->setComplete($browserRaw->getVersion());
106 1
            }
107
108 1
            return;
109 1
        }
110
111 1
        if (isset($browserRaw->using) && $browserRaw->using instanceof \WhichBrowser\Model\Using) {
112 1
            /* @var $usingRaw \WhichBrowser\Model\Using */
113
            $usingRaw = $browserRaw->using;
114
115 View Code Duplication
            if ($this->isRealResult($usingRaw->getName()) === 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...
116 4
                $browser->setName($usingRaw->getName());
117
118
                if ($this->isRealResult($usingRaw->getVersion()) === true) {
119
                    $browser->getVersion()->setComplete($usingRaw->getVersion());
120
                }
121
            }
122
        }
123 5
    }
124
125 5
    /**
126 1
     *
127
     * @param Model\RenderingEngine      $engine
128
     * @param \WhichBrowser\Model\Engine $engineRaw
129 5
     */
130 1 View Code Duplication
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, \WhichBrowser\Model\Engine $engineRaw)
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...
131
    {
132 5
        if ($this->isRealResult($engineRaw->getName()) === true) {
133
            $engine->setName($engineRaw->getName());
134
        }
135
136
        if ($this->isRealResult($engineRaw->getVersion()) === true) {
137
            $engine->getVersion()->setComplete($engineRaw->getVersion());
138
        }
139 5
    }
140
141 5
    /**
142 1
     *
143
     * @param Model\OperatingSystem  $os
144
     * @param \WhichBrowser\Model\Os $osRaw
145 5
     */
146 1 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, \WhichBrowser\Model\Os $osRaw)
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...
147
    {
148 5
        if ($this->isRealResult($osRaw->getName()) === true) {
149
            $os->setName($osRaw->getName());
150
        }
151
152
        if ($this->isRealResult($osRaw->getVersion()) === true) {
153
            $os->getVersion()->setComplete($osRaw->getVersion());
154
        }
155
    }
156 5
157
    /**
158 5
     *
159 1
     * @param Model\Device               $device
160
     * @param \WhichBrowser\Model\Device $deviceRaw
161
     * @param WhichBrowserParser         $parser
162 5
     */
163 1
    private function hydrateDevice(Model\Device $device, \WhichBrowser\Model\Device $deviceRaw, WhichBrowserParser $parser)
164
    {
165
        if ($this->isRealResult($deviceRaw->getModel()) === true) {
166 5
            $device->setModel($deviceRaw->getModel());
167 1
        }
168
169
        if ($this->isRealResult($deviceRaw->getManufacturer()) === true) {
170 5
            $device->setBrand($deviceRaw->getManufacturer());
171 1
        }
172
173 5
        if ($this->isRealResult($parser->getType()) === true) {
174
            $device->setType($parser->getType());
175 7
        }
176
177 7
        if ($parser->isType('mobile', 'tablet', 'ereader', 'media', 'watch', 'camera', 'gaming:portable') === true) {
178
            $device->setIsMobile(true);
179 7
        }
180
    }
181
182
    public function parse($userAgent, array $headers = [])
183
    {
184 7
        $headers['User-Agent'] = $userAgent;
185 1
186
        $parser = $this->getParser($headers);
187
188
        /*
189
         * No result found?
190
         */
191 6
        if ($parser->isDetected() !== true) {
192 6
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
193
        }
194
195
        /*
196
         * Hydrate the model
197 6
         */
198 1
        $result = new Model\UserAgent();
199
        $result->setProviderResultRaw($parser->toArray());
200 1
201
        /*
202
         * Bot detection
203
         */
204
        if ($parser->getType() === 'bot') {
205
            $this->hydrateBot($result->getBot(), $parser->browser);
206 5
207 5
            return $result;
208 5
        }
209 5
210
        /*
211 5
         * hydrate the result
212
         */
213
        $this->hydrateBrowser($result->getBrowser(), $parser->browser);
214
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $parser->engine);
215
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $parser->os);
216
        $this->hydrateDevice($result->getDevice(), $parser->device, $parser);
217
218
        return $result;
219
    }
220
}
221