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
Push — master ( b1c8ca...32e807 )
by Martin
09:06
created

HandsetDetection::hydrateBrowser()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 3
eloc 5
nc 4
nop 2
crap 3
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use HandsetDetection as Parser;
5
use UserAgentParser\Exception\NoResultFoundException;
6
use UserAgentParser\Model;
7
8
/**
9
 * Abstraction for ua-parser/uap-php
10
 *
11
 * @author Martin Keckeis <[email protected]>
12
 * @license MIT
13
 * @see https://github.com/HandsetDetection/php-apikit
14
 */
15
class HandsetDetection extends AbstractProvider
16
{
17
    /**
18
     * Name of the provider
19
     *
20
     * @var string
21
     */
22
    protected $name = 'HandsetDetection';
23
24
    /**
25
     * Homepage of the provider
26
     *
27
     * @var string
28
     */
29
    protected $homepage = 'https://github.com/HandsetDetection/php-apikit';
30
31
    /**
32
     * Composer package name
33
     *
34
     * @var string
35
     */
36
    protected $packageName = 'handsetdetection/php-apikit';
37
38
    protected $detectionCapabilities = [
39
40
        'browser' => [
41
            'name'    => true,
42
            'version' => true,
43
        ],
44
45
        'renderingEngine' => [
46
            'name'    => false,
47
            'version' => false,
48
        ],
49
50
        'operatingSystem' => [
51
            'name'    => true,
52
            'version' => true,
53
        ],
54
55
        'device' => [
56
            'model'    => true,
57
            'brand'    => true,
58
            'type'     => false,
59
            'isMobile' => false,
60
            'isTouch'  => false,
61
        ],
62
63
        'bot' => [
64
            'isBot' => false,
65
            'name'  => false,
66
            'type'  => false,
67
        ],
68
    ];
69
70
    protected $defaultValues = [
71
72
        'general' => [
73
            '/^generic$/i',
74
        ],
75
76
        'device' => [
77
            'model' => [
78
                '/analyzer/i',
79
                '/bot/i',
80
                '/crawler/i',
81
                '/library/i',
82
                '/spider/i',
83
            ],
84
        ],
85
    ];
86
87
    /**
88
     *
89
     * @var Parser\HD4
90
     */
91
    private $parser;
92
93
    /**
94
     *
95
     * @param Parser\HD4 $parser
96
     */
97 12
    public function __construct(Parser\HD4 $parser)
98
    {
99 12
        $this->parser = $parser;
100 12
    }
101
102
    /**
103
     *
104
     * @return Parser\HD4
105
     */
106 5
    public function getParser()
107
    {
108 5
        return $this->parser;
109
    }
110
111
    /**
112
     *
113
     * @param array $resultRaw
114
     *
115
     * @return bool
116
     */
117 4
    private function hasResult(array $resultRaw)
118
    {
119 4
        if (isset($resultRaw['general_browser']) && $this->isRealResult($resultRaw['general_browser'])) {
120 1
            return true;
121
        }
122
123 3
        if (isset($resultRaw['general_platform']) && $this->isRealResult($resultRaw['general_platform'])) {
124 1
            return true;
125
        }
126
127 2
        if (isset($resultRaw['general_model']) && $this->isRealResult($resultRaw['general_model'], 'device', 'model') && $this->isRealResult($resultRaw['general_vendor'], 'device', 'brand')) {
128 1
            return true;
129
        }
130
131 1
        return false;
132
    }
133
134
    /**
135
     *
136
     * @param Model\Browser $browser
137
     * @param array         $resultRaw
138
     */
139 3
    private function hydrateBrowser(Model\Browser $browser, array $resultRaw)
140
    {
141 3
        if (isset($resultRaw['general_browser'])) {
142 1
            $browser->setName($this->getRealResult($resultRaw['general_browser']));
143
        }
144 3
        if (isset($resultRaw['general_browser_version'])) {
145 1
            $browser->getVersion()->setComplete($this->getRealResult($resultRaw['general_browser_version']));
146
        }
147 3
    }
148
149
    /**
150
     *
151
     * @param Model\OperatingSystem $os
152
     * @param array                 $resultRaw
153
     */
154 3
    private function hydrateOperatingSystem(Model\OperatingSystem $os, array $resultRaw)
155
    {
156 3
        if (isset($resultRaw['general_platform'])) {
157 1
            $os->setName($this->getRealResult($resultRaw['general_platform']));
158
        }
159 3
        if (isset($resultRaw['general_platform_version'])) {
160 1
            $os->getVersion()->setComplete($this->getRealResult($resultRaw['general_platform_version']));
161
        }
162 3
    }
163
164
    /**
165
     *
166
     * @param Model\UserAgent $device
167
     * @param array           $resultRaw
168
     */
169 3
    private function hydrateDevice(Model\Device $device, array $resultRaw)
170
    {
171 3
        if (isset($resultRaw['general_model']) && $this->isRealResult($resultRaw['general_model'], 'device', 'model') && isset($resultRaw['general_vendor']) && $this->isRealResult($resultRaw['general_vendor'], 'device', 'brand')) {
172 1
            $device->setModel($this->getRealResult($resultRaw['general_model'], 'device', 'model'));
173 1
            $device->setBrand($this->getRealResult($resultRaw['general_vendor'], 'device', 'brand'));
174
        }
175 3
    }
176
177 5
    public function parse($userAgent, array $headers = [])
178
    {
179 5
        $headers['User-Agent'] = $userAgent;
180
181 5
        $parser = $this->getParser();
182
        // $config = $parser->config;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
183
184
        // $parser = new Parser\HD4($config);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
185
186
        /*
187
         * No result found?
188
         */
189 5
        if ($parser->deviceDetect($headers) !== true) {
190 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
191
        }
192
193 4
        $resultRaw = $parser->getReply();
194
195
        /*
196
         * No result found?
197
         */
198 4
        if (! isset($resultRaw['hd_specs']) || $this->hasResult($resultRaw['hd_specs']) !== true) {
199 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
200
        }
201
202
        /*
203
         * Hydrate the model
204
         */
205 3
        $result = new Model\UserAgent();
206 3
        $result->setProviderResultRaw($resultRaw['hd_specs']);
207
208
        /*
209
         * hydrate the result
210
         */
211 3
        $this->hydrateBrowser($result->getBrowser(), $resultRaw['hd_specs']);
212
        // renderingEngine not available
213 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw['hd_specs']);
214 3
        $this->hydrateDevice($result->getDevice(), $resultRaw['hd_specs']);
215
216 3
        return $result;
217
    }
218
}
219