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
04:01
created

UAParser::hasResult()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 8.8571
cc 5
eloc 10
nc 5
nop 1
crap 5
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
                '/^Smartphone$/i',
78
                '/^Feature Phone$/i',
79
                '/^iOS-Device$/i',
80
                '/^Tablet$/i',
81
                '/^Touch$/i',
82
                '/^Windows$/i',
83
                '/^Windows Phone$/i',
84
            ],
85
        ],
86
87
        'bot' => [
88
            'name' => [
89
                '/^Other$/i',
90
                '/^crawler$/i',
91
                '/^robot$/i',
92
                '/^crawl$/i',
93
                '/^Spider$/i',
94
            ],
95
        ],
96
    ];
97
98
    private $parser;
99
100
    /**
101
     *
102
     * @param  Parser                    $parser
103
     * @throws PackageNotLoadedException
104
     */
105 32 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...
106
    {
107 32
        if ($parser === null && ! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) {
108 1
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
109
        }
110
111 31
        $this->parser = $parser;
112 31
    }
113
114
    /**
115
     *
116
     * @return Parser
117
     */
118 10
    public function getParser()
119
    {
120 10
        if ($this->parser !== null) {
121 10
            return $this->parser;
122
        }
123
124 1
        $this->parser = Parser::create();
125
126 1
        return $this->parser;
127
    }
128
129
    /**
130
     *
131
     * @param \UAParser\Result\Client $resultRaw
132
     *
133
     * @return bool
134
     */
135 9
    private function hasResult(\UAParser\Result\Client $resultRaw)
136
    {
137 9
        if ($this->isBot($resultRaw) === true) {
138 2
            return true;
139
        }
140
141 7
        if ($this->isRealResult($resultRaw->ua->family)) {
142 1
            return true;
143
        }
144
145 6
        if ($this->isRealResult($resultRaw->os->family)) {
146 2
            return true;
147
        }
148
149 4
        if ($this->isRealResult($resultRaw->device->model, 'device', 'model')) {
150 1
            return true;
151
        }
152
153 3
        return false;
154
    }
155
156
    /**
157
     *
158
     * @param \UAParser\Result\Client $resultRaw
159
     *
160
     * @return bool
161
     */
162 9
    private function isBot(\UAParser\Result\Client $resultRaw)
163
    {
164 9
        if ($resultRaw->device->family === 'Spider') {
165 2
            return true;
166
        }
167
168 7
        return false;
169
    }
170
171
    /**
172
     *
173
     * @param Model\Bot               $bot
174
     * @param \UAParser\Result\Client $resultRaw
175
     */
176 2
    private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw)
177
    {
178 2
        $bot->setIsBot(true);
179
180 2
        if ($this->isRealResult($resultRaw->ua->family, 'bot', 'name') === true) {
181 1
            $bot->setName($resultRaw->ua->family);
182 1
        }
183 2
    }
184
185
    /**
186
     *
187
     * @param Model\Browser              $browser
188
     * @param \UAParser\Result\UserAgent $uaRaw
189
     */
190 4 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...
191
    {
192 4
        if ($this->isRealResult($uaRaw->family) === true) {
193 1
            $browser->setName($uaRaw->family);
194 1
        }
195
196 4
        if ($this->isRealResult($uaRaw->major) === true) {
197 1
            $browser->getVersion()->setMajor($uaRaw->major);
198 1
        }
199
200 4
        if ($this->isRealResult($uaRaw->minor) === true) {
201 1
            $browser->getVersion()->setMinor($uaRaw->minor);
202 1
        }
203
204 4
        if ($this->isRealResult($uaRaw->patch) === true) {
205 1
            $browser->getVersion()->setPatch($uaRaw->patch);
206 1
        }
207 4
    }
208
209
    /**
210
     *
211
     * @param Model\OperatingSystem            $os
212
     * @param \UAParser\Result\OperatingSystem $osRaw
213
     */
214 4 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...
215
    {
216 4
        if ($this->isRealResult($osRaw->family) === true) {
217 2
            $os->setName($osRaw->family);
218 2
        }
219
220 4
        if ($this->isRealResult($osRaw->major) === true) {
221 2
            $os->getVersion()->setMajor($osRaw->major);
222 2
        }
223
224 4
        if ($this->isRealResult($osRaw->minor) === true) {
225 1
            $os->getVersion()->setMinor($osRaw->minor);
226 1
        }
227
228 4
        if ($this->isRealResult($osRaw->patch) === true) {
229 1
            $os->getVersion()->setPatch($osRaw->patch);
230 1
        }
231 4
    }
232
233
    /**
234
     *
235
     * @param Model\UserAgent         $device
236
     * @param \UAParser\Result\Device $deviceRaw
237
     */
238 4
    private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw)
239
    {
240 4
        if ($this->isRealResult($deviceRaw->model, 'device', 'model') === true) {
241 1
            $device->setModel($deviceRaw->model);
242 1
        }
243
244 4
        if ($this->isRealResult($deviceRaw->brand, 'device', 'brand') === true) {
245 1
            $device->setBrand($deviceRaw->brand);
246 1
        }
247 4
    }
248
249 9
    public function parse($userAgent, array $headers = [])
250
    {
251 9
        $parser = $this->getParser();
252
253
        /* @var $resultRaw \UAParser\Result\Client */
254 9
        $resultRaw = $parser->parse($userAgent);
255
256
        /*
257
         * No result found?
258
         */
259 9
        if ($this->hasResult($resultRaw) !== true) {
260 3
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
261
        }
262
263
        /*
264
         * Hydrate the model
265
         */
266 6
        $result = new Model\UserAgent();
267 6
        $result->setProviderResultRaw($resultRaw);
268
269
        /*
270
         * Bot detection
271
         */
272 6
        if ($this->isBot($resultRaw) === true) {
273 2
            $this->hydrateBot($result->getBot(), $resultRaw);
274
275 2
            return $result;
276
        }
277
278
        /*
279
         * hydrate the result
280
         */
281 4
        $this->hydrateBrowser($result->getBrowser(), $resultRaw->ua);
282
        // renderingEngine not available
283 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->os);
284 4
        $this->hydrateDevice($result->getDevice(), $resultRaw->device);
285
286 4
        return $result;
287
    }
288
}
289