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