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:47
created

UAParser   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 268
Duplicated Lines 16.42 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 100%

Importance

Changes 17
Bugs 1 Features 1
Metric Value
wmc 29
c 17
b 1
f 1
lcom 1
cbo 14
dl 44
loc 268
ccs 88
cts 88
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 3
A getParser() 0 10 2
A hasResult() 0 16 4
A isBot() 0 8 2
A hydrateBot() 0 8 2
B hydrateBrowser() 18 18 5
B hydrateOperatingSystem() 18 18 5
A hydrateDevice() 0 10 3
B parse() 0 39 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 UAParser\Parser;
5
use UserAgentParser\Exception\NoResultFoundException;
6
use UserAgentParser\Exception\PackageNotLoadedException;
7
use UserAgentParser\Model;
8
9
class UAParser extends AbstractProvider
10
{
11
    /**
12
     * Name of the provider
13
     *
14
     * @var string
15
     */
16
    protected $name = 'UAParser';
17
18
    /**
19
     * Homepage of the provider
20
     *
21
     * @var string
22
     */
23
    protected $homepage = 'https://github.com/ua-parser/uap-php';
24
25
    /**
26
     * Composer package name
27
     *
28
     * @var string
29
     */
30
    protected $packageName = 'ua-parser/uap-php';
31
32
    protected $detectionCapabilities = [
33
34
        'browser' => [
35
            'name'    => true,
36
            'version' => true,
37
        ],
38
39
        'renderingEngine' => [
40
            'name'    => false,
41
            'version' => false,
42
        ],
43
44
        'operatingSystem' => [
45
            'name'    => true,
46
            'version' => true,
47
        ],
48
49
        'device' => [
50
            'model'    => true,
51
            'brand'    => true,
52
            'type'     => false,
53
            'isMobile' => false,
54
            'isTouch'  => false,
55
        ],
56
57
        'bot' => [
58
            'isBot' => true,
59
            'name'  => true,
60
            'type'  => false,
61
        ],
62
    ];
63
64
    protected $defaultValues = [
65
66
        'general' => [
67
            '/^Other$/i',
68
        ],
69
70
        'device' => [
71
72
            'brand' => [
73
                '/^Generic/i',
74
            ],
75
76
            'model' => [
77
                '/^Feature Phone$/i',
78 13
                '/^iOS-Device$/i',
79
                '/^Smartphone$/i',
80 13
            ],
81 1
        ],
82
83
        'bot' => [
84 12
            'name' => [
85 12
                '/^crawler$/i',
86
            ],
87
        ],
88
    ];
89
90
    private $parser;
91 6
92
    /**
93 6
     *
94 6
     * @param  Parser                    $parser
95
     * @throws PackageNotLoadedException
96
     */
97 1 View Code Duplication
    public function __construct(Parser $parser = null)
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...
98
    {
99 1
        if ($parser === null && ! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) {
100
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
101
        }
102
103
        $this->parser = $parser;
104
    }
105
106
    /**
107
     *
108 5
     * @return Parser
109
     */
110 5
    public function getParser()
111 2
    {
112
        if ($this->parser !== null) {
113
            return $this->parser;
114 3
        }
115 1
116
        $this->parser = Parser::create();
117
118 2
        return $this->parser;
119 1
    }
120
121
    /**
122 1
     *
123
     * @param \UAParser\Result\Client $resultRaw
124
     *
125 3
     * @return bool
126
     */
127
    private function hasResult(\UAParser\Result\Client $resultRaw)
128 3
    {
129 3
        if ($this->isRealResult($resultRaw->ua->family)) {
130 3
            return true;
131 3
        }
132
133
        if ($this->isRealResult($resultRaw->os->family)) {
134 3
            return true;
135
        }
136
137 3
        if ($this->isRealResult($resultRaw->device->model)) {
138 3
            return true;
139 3
        }
140 3
141
        return false;
142
    }
143
144
    /**
145
     *
146
     * @param \UAParser\Result\Client $resultRaw
147
     *
148
     * @return bool
149 4
     */
150
    private function isBot(\UAParser\Result\Client $resultRaw)
151 4
    {
152 1
        if ($resultRaw->device->family === 'Spider') {
153
            return true;
154
        }
155 3
156
        return false;
157
    }
158
159
    /**
160
     *
161
     * @param Model\Bot               $bot
162
     * @param \UAParser\Result\Client $resultRaw
163 1
     */
164
    private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw)
