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.

HandsetDetection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use HandsetDetection as Parser;
5
use UserAgentParser\Exception\InvalidArgumentException;
6
use UserAgentParser\Exception\NoResultFoundException;
7
use UserAgentParser\Model;
8
9
/**
10
 * Abstraction for ua-parser/uap-php
11
 *
12
 * @author Martin Keckeis <[email protected]>
13
 * @license MIT
14
 * @see https://github.com/HandsetDetection/php-apikit
15
 */
16
class HandsetDetection extends AbstractProvider
17
{
18
    /**
19
     * Name of the provider
20
     *
21
     * @var string
22
     */
23
    protected $name = 'HandsetDetection';
24
25
    /**
26
     * Homepage of the provider
27
     *
28
     * @var string
29
     */
30
    protected $homepage = 'https://github.com/HandsetDetection/php-apikit';
31
32
    /**
33
     * Composer package name
34
     *
35
     * @var string
36
     */
37
    protected $packageName = 'handsetdetection/php-apikit';
38
39
    protected $detectionCapabilities = [
40
41
        'browser' => [
42
            'name'    => true,
43
            'version' => true,
44
        ],
45
46
        'renderingEngine' => [
47
            'name'    => false,
48
            'version' => false,
49
        ],
50
51
        'operatingSystem' => [
52
            'name'    => true,
53
            'version' => true,
54
        ],
55
56
        'device' => [
57
            'model'    => true,
58
            'brand'    => true,
59
            'type'     => false,
60
            'isMobile' => false,
61
            'isTouch'  => false,
62
        ],
63
64
        'bot' => [
65
            'isBot' => false,
66
            'name'  => false,
67
            'type'  => false,
68
        ],
69
    ];
70
71
    protected $defaultValues = [
72
73
        'general' => [
74
            '/^generic$/i',
75
        ],
76
77
        'device' => [
78
            'model' => [
79
                '/analyzer/i',
80
                '/bot/i',
81
                '/crawler/i',
82
                '/library/i',
83
                '/spider/i',
84
            ],
85
        ],
86
    ];
87
88
    /**
89
     *
90
     * @var Parser\HD4
91
     */
92
    private $parser;
93
94
    /**
95
     *
96
     * @param Parser\HD4 $parser
97
     */
98 13
    public function __construct(Parser\HD4 $parser)
99
    {
100 13
        $this->parser = $parser;
101 13
    }
102
103
    /**
104
     *
105
     * @return Parser\HD4
106
     */
107 6
    public function getParser()
108
    {
109 6
        return $this->parser;
110
    }
111
112
    /**
113
     *
114
     * @param array $resultRaw
115
     *
116
     * @return bool
117
     */
118 5
    private function hasResult(array $resultRaw)
119
    {
120 5
        if (isset($resultRaw['general_browser']) && $this->isRealResult($resultRaw['general_browser'])) {
121 2
            return true;
122
        }
123
124 3
        if (isset($resultRaw['general_platform']) && $this->isRealResult($resultRaw['general_platform'])) {
125 1
            return true;
126
        }
127
128 2
        if (isset($resultRaw['general_model']) && $this->isRealResult($resultRaw['general_model'], 'device', 'model') && $this->isRealResult($resultRaw['general_vendor'], 'device', 'brand')) {
129 1
            return true;
130
        }
131
132 1
        return false;
133
    }
134
135
    /**
136
     *
137
     * @param Model\Browser $browser
138
     * @param array         $resultRaw
139
     */
140 4
    private function hydrateBrowser(Model\Browser $browser, array $resultRaw)
141
    {
142 4
        if (isset($resultRaw['general_browser'])) {
143 2
            $browser->setName($this->getRealResult($resultRaw['general_browser']));
144
        }
145 4
        if (isset($resultRaw['general_browser_version'])) {
146 2
            $browser->getVersion()->setComplete($this->getRealResult($resultRaw['general_browser_version']));
147
        }
148 4
    }
149
150
    /**
151
     *
152
     * @param Model\OperatingSystem $os
153
     * @param array                 $resultRaw
154
     */
155 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, array $resultRaw)
156
    {
157 4
        if (isset($resultRaw['general_platform'])) {
158 1
            $os->setName($this->getRealResult($resultRaw['general_platform']));
159
        }
160 4
        if (isset($resultRaw['general_platform_version'])) {
161 1
            $os->getVersion()->setComplete($this->getRealResult($resultRaw['general_platform_version']));
162
        }
163 4
    }
164
165
    /**
166
     *
167
     * @param Model\UserAgent $device
168
     * @param array           $resultRaw
169
     */
170 4
    private function hydrateDevice(Model\Device $device, array $resultRaw)
171
    {
172 4
        if (isset($resultRaw['general_model']) && $this->isRealResult($resultRaw['general_model'], 'device', 'model') && isset($resultRaw['general_vendor']) && $this->isRealResult($resultRaw['general_vendor'], 'device', 'brand')) {
173 1
            $device->setModel($this->getRealResult($resultRaw['general_model'], 'device', 'model'));
174 1
            $device->setBrand($this->getRealResult($resultRaw['general_vendor'], 'device', 'brand'));
175
        }
176 4
    }
177
178 6
    public function parse($userAgent, array $headers = [])
179
    {
180 6
        $headers['User-Agent'] = $userAgent;
181
182 6
        $parser = $this->getParser();
183
        // $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...
184
185
        // $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...
186
187
        /*
188
         * No result found?
189
         */
190 6
        $result    = $parser->deviceDetect($headers);
191 6
        $resultRaw = $parser->getReply();
192
193 6
        if ($result !== true) {
194 1
            if (isset($resultRaw['status']) && $resultRaw['status'] == '299') {
195
                throw new InvalidArgumentException('You need to warm-up the cache first to use this provider');
196
            }
197
198 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
199
        }
200
201
        /*
202
         * No result found?
203
         */
204 5
        if (! isset($resultRaw['hd_specs']) || $this->hasResult($resultRaw['hd_specs']) !== true) {
205 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
206
        }
207
208
        /*
209
         * Hydrate the model
210
         */
211 4
        $result = new Model\UserAgent($this->getName(), $this->getVersion());
212 4
        $result->setProviderResultRaw($resultRaw['hd_specs']);
213
214
        /*
215
         * hydrate the result
216
         */
217 4
        $this->hydrateBrowser($result->getBrowser(), $resultRaw['hd_specs']);
218
        // renderingEngine not available
219 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw['hd_specs']);
220 4
        $this->hydrateDevice($result->getDevice(), $resultRaw['hd_specs']);
221
222 4
        return $result;
223
    }
224
}
225