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
03:19
created

UAParser::getDeviceModelDefaultValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
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 28 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 28
        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 27
        $this->parser = $parser;
112 27
    }
113
114
    /**
115
     *
116
     * @return Parser
117
     */
118 6
    public function getParser()
119
    {
120 6
        if ($this->parser !== null) {
121 6
            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 5
    private function hasResult(\UAParser\Result\Client $resultRaw)
136
    {
137 5
        if ($this->isRealResult($resultRaw->ua->family)) {
138 2
            return true;
139
        }
140
141 3
        if ($this->isRealResult($resultRaw->os->family)) {
142 1
            return true;
143
        }
144
145 2
        if ($this->isRealResult($resultRaw->device->model, 'device', 'model')) {
146 1
            return true;
147
        }
148
149 1
        return false;
150
    }
151
152
    /**
153
     *
154
     * @param \UAParser\Result\Client $resultRaw
155
     *
156
     * @return bool
157
     */
158 4
    private function isBot(\UAParser\Result\Client $resultRaw)
159
    {
160 4
        if ($resultRaw->device->family === 'Spider') {
161 1
            return true;
162
        }
163
164 3
        return false;
165
    }
166
167
    /**
168
     *
169
     * @param Model\Bot               $bot
170
     * @param \UAParser\Result\Client $resultRaw
171
     */
172 1
    private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw)
173
    {
174 1
        $bot->setIsBot(true);
175
176 1
        if ($this->isRealResult($resultRaw->ua->family, 'bot', 'name') === true) {
177 1
            $bot->setName($resultRaw->ua->family);
178 1
        }
179 1
    }
180
181
    /**
182
     *
183
     * @param Model\Browser              $browser
184
     * @param \UAParser\Result\UserAgent $uaRaw
185
     */
186 3 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...
187
    {
188 3
        if ($this->isRealResult($uaRaw->family) === true) {
189 1
            $browser->setName($uaRaw->family);
190 1
        }
191
192 3
        if ($this->isRealResult($uaRaw->major) === true) {
193 1
            $browser->getVersion()->setMajor($uaRaw->major);
194 1
        }
195
196 3
        if ($this->isRealResult($uaRaw->minor) === true) {
197 1
            $browser->getVersion()->setMinor($uaRaw->minor);
198 1
        }
199
200 3
        if ($this->isRealResult($uaRaw->patch) === true) {
201 1
            $browser->getVersion()->setPatch($uaRaw->patch);
202 1
        }
203 3
    }
204
205
    /**
206
     *
207
     * @param Model\OperatingSystem            $os
208
     * @param \UAParser\Result\OperatingSystem $osRaw
209
     */
210 3 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...
211
    {
212 3
        if ($this->isRealResult($osRaw->family) === true) {
213 1
            $os->setName($osRaw->family);
214 1
        }
215
216 3
        if ($this->isRealResult($osRaw->major) === true) {
217 1
            $os->getVersion()->setMajor($osRaw->major);
218 1
        }
219
220 3
        if ($this->isRealResult($osRaw->minor) === true) {
221 1
            $os->getVersion()->setMinor($osRaw->minor);
222 1
        }
223
224 3
        if ($this->isRealResult($osRaw->patch) === true) {
225 1
            $os->getVersion()->setPatch($osRaw->patch);
226 1
        }
227 3
    }
228
229
    /**
230
     *
231
     * @param Model\UserAgent         $device
232
     * @param \UAParser\Result\Device $deviceRaw
233
     */
234 3
    private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw)
235
    {
236 3
        if ($this->isRealResult($deviceRaw->model, 'device', 'model') === true) {
237 1
            $device->setModel($deviceRaw->model);
238 1
        }
239
240 3
        if ($this->isRealResult($deviceRaw->brand, 'device', 'brand') === true) {
241 1
            $device->setBrand($deviceRaw->brand);
242 1
        }
243 3
    }
244
245 5
    public function parse($userAgent, array $headers = [])
246
    {
247 5
        $parser = $this->getParser();
248
249
        /* @var $resultRaw \UAParser\Result\Client */
250 5
        $resultRaw = $parser->parse($userAgent);
251
252
        /*
253
         * No result found?
254
         */
255 5
        if ($this->hasResult($resultRaw) !== true) {
256 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
257
        }
258
259
        /*
260
         * Hydrate the model
261
         */
262 4
        $result = new Model\UserAgent();
263 4
        $result->setProviderResultRaw($resultRaw);
264
265
        /*
266
         * Bot detection
267
         */
268 4
        if ($this->isBot($resultRaw) === true) {
269 1
            $this->hydrateBot($result->getBot(), $resultRaw);
270
271 1
            return $result;
272
        }
273
274
        /*
275
         * hydrate the result
276
         */
277 3
        $this->hydrateBrowser($result->getBrowser(), $resultRaw->ua);
278
        // renderingEngine not available
279 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->os);
280 3
        $this->hydrateDevice($result->getDevice(), $resultRaw->device);
281
282 3
        return $result;
283
    }
284
}
285