165 1
    {
166
        $bot->setIsBot(true);
167 1
168 1
        if ($this->isRealResult($resultRaw->ua->family) === true) {
169 1
            $bot->setName($resultRaw->ua->family);
170 1
        }
171
    }
172
173
    /**
174
     *
175
     * @param Model\Browser              $browser
176
     * @param \UAParser\Result\UserAgent $uaRaw
177 3
     */
178 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, \UAParser\Result\UserAgent $uaRaw)
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...
179 3
    {
180 1
        if ($this->isRealResult($uaRaw->family) === true) {
181 1
            $browser->setName($uaRaw->family);
182
        }
183 3
184 1
        if ($this->isRealResult($uaRaw->major) === true) {
185 1
            $browser->getVersion()->setMajor($uaRaw->major);
186
        }
187 3
188 1
        if ($this->isRealResult($uaRaw->minor) === true) {
189 1
            $browser->getVersion()->setMinor($uaRaw->minor);
190
        }
191 3
192 1
        if ($this->isRealResult($uaRaw->patch) === true) {
193 1
            $browser->getVersion()->setPatch($uaRaw->patch);
194 3
        }
195
    }
196
197
    /**
198
     *
199
     * @param Model\OperatingSystem            $os
200
     * @param \UAParser\Result\OperatingSystem $osRaw
201 3
     */
202 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, \UAParser\Result\OperatingSystem $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...
203 3
    {
204 1
        if ($this->isRealResult($osRaw->family) === true) {
205 1
            $os->setName($osRaw->family);
206
        }
207 3
208 1
        if ($this->isRealResult($osRaw->major) === true) {
209 1
            $os->getVersion()->setMajor($osRaw->major);
210
        }
211 3
212 1
        if ($this->isRealResult($osRaw->minor) === true) {
213 1
            $os->getVersion()->setMinor($osRaw->minor);
214
        }
215 3
216 1
        if ($this->isRealResult($osRaw->patch) === true) {
217 1
            $os->getVersion()->setPatch($osRaw->patch);
218 3
        }
219
    }
220
221
    /**
222
     *
223
     * @param Model\UserAgent         $device
224
     * @param \UAParser\Result\Device $deviceRaw
225 3
     */
226
    private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw)
227 3
    {
228 1
        if ($this->isRealResult($deviceRaw->model, 'device', 'model') === true) {
229 1
            $device->setModel($deviceRaw->model);
230
        }
231 3
232 1
        if ($this->isRealResult($deviceRaw->brand, 'device', 'brand') === true) {
233 1
            $device->setBrand($deviceRaw->brand);
234 3
        }
235
    }
236 5
237
    public function parse($userAgent, array $headers = [])
238 5
    {
239
        $parser = $this->getParser();
240
241 5
        /* @var $resultRaw \UAParser\Result\Client */
242
        $resultRaw = $parser->parse($userAgent);
243
244
        /*
245
         * No result found?
246 5
         */
247 1
        if ($this->hasResult($resultRaw) !== true) {
248
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
249
        }
250
251
        /*
252
         * Hydrate the model
253 4
         */
254 4
        $result = new Model\UserAgent();
255
        $result->setProviderResultRaw($resultRaw);
256
257
        /*
258
         * Bot detection
259 4
         */
260 1
        if ($this->isBot($resultRaw) === true) {
261
            $this->hydrateBot($result->getBot(), $resultRaw);
262 1
263
            return $result;
264
        }
265
266
        /*
267
         * hydrate the result
268 3
         */
269
        $this->hydrateBrowser($result->getBrowser(), $resultRaw->ua);
270 3
        // renderingEngine not available
271 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->os);
272
        $this->hydrateDevice($result->getDevice(), $resultRaw->device);
273 3
274
        return $result;
275
    }
276
}
